Change field attribute and write new pdf

19 views
Skip to first unread message

M Pinney

unread,
Feb 19, 2021, 1:52:23 PM2/19/21
to PDFTron SDK
I'm trying to batch update all of the `T` attributes in a PDF.

I have written the following scala script which runs to completion, but cmp shows that the output PDF is identical to the input PDF.

def run (infile: File): Unit = {
    PDFNet.initialize();

    var doc = new PDFDoc("input.pdf")
    var iter = doc.fdfExtract().getFieldIterator

    while (iter.hasNext) {
      var obj = iter.next.findAttribute("T")
      if (obj != null && obj.isString() && obj.getAsPDFText()) {
          obj.setString("newtitle");
      }
    }

    doc.save(new FileOutputStream("output.pdf"), SDFDoc.SaveMode.INCREMENTAL, null);
}

Viewing the pdf in vim demonstrates that the `T` attributes are indeed unchanged:

12 0 obj
<<
  /AP <<
    /N 553 0 R
  >>
  /F 4
  /FT /Tx
  /MK <<
  >>
  /P 541 0 R
  /Rect [
    490.54
    735.856
    554.14
    746.416
  ]
  /Subtype /Widget
  /T (original value)
  /Type /Annot
>>
endobj

What am I doing wrong?
Message has been deleted

Ryan

unread,
Feb 19, 2021, 2:25:16 PM2/19/21
to PDFTron SDK

The issue is the call to fdfExtract() which exports (makes a copy) of the fields and returns them as a FDFDoc, so you are editing a temporary object. Which is why later when you call fdfExtract() you are getting the same original data, since you never edited the original PDFDoc.

If your intention is to edit the FDFDoc then keep the reference. FDFDoc fdfdoc = pdfdoc.fdfExtract();

If your intention is to edit the PDF itself, then erase your fdfExtract calls and instead call pdfdoc.getFieldIterator()

Reply all
Reply to author
Forward
0 new messages