You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Tilman Hausherr (JIRA)" <ji...@apache.org> on 2019/02/07 19:04:00 UTC

[jira] [Commented] (PDFBOX-4233) Flatten does not work properly on field with type 0 font

    [ https://issues.apache.org/jira/browse/PDFBOX-4233?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16762975#comment-16762975 ] 

Tilman Hausherr commented on PDFBOX-4233:
-----------------------------------------

Your file has empty appearance streams and needAppearances set so PDFBox should first draw the appearances, but it doesn't do that (Adobe does). This could be done like this by the user:
{code:java}
for (PDField field : acroForm.getFieldTree())
{
    if (field instanceof PDTerminalField)
    {
        field.setValue(field.getValueAsString());
    }
}
{code}
but it doesn't work:
{code:java}
java.lang.IllegalArgumentException: No glyph for U+011B (ě) in font AACDGC+TimesSansSerif
	at org.apache.pdfbox.pdmodel.font.PDCIDFontType2.encode(PDCIDFontType2.java:366)
	at org.apache.pdfbox.pdmodel.font.PDType0Font.encode(PDType0Font.java:433)
	at org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:328)
	at org.apache.pdfbox.pdmodel.font.PDFont.getStringWidth(PDFont.java:357)
	at org.apache.pdfbox.pdmodel.interactive.form.PlainTextFormatter.format(PlainTextFormatter.java:195)
	at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.insertGeneratedAppearance(AppearanceGeneratorHelper.java:507)
	at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.setAppearanceContent(AppearanceGeneratorHelper.java:366)
	at org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.setAppearanceValue(AppearanceGeneratorHelper.java:238)
	at org.apache.pdfbox.pdmodel.interactive.form.PDTextField.constructAppearances(PDTextField.java:262)
	at org.apache.pdfbox.pdmodel.interactive.form.PDTerminalField.applyChange(PDTerminalField.java:226)
	at org.apache.pdfbox.pdmodel.interactive.form.PDTextField.setValue(PDTextField.java:218)
{code}
I haven't understood all the details, but it seems that this font subset is missing some details to encode unicode to the codes used in the PDF.

The best is not to use subsets for fonts in acroform fields. This doesn't end well, because a user might use characters not in the font.

Another solution would be to replace the font:
{code:java}
PDDocument sourcePdfForm = PDDocument.load(new File("sampleform_filled.pdf"));
PDAcroForm acroForm = sourcePdfForm.getDocumentCatalog().getAcroForm();
PDResources defaultResources = acroForm.getDefaultResources();
InputStream is = new FileInputStream("c:/windows/fonts/times.ttf");
defaultResources.put(COSName.getPDFName("F5"), PDType0Font.load(sourcePdfForm, is, false));
for (PDField field : acroForm.getFieldTree())
{
    if (field instanceof PDTerminalField)
    {
        field.setValue(field.getValueAsString());
    }
}
acroForm.flatten();
sourcePdfForm.save("sampleform_filled_flattened.pdf");
{code}

> Flatten does not work properly on field with type 0 font
> --------------------------------------------------------
>
>                 Key: PDFBOX-4233
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4233
>             Project: PDFBox
>          Issue Type: Bug
>          Components: AcroForm
>    Affects Versions: 2.0.10
>         Environment: win10, JDK 1.8
>            Reporter: rumburak
>            Priority: Major
>         Attachments: sampleform_filled.pdf, sampleform_filled_flattened.pdf, sampleform_filled_in_acrobat_reader.png
>
>
> Pdf form *sampleform_filled.pdf* contains one field called "txtField" with this content: "ěščřžýáíéúůóďťĚŠČŘŽÝÁÍÉÚŮÓĎŤ".  The font used in this field is "/F5"
> when you run this code:
>  
> {code:java}
> try {
>  PDDocument sourcePdfForm = PDDocument.load(new File("sampleform_filled.pdf"));
>  PDAcroForm acroForm = sourcePdfForm.getDocumentCatalog()
>  .getAcroForm();
>  acroForm.flatten();
>  sourcePdfForm.save("sampleform_filled_flattened.pdf");
> } catch (IOException e) {
>  e.printStackTrace();
> }{code}
>  
>  
> You will get *sampleform_filled_flattened.pdf* with empty or not visible content of the field.
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org