Change field attribute and write new pdf

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 = {

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

while (iter.hasNext) {
var obj =“T”)
if (obj != null && obj.isString() && obj.getAsPDFText()) {
} 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 [
/Subtype /Widget
/T (original value)
/Type /Annot


What am I doing wrong?

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()