I am trying to change all of the fields in a PDF form to use a TrueType
font that I am embedding. Or trying to embed more accurately.

I have had good success with embedding fonts went I am drawing text in a
document, but for form fields, I have been unable to programmatically force
the font to embed.

I load the font in the constructor like this:

*        final PDFont formFont = loadFontResourceWithDef(pdf, DEFAULT_F**O*
*NT_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;*
*    }*

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:

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

The result of this is that the appearances for the text fields are all
correct, but the font is never embedded, so it looks OK on my machine
(because the font that I am loading is available locally), but I get
charming empty rectangles on machines where the font is not present. If I
open the PDF in acrobat and manually change the font on one field and
change it back, the font them embeds and everything works fine.

I have uploaded an example to
https://dl.dropboxusercontent.com/u/25802656/ofev_rx_3338.pdf

Any thoughts would be helpful. I would rather not do a manual operation on
more than one hundred files.

Thank you, you the always helpful and wonderful PDFBox community.
-- 
*Evan Williams*
Sr. Software Engineer
evan.willi...@zapprx.com

*www.ZappRx.com <http://www.zapprx.com/>*

Reply via email to