1

I want to batch update the titles of all objects in a PDF. Is there a way for me to do this? I had in mind to iterate through the fields and change the T values, but this appears not to work; changes to the fields don't persist from one iteration to the next, much less appear in the saved output file:

PDFNet.initialize();
var doc = new PDFDoc(infile.getAbsolutePath)
var iter = doc.fdfExtract().getFieldIterator

while (iter.hasNext) {
  var field = iter.next
  var obj = field.findAttribute("T")
  if (obj != null && obj.isString) {
      obj.setString("new title")
      println(field.getName) // Outputs "new title"
  }
}

iter = doc.fdfExtract().getFieldIterator
while (iter.hasNext) {
  var field = iter.next
  var obj = field.findAttribute("T")
  if (obj != null && obj.isString) {
      println(field.getName) // Outputs the original title
  }
}

doc.save(new FileOutputStream("out.pdf"), SDFDoc.SaveMode.INCREMENTAL, null)
doc.close

Here's a decompressed, toy pdf on which I've experimented (uploaded as a text file). It has only one input.

JellicleCat
  • 28,480
  • 24
  • 109
  • 162

1 Answers1

0

The issue is that you are calling 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()

Ryan
  • 2,473
  • 1
  • 11
  • 14
  • Thanks, @Ryan I see that `findAttribute` is described in the API documentation here: https://www.pdftron.com/api/PDFTronSDK/java/com/pdftron/fdf/FDFField.html#findAttribute(java.lang.String) I'm not sure if I understood your comment about it. – JellicleCat Feb 17 '21 at 01:54
  • Here's [a decompressed, toy pdf](https://pastebin.com/8ejex7Yb) on which I've experimented (uploaded as a text file). It has only one input. And I've [updated my code](https://pastebin.com/S4Y7B976) to use `field.getSDFObj().findObj("T")`, yet the results are the same as before. – JellicleCat Feb 17 '21 at 01:57
  • @JellicleCat I see the issue now, thanks for the extra info. I updated my answer. – Ryan Feb 17 '21 at 17:43