[
https://issues.apache.org/jira/browse/PDFBOX-3832?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16056316#comment-16056316
]
Tilman Hausherr commented on PDFBOX-3832:
-----------------------------------------
This code works for me - note the extra parameter in font loading. The font is
fully embedded in the default resources. Not using the extra parameter results
in nothing embedded anywhere, regardless if value set or not.
{code:java}
public class PDFBox3832EmbedFormFont
{
public static void main(String[] args) throws IOException
{
try (PDDocument doc = new PDDocument())
{
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(acroForm);
PDFont formFont = PDType0Font.load(doc, new
FileInputStream("DejaVuSans.ttf"), false);
final PDResources resources = new PDResources();
acroForm.setDefaultResources(resources);
final String fontName = resources.add(formFont).getName();
acroForm.setDefaultResources(resources);
String defaultAppearanceString = "/" + fontName + " 0 Tf 0 g";
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
textBox.setDefaultAppearance(defaultAppearanceString);
acroForm.getFields().add(textBox);
// Specify the annotation associated with the field
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(50, 700, 200, 50);
widget.setRectangle(rect);
widget.setPage(page);
page.getAnnotations().add(widget);
// set the field value
//textBox.setValue("Sample field");
doc.save("PDFBox3832.pdf");
}
}
}
{code}
Please give feedback whether your problem has been solved or not. I'm also
thinking about adding this code to the example subproject.
> 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
> Fix For: 2.0.7, 3.0.0
>
>
> 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]