Skip to content

Is there a way to get the PdfField's inside a fillable pdf in a specific order?? #1490

Closed Answered by satyajitnayk
satyajitnayk asked this question in Q&A
Discussion options

You must be logged in to vote

Found the solution:
using y coordinate(vertical position) we can sort the fields.

import {PDFDocument} from 'pdf-lib';
const fields = pdfDocument
      .getForm()
      .getFields()
      .sort((f1, f2) => {
        // sort by y coordinate value(y value more means the text is on top)
        const y1 = f1.acroField.getWidgets()[0].getRectangle().y;
        const y2 = f2.acroField.getWidgets()[0].getRectangle().y;
        return y1 < y2 ? 1 : y1 > y2 ? -1 : 0;
      });

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by satyajitnayk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant