PDFBox inconsistent PDTextField behaviour after setValue

PDFBox setValue() is not setting data for each PDTextField. It is saving few fields. It is not working for fields which have similar appearance in getFullyQualifiedName().

Note: field.getFullyQualifiedName() { customdutiesa, customdutiesb, customdutiesc } it is working for customdutiesa, but not working for customdutiesb and customdutiesc etc…

public void testb3Generator() throws IOException {
    File f = new File(inputFile);

    outputFile = String.format("%s_b3-3.pdf", "123");

    try (PDDocument document = PDDocument.load(f)) {

        PDDocumentCatalog catalog = document.getDocumentCatalog();
        PDAcroForm acroForm = catalog.getAcroForm();
        int i = 0;
        for (PDField field : acroForm.getFields()) {
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;


        document.save(new File(outputFile));
    catch (Exception e) {


Input pdf link : https://s3-us-west-2.amazonaws.com/kx-filing-docs/b3-3.pdf Ouput pdf link : https://kx-filing-docs.s3-us-west-2.amazonaws.com/123_b3-3.pdf


The problem is that under certain conditions PDFBox does not construct appearances for fields it sets the value of, and, therefore, during flattening completely forgets the field content:

// in case all tests fail the field will be formatted by acrobat
// when it is opened. See FreedomExpressions.pdf for an example of this.  
if (actions == null || actions.getF() == null ||
    widget.getCOSObject().getDictionaryObject(COSName.AP) != null)
    ... generate appearance ...


I.e. if there is a JavaScript action for value formatting associated with the field and no appearance stream is yet present, PDFBox assumes it does not need to create an appearance (and probably would do it wrong anyways as it does not use that formatting action).

In case of a use case later flattening the form, that assumption of PDFBox obviously is wrong.

To force PDFBox to generate appearances for those fields, too, simply remove the actions before setting field values:

if (field instanceof PDTextField) {
    PDTextField textField = (PDTextField) field;

(from FillAndFlatten test testLikeAbubakarRemoveAction)

Leave a Reply

Your email address will not be published. Required fields are marked *