Hi guys:
I tried to fill a existing PDF form using PDFBox, the fields I tried to
fill is textfield and check box. It runs OK, and I can verify all values are
correctly set in acroFom, but I get some strange results while trying to view
it:
1, after save the doc, and open it using Adobe Reader, the text data is
missing, but the check box is in the right place. If searching the pdf using
text editor, all values are there, which means there are inserted, but not
picked up by AdobeReader.
2, If I print the pdf or open it through a web browser, all field values
shows.
I am new to this project and PDF. I will appreciate if anybody can help me
to find out the reason why the field values are not viewable in Adobe Reader.
The code I use to fill the form is below the email.
Thank you!
Ge
=====================================================================
public static void main(String[] args) {
PDDocument doc = null;
try {
doc = PDDocument.load("./test.pdf");
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
PDField field = (PDField) acroForm.getFields().get(0);
setField(acroForm, "testform[0].Page1[0].last_name[0]", "HELLO");
setField(acroForm, "testform[0].Page1[0].first_name[0]", "World");
setFieldChecked(acroForm, PDF_700U_Constants.FILER_INFO_YES);
doc.save("test.pdf");
//doc.print();
} catch (Exception e) {
System.out.println(e);
} finally {
if (doc != null) {
try {
doc.close();
} catch (Exception e2) {
System.err.println("Unable to close document");
}
}
}
}
protected static void setField(PDAcroForm acroForm, String name, String
value) throws IOException {
PDField field = acroForm.getField(name);
if (field != null) {
((PDTextbox)field).setValue(value);
} else {
System.err.println("*********************No field found with name:" + name);
}
}
protected static void setFieldChecked(PDAcroForm acroForm, String name) throws
IOException {
PDField field = acroForm.getField(name);
if (field != null) {
((PDCheckbox) field).check();
} else {
System.err.println("No name found for this field: " + name);
}
}