[ 
https://issues.apache.org/jira/browse/PDFBOX-3832?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Evan Williams updated PDFBOX-3832:
----------------------------------
    Description: 
When working with AcroForms, attempting to se the font of a form field, the 
font will only properly embed if the form field contains at least one 
character. Fonts set on empty fields do not embed. The font is only embedded if 
it is actually used.

Example code:

{code:Java}
        final PDFont formFont = loadFontResourceWithDef(pdf, DEFAULT_FONT_PATH,
                DEF_DOCUMENT_FONT);
        final PDResources resources = acroForm.getDefaultResources();
        final String fontName = resources.add(formFont).getName();
        acroForm.setDefaultResources(resources);
        this.defaultAppearanceString = "/" + fontName + " 0 Tf 0 g";
    }

    public static PDFont loadFontResourceWithDef(PDDocument doc,
            String fontpath, PDFont defFont) {
        PDType0Font resFont = null;
        try {
            final InputStream is = 
this.getClass().getResourceAsStream(fontpath);
            if (is != null) {
                resFont = PDType0Font.load(doc, is);
            }
        } catch (IOException e) {
            log("Error loading font " + fontpath, e);
        }
        return resFont != null ? resFont : defFont;
    }

{code}

Presume that I have verified that the font actually is loaded into the formFont 
variable.

Some time later, I iterate over the fields in the acroform and, for each field, 
do this:

{code:Java}

            if (field instanceof PDTextField) {
                final PDTextField textfield = (PDTextField) field;
                textfield.setDefaultAppearance(defaultAppearanceString);
            }

{code}


  was:
When working with AcroForms, attempting to se the font of a form field, the 
font will only properly embed if the form field contains at least one 
character. Fonts set on empty fields do not embed. The font is only embedded if 
it is actually used.




> PDFBox Won't Embed a Font for a Form Field if Field is Empty
> ------------------------------------------------------------
>
>                 Key: PDFBOX-3832
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-3832
>             Project: PDFBox
>          Issue Type: Bug
>          Components: AcroForm, FontBox
>    Affects Versions: 2.0.5, 2.0.6
>            Reporter: Evan Williams
>
> When working with AcroForms, attempting to se the font of a form field, the 
> font will only properly embed if the form field contains at least one 
> character. Fonts set on empty fields do not embed. The font is only embedded 
> if it is actually used.
> Example code:
> {code:Java}
>         final PDFont formFont = loadFontResourceWithDef(pdf, 
> DEFAULT_FONT_PATH,
>                 DEF_DOCUMENT_FONT);
>         final PDResources resources = acroForm.getDefaultResources();
>         final String fontName = resources.add(formFont).getName();
>         acroForm.setDefaultResources(resources);
>         this.defaultAppearanceString = "/" + fontName + " 0 Tf 0 g";
>     }
>     public static PDFont loadFontResourceWithDef(PDDocument doc,
>             String fontpath, PDFont defFont) {
>         PDType0Font resFont = null;
>         try {
>             final InputStream is = 
> this.getClass().getResourceAsStream(fontpath);
>             if (is != null) {
>                 resFont = PDType0Font.load(doc, is);
>             }
>         } catch (IOException e) {
>             log("Error loading font " + fontpath, e);
>         }
>         return resFont != null ? resFont : defFont;
>     }
> {code}
> Presume that I have verified that the font actually is loaded into the 
> formFont variable.
> Some time later, I iterate over the fields in the acroform and, for each 
> field, do this:
> {code:Java}
>             if (field instanceof PDTextField) {
>                 final PDTextField textfield = (PDTextField) field;
>                 textfield.setDefaultAppearance(defaultAppearanceString);
>             }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to