Prabhjot Singh Rai
Prabhjot Singh Rai

Reputation: 2515

How to fill PDF form with specific font using pdfbox?

I am trying to fill pdf form and I am able to fill it using the following approach through PDFBox library.

val pdf: PDDocument = PDDocument.load(file)
pdf.setAllSecurityToBeRemoved(true)
val docCatalog: PDDocumentCatalog = pdf.getDocumentCatalog
val acroForm: PDAcroForm = docCatalog.getAcroForm

def populateFields(inputJson: String, targetPdfPath: String): Unit = {
    val valueMap: Array[Field] = gson.fromJson(inputJson, classOf[Array[Field]])
    valueMap.foreach((field) => {
      val pdField: PDField = acroForm.getField(field.name)

      if (pdField != null) {
        pdField.setValue(field.value)
      } else {
        println(s"No field with name ${field.name}")
      }
    })

    pdf.save(targetPdfPath)
    pdf.close()
  }

The only problem is, I don't see any option to set the font before filling the pdf. Can you help me here?

Upvotes: 1

Views: 2022

Answers (1)

Emilio Dalla Torre
Emilio Dalla Torre

Reputation: 146

You can achieve it by using these methods (note that you have to use PDFBox 1.8.15, not the newer 2.0).

// Set the field with custom font.
private void setField(String name, String value, String fontSource) throws IOException {
    PDDocumentCatalog docCatalog;
    PDAcroForm acroForm;
    PDField field;
    COSDictionary dict;
    COSString defaultAppearance;
    docCatalog = pdfTemplate.getDocumentCatalog();
    acroForm = docCatalog.getAcroForm();
    field = acroForm.getField(name);
    dict = (field).getDictionary();
    defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);

    if (defaultAppearance != null)
    {
        dict.setString(COSName.DA, "/" + fontName + " 10 Tf 0 g");
        if (name.equalsIgnoreCase("Field1")) {
            dict.setString(COSName.DA, "/" + fontName + " 12 Tf 0 g");
        }
    }
    if (field instanceof PDTextbox)
    {
        field = new PDTextbox(acroForm, dict);
        (field).setValue(value);
    }
}

// Set the field with custom font.
private List<String> prepareFont(PDDocument _pdfDocument, List<PDFont> fonts) {
    PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog();
    PDAcroForm acroForm = docCatalog.getAcroForm();

    PDResources res = acroForm.getDefaultResources();
    if (res == null)
        res = new PDResources();

    List<String> fontNames = new ArrayList<>();
    for (PDFont font: fonts)
    {
        fontNames.add(res.addFont(font));
    }

    acroForm.setDefaultResources(res);
    return fontNames;
}

// Set the field with custom font.
private PDFont loadTrueTypeFont(PDDocument _pdfDocument, String resourceName) throws IOException
{
    return PDTrueTypeFont.loadTTF(_pdfDocument, new File(resourceName));
}

Now, you only have to source the method setField with the name of the field, the value you want to insert and a string which is the path to the TTF font you wanna use.

Hope it helps!

Upvotes: 2

Related Questions