Reputation: 10068
I've write a simple code for edit content of a form inside a pdf. Everithing works fine (new pdf is created with modified form) but a strange excetpion about fonts were thrown. This is my code:
try {
PDDocument pdfDoc = PDDocument.load("/home/giozh/universita/schedepazienti.pdf");
PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField("dx1");
if (field != null) {
field.setValue("asd");
} else {
System.err.println("No field found with name:" + "applicationPrepaid[0].#pageSet[0].Pagina1[0].txtFirstName[0]");
}
pdfDoc.save("/home/giozh/universita/schedepazienti1.pdf");
pdfDoc.close();
} catch (IOException ex) {
Logger.getLogger(Prove.class.getName()).log(Level.SEVERE, null, ex);
} catch (COSVisitorException ex) {
Logger.getLogger(Prove.class.getName()).log(Level.SEVERE, null, ex);
}
and this is exception:
Grave: error while creating a font
java.io.IOException: Cannot create font as /SubType is not set.
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:88)
at org.apache.pdfbox.pdmodel.PDResources.getFonts(PDResources.java:203)
at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:439)
at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
at prove.Prove.main(Prove.java:37)
(line 37 is where i call setValue() method) how can i fix it?
Upvotes: 1
Views: 3042
Reputation: 1
I just wanted to add that I was getting a similar issue for Helvetica font, which is a standard font It turned out to be the size of the form text area. The form would fill, but when it went to flatten the fields would disappear. I think this was a combination of issues (wrapping, overflow, etc), but the only error in the logs was about the font. The form was created using adobe, and filled with PDFbox
Upvotes: 0
Reputation: 95898
Your PDF has an error which gracefully (merely with some logging output) is ignored.
The AcroForm dictionary in your PDF looks like this:
4 0 obj
<<
/Fields [ 12 0 R ]
/DA(/Helvetica 0 Tf 0 g )
/DR
<<
/Font
<<
/Helvetica 11 0 R
/Encoding<</PDFDocEncoding 10 0 R>>
>>
>>
/NeedAppearances true
>>
endobj
The Font dictionary in there contains one entry for the font Helvetica (which is the font used in the field in question, cf. its DA value /Helvetica 0 Tf 0.000000 0.000000 0.000000 rg) and an entry for Encoding whose value does not represent a font at all.
PDFBox tries to parse that "font" called Encoding and fails doing so. but PDResources.getFonts()
only logs the resulting exception and continues ignoring this issue:
try
{
newFont = PDFontFactory.createFont( (COSDictionary)font );
}
catch (IOException exception)
{
LOG.error("error while creating a font", exception);
}
(pdfbx-1.8.2.jar, org.apache.pdfbox.pdmodel.PDResources)
Thus, you see the exception headed by "error while creating a font" (because it explicitly is logged like that) but the result is created all right (because the exception essentially is ignored).
Upvotes: 1