PDFBox inconsistent PDTextField behaviour after setValue Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of PDFBox inconsistent PDTextField behaviour after setValue without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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)

We are here to answer your question about PDFBox inconsistent PDTextField behaviour after setValue - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji