javafontspdf-generationpdfbox

Form field displays gibberish characters when using subsetted TrueType font


I'm using Apache PDFBox to create a PDF with form fields and embed a custom TrueType font (FreeSans.ttf) with subsetting enabled. However, when I set a value in the text field, it displays gibberish characters instead of the actual text.

Versions : 2.x, 3.x

Fonts: Any fonts loaded from external source except 14 Standard Fonts

Reproducible Example:

package org.example;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;

import java.io.InputStream;


public class Main {

    public static void main(String[] args) {
        try (PDDocument document = new PDDocument()) {

            PDPage page = new PDPage(PDRectangle.A4);
            document.addPage(page);

            PDAcroForm acroForm = new PDAcroForm(document);
            document.getDocumentCatalog().setAcroForm(acroForm);

            // Load external TrueType font with subsetting enabled
            PDType0Font font;
            try (InputStream fontStream = Main.class.getResourceAsStream("/FreeSans.ttf")) {
                if (fontStream == null) {
                    throw new RuntimeException("FreeSans.ttf not found in resources");
                }
                font = PDType0Font.load(document, fontStream, true);
            }

            // Add font to form resources
            PDResources formResources = new PDResources();
            String fontResourceName = formResources.add(font).getName();
            acroForm.setDefaultResources(formResources);
            acroForm.setDefaultAppearance("/" + fontResourceName + " 12 Tf 0 0 0 rg");

            // Create text field with value
            PDTextField textField = new PDTextField(acroForm);
            textField.setPartialName("testField");
            textField.setDefaultAppearance("/" + fontResourceName + " 12 Tf 0 0 0 rg");

            // Set widget rectangle and add to page
            textField.getWidgets().get(0).setRectangle(new PDRectangle(100, 700, 200, 20));
            textField.getWidgets().get(0).setPage(page);
            page.getAnnotations().add(textField.getWidgets().get(0));

            acroForm.getFields().add(textField);

            textField.setValue("Hello, this is a filled form field!");
            
            acroForm.setNeedAppearances(false);
            acroForm.refreshAppearances();
            
            document.save("output.pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Console Output:

WARNING: Using fallback font LiberationSans for CID-keyed TrueType font FreeSans
WARNING: widget of field testField has no rectangle, no appearance stream created

Solution

  • Never subset the font for acroform fields. Change

    font = PDType0Font.load(document, fontStream, true);
    

    to

    font = PDType0Font.load(document, fontStream, false);
    

    Some background in this answer.