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.

[Question] keys for PDFNames and PDFCatalog

See original GitHub issue

form_8949.pdf form_8949.pdf

Hi - I have been referencing other closed issues which have been helpful such as https://github.com/Hopding/pdf-lib/issues/109

Hopding and others are getting objects out of the pdfDoc.index with various getMaybe(key) calls from the catalog with keys such as 'AcroForm' as in the below code from the above issue. Is there any documentation that explains what all of these keys are? Although I can see them in catalog.validKeys, it feels like I’m arbitrarily pulling down these names, and I’m curious what they all do for me. For example, I’m following along with the code in the above issue, and the 'AcroForm' doesn’t seem to give me all of the fields in my pdf, it only returns 6 fields as opposed to close to a hundred fields on the form.

const getAcroFields = pdfDoc => {
  if (!pdfDoc.catalog.getMaybe('AcroForm')) return [];
  const acroForm = pdfDoc.index.lookup(pdfDoc.catalog.get('AcroForm'));

  if (!acroForm.getMaybe('Fields')) return [];
  const acroFields = pdfDoc.index.lookup(acroForm.get('Fields'));

  return acroFields.array.map(pdfDoc.index.lookup);
};

Similarly once a field is targeted say in an acroField, is there documentation stating what all of the keys are for an acroField? For a particular field I’m looking at, I see keys with names of ‘T’, ‘F’, ‘Subtype’, ‘DA’, ‘MK’ etc, and have no idea what they each represent.

Thank you. For reference, the form I have been looking at is attached if that helps.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Hopdingcommented, Feb 13, 2020

For future reference, the pdfDoc.index.index.entries() call can be replaced with pdfDoc.context.enumerateIndirectObjects().

2reactions
Hopdingcommented, Jun 15, 2019

I did a bit of investigation into the PDF you shared. It turns out that:

  • The 6 acroform fields correspond to the 6 check boxes in the document (3 per page).
  • The 238 are not part of the acroform. They’re just “loose” text fields attach to the pages.

I wrote a function to extract the text fields, and used it to populate each of them with their index number. Here’s the resulting document: filled_form_8949.pdf.

And here’s the script I used to create it:

// ...imports omitted...

const getTextFields = (pdfDoc) => {
  const indirectObjects = Array.from(pdfDoc.index.index.entries());
  return indirectObjects
    .filter(
      ([ref, object]) =>
        object instanceof PDFDictionary &&
        object.getMaybe('FT') === PDFName.from('Tx'),
    )
    .map(([ref, object]) => object);
};

const pdfDoc = PDFDocumentFactory.load(fs.readFileSync('./form_8949.pdf'));

const textFields = getTextFields(pdfDoc);

textFields.forEach((field, idx) => {
  field.delete('AP');
  field.set('V', PDFString.fromString(`Field ${idx}`));
});

const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc);

fs.writeFileSync('./filled_form_8949.pdf', pdfBytes)

I hope this helps. Please let me know if you have any further questions!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using iText Java to add a new entry to the PDF Catalog
To do that I am using the library iText Java and the new entry is a pair whose key is "/MyVar" and value...
Read more >
Example usage for com.itextpdf.text.pdf PdfDictionary ...
In this page you can find the example usage for com.itextpdf.text.pdf PdfDictionary getAsArray. Prototype. public PdfArray getAsArray(final PdfName key). Source ...
Read more >
PdfDictionary (iText 7 7.1.3 API)
A Dictionary is a mapping between keys and values. Keys are PdfNames and the values are PdfObjects . Each key can only be...
Read more >
[iText-questions] Does iText support PJTF
PJTF is just an extra key in the PDF catalog. ... So if I create a PdfName/PdfObject pair and put it into the...
Read more >
BCI AIBE 11 Questions and Answer Key (Set A)
BCI AIBE 11 Questions and Answer Key (Set A). Original Document (PDF) ». Contributed by Legally India (Legally India). Document, Plain Text, Thumbnail ......
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