Is the file you uploaded the result before or after your operation? Because everything works nicely with Adobe. The font Noto Sans is embedded, I don't have it on my machine.

If it is the result "before" or "with Adobe", then please upload the result "after PDFBox" (and if possible, screenshots of "own machine" and "other machine without the font") and if possible use an unusual font, and also try


resFont = PDType0Font.load(doc, is, false);


I haven't tried running your code yet.

Tilman




Am 20.04.2017 um 18:59 schrieb Evan Williams:
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.



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

Reply via email to