I renamed fields in a PDF document "source.PDF" and saved the document as
'target.PDF". I was then not able to set values to the fields in the saved
document 'target.PDF". I also observed reduction in the size of the saved PDF -
source.PDF (3.34 MB) - target.PDF (911 KB).
Can anyone help? The code I used to rename the fields is given below:
PDDocument pdfDocument = PDDocument.load(pdfSource);
try
{
if (pdfDocument.isEncrypted())
{
pdfDocument.decrypt("");
}
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
if (acroForm != null)
{
List pdfFields = acroForm.getFields();
if (pdfFields != null)
{
PDField pdfField;
for (int i = 0; i < pdfFields.size(); i++)
{
pdfField = (PDField) pdfFields.get(i);
String fieldName = pdfField.getPartialName();
if( fieldName.equalsIgnoreCase("Test1") ){
String newFieldName = "acco...@name1";
pdfField.setPartialName(newFieldName);
}
}
pdfDocument.save(pdfOutput);
} else {
return "No Acro Form Fields found in PDF Document " + pdfSource + "!";
}
} else {
return "PDF document " + pdfSource + " not able to access!";
}
} catch (Exception e) {
return "PDF Document " + pdfSource + " is encrypted! Cannot process.";
}
pdfDocument.close();
Thanks & Regards
Venkatesh Prasad.B.K.