Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-3832

PDFBox Won't Embed a Font for a Form Field if Field is Empty

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.5, 2.0.6
    • Fix Version/s: 2.0.7, 3.0.0 PDFBox
    • Component/s: AcroForm, FontBox
    • Labels:
      None

      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:

              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;
          }
      
      

      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);
                  }
      
      

        Attachments

          Activity

            People

            • Assignee:
              tilman Tilman Hausherr
              Reporter:
              evanjpw Evan Williams
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: