Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Created November 18, 2014 18:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JamoCA/96a9f3ce3ebdca2a052b to your computer and use it in GitHub Desktop.
Save JamoCA/96a9f3ce3ebdca2a052b to your computer and use it in GitHub Desktop.
Using ColdFusion & iText to Rename PDF Form Fields
<cfscript>
/* If merging multiple PDFs and field names are not unique, they will all be bound/edited as a single group.
Use this technique to append a generic suffix to each fieldname to make it unique (then ignore/santize during form processing).
Credit ~2009: http://itext-general.2136553.n4.nabble.com/Problem-Renaming-Fields-with-iText-and-ColdFusion-9-td2141953.html
*/
newFieldSuffix = "_" & randRange(1,10000, "SHA1PRNG"); /* Generate a field suffix to make fieldname unique */
inFile = "c:\generic_input_fields.pdf";
outFile = "c:\unique_input_fields" & newFieldSuffix & ".pdf";
pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader");
outputStream = createObject("java", "java.io.FileOutputStream");
pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper");
pdfWriter = createObject("java", "com.lowagie.text.pdf.PdfWriter");
pdfReader.init( inFile );
outputStream.init( outFile );
pdfStamper.init(pdfReader, outputStream);
thisForm = pdfStamper.getAcroFields();
strFormFields = thisForm.getFields();
oldFormFields = ListToArray(StructKeyList(strFormFields));
for (oldFormField in oldFormFields) {
thisForm.renameField(oldFormField, oldFormField & newFieldSuffix);
}
pdfStamper.close();
outputStream.close();
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment