question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] API to access coordinates and dimensions of form fields

See original GitHub issue

Looking at the low level acroform spec, we should be able to get dimensions / positions of fields.

A good use case for this is knowing where and what size to stamp an image. We can leave a blank readOnly field in our form, and effectively “fill” it with an image.

const form = doc.getForm();
const field = doc.getField('somefield');

// This would be useful
const fieldDimensions = field.getDimensions(); // {x: number, y: number, width: number, height: number}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
Hopdingcommented, Sep 15, 2020

Hello @jamesmfriedman!

Happy to accept PRs for this. The information is certainly accessible. The main thing to be aware of when designing such an API is that form fields do not necessarily have just one single location on a page. The same field can actually be rendered in multiple locations on different pages. So, effectively, a single field can have multiple coordinates and dimensions. See https://pdf-lib.js.org/docs/api/classes/pdffield for a bit more detail about this.

Here’s an example of how you can access the information with the current APIs:

import { PDFDocument } from 'pdf-lib';

(async () => {
  const url = 'https://pdf-lib.js.org/assets/dod_character.pdf';
  const fancyFields = await fetch(url).then((res) => res.arrayBuffer());

  const pdfDoc = await PDFDocument.load(fancyFields);

  const form = pdfDoc.getForm();

  const textField = form.getTextField('CharacterName 2');

  const widgets = textField.acroField.getWidgets();

  widgets.forEach((w) => {
    const rect = w.getRectangle();
    console.log(rect);
  });
})();

// Output:
//   { x: 47.52, y: 705.92, width: 208.44, height: 20.879999999999995 }

Note also that if you’re specifically looking to stamp images on fields, pdf-lib provides a PDFButton.setImage method that does exactly this. Though it currently only exists for button fields.

4reactions
btecucommented, Dec 11, 2020

I’m not sure it makes sense to mix the page with the coordinates. Once #695 gets merged, you would be able to find the page or page number of a widget:

let page = document.getPages().find(x => x.ref === widget.P());
let pageNumber = document.getPages().findIndex(x => x.ref === widget.P());

element.getCoordinates() is trivial to implement, if we just want the coordinates. @Hopding is the plan to keep acroField private? We could document it, along with widget. Otherwise, we could implement a getCoordinates() on the element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the Form_Fields_Per_Document parameter
The form_fields_per_document parameter allows you to place fields on a document using a coordinate system. You can set the x and y coordinates...
Read more >
Query (Feature Service/Layer)—ArcGIS REST APIs
The feature service layer Query operation supports querying the count of distinct features within a field using the returnDistinctValues and returnCountOnly ...
Read more >
Coordinates | Maps JavaScript API - Google Developers
Returns a string of the form "lat,lng" for this LatLng. We round the lat/lng values to 6 decimal places by default. LatLngLiteral interface....
Read more >
jQuery API Documentation
jQuery API. jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, ...
Read more >
Web Services APIs - Mapbox docs
The Mapbox web services APIs allow you to programmatically access Mapbox tools and ... API, Default coordinates per request, Default requests per minute ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found