Christoph Grimmer
Christoph Grimmer

Reputation: 4323

How to substitute missing font when filling a form with PDFBox?

I'm trying to fill out a bunch of PDF Forms using PDFBox 2.0.8. For some documents I get the following error when setting the PDTextField's value:

java.io.IOException: Could not find font: /ArialMT

Apparently the font is not correctly embedded as is often the case with proprietary Microsoft fonts.

How can I tell PDFBox to substitute the font e.g. with "normal" Arial or some other font? Setting the fields DA string to "/Helv 0 tf 0 g" resulted in a NullPointerException.

Upvotes: 4

Views: 3413

Answers (2)

Christoph Grimmer
Christoph Grimmer

Reputation: 4323

Based on the comments from Tilman Hausherr I built a first fix which works independent from the operating system (which is a Linux in my case).

acroForm.defaultResources.put(COSName.getPDFName("ArialMT"),
    PDType0Font.load (pdDocument, this.javaClass.classLoader.getResourceAsStream("fonts/ARIALMT.ttf"), false))

This will only work for this particular font, though. What's still missing - and was actually the main intention of my question - is an option to tell PDFBox to fall back to a certain font resp. DA if the font that is required cannot be provided.

After Tilman again came for the rescue I can now present the complete solution. Again, this is Kotlin, not Java:

PDDocument.load(file).use { pdDocument ->
    val acroForm = pdDocument.documentCatalog.acroForm
    acroForm.defaultResources.put(COSName.getPDFName("ArialMT"),
            PDType0Font.load (pdDocument, this.javaClass.classLoader.getResourceAsStream("fonts/ARIALMT.ttf"), false))
    val pdField: PDField? = acroForm.getField(fieldname)
    val value = ...
    when (pdField) {
        is PDCheckBox -> {
            if (value is Boolean) {
                when (value) {
                    true -> pdField.check()
                    false -> pdField.unCheck()
                }
            } else {
                log.error("RENDER_FORM: Need Boolean for ${pdField.fullyQualifiedName} but got $value")
            }
        }
        is PDTextField -> {
            try {
                pdField.value = value?.toString() ?: ""
            } catch (ioException: IOException) {
                pdField.cosObject.setString(COSName.DA, "/Helv 0 Tf 0 g")
                pdField.value = value?.toString() ?: ""
                log.error("RENDER_FORM: Writing text field failed: ${ioException.message}")
            }
        }
        null -> {
            log.error("RENDER_FORMULAR: Formfield $fieldname does not exist in $name")
        }
        else -> log.error("RENDER_FORMULAR: Formfield $pdField ($fieldname) is of unhandled type ${pdField.fieldType}")
    }

    val stream = ByteArrayOutputStream()
    pdDocument.save(stream)
    pdDocument.close()
    return stream.toByteArray()
}

Upvotes: 6

Tilman Hausherr
Tilman Hausherr

Reputation: 18926

Add "ArialMT" to the default resources:

try (PDDocument doc = PDDocument.load(new File("F2_Datenblatt_022015.pdf")))
{
    PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
    PDField field = acroForm.getField("Vorname_Name");

    // fails with IOException as described in question
    //field.setValue("Tilman Hausherr");

    // Method 1, just add type1 Helvetica (allows only WinAnsiEncoding glyphs)
    //acroForm.getDefaultResources().put(COSName.getPDFName("ArialMT"), PDType1Font.HELVETICA);

    // Method 2, add the full Arial font (allows for more different glyphs)
    // important: use the method that switches off subsetting
    acroForm.getDefaultResources().put(
        COSName.getPDFName("ArialMT"), 
        PDType0Font.load(doc, new FileInputStream("c:/windows/fonts/arial.ttf"), false));


    field.setValue("Tilman Hausherr");

    doc.save("F2_Datenblatt_022015-mod.pdf");
}

Update: Turns out the code in the question would have worked too with the file - almost. It's "Tf" and not "tf", so the string would have been "/Helv 0 Tf 0 g". We'll research how to avoid an NPE and get a meaningful exception.

Upvotes: 2

Related Questions