Oh, I could reproduce this.

Then I debugged it. The code doesn't set the appearance if the /F entry is set. (That is a Javascript action)

There is a code comment:

            PDFormFieldAdditionalActions actions = field.getActions();

            // 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)
            {

This code part is from before it became an apache project, I don't have that file, didn't search for the ticket.

So remove the /F entry before setting, with

field.getActions().setF(null);

The drawback is that the "formatter" will be gone. A compromise would be to put it back after setting the field:

        PDDocument doc = PDDocument.load(...);
        PDField f = doc.getDocumentCatalog().getAcroForm().getField("dateField");
        PDAction fa = f.getActions().getF();
        f.getActions().setF(null);
        f.setValue("01.01.2020");
        f.getActions().setF(fa);


Tilman

Am 24.12.2019 um 16:57 schrieb Robert Pepersack:
Hi Tilman,
I'm using PDFBox version 2.0.17.  Here is my code:

  public void fillForm(Map<String, String> formMap)
{

File file = new File(myFile);

try (PDDocument doc = PDDocument.load(file)) {

PDDocumentCatalog docCatalog = doc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();

if (acroForm != null) {

for (Map.Entry<String, String> entry : formMap.entrySet()) {
String key = entry.getKey();
PDField field = acroForm.getField(key);

if (field instanceof PDVariableText) {

if (field instanceof PDTextField) {
PDTextField textField = PDTextField.class.cast(field);
textField.setValue(formMap.get(key));

} else if (field instanceof PDChoice) {
PDChoice choice = PDChoice.class.cast(field);
choice.setValue(formMap.get(key));

}
} else if (field instanceof PDCheckBox) {
PDCheckBox checkBox = PDCheckBox.class.cast(field);
boolean value = Boolean.valueOf(formMap.get(key));
if (value) {
checkBox.check();
} else {
checkBox.unCheck();
}

} else if (field instanceof PDRadioButton) {
PDRadioButton radioButton = PDRadioButton.class.cast(field);
radioButton.setValue(formMap.get(key));

}
}

File outputFile = new File("test.pdf");
doc.save(outputFile);
}
} catch (IOException e) {
throw new AssistBusinessException(e);
}
}

}

On Tue, Dec 24, 2019 at 10:43 AM Tilman Hausherr <thaush...@t-online.de>
wrote:

Am 23.12.2019 um 22:09 schrieb Robert Pepersack:
2. Run my Java program that loads the .pdf file, populates its form
fields, and saves it to a new file on my local disk.
What code did you use to populate the date field, and what PDFBox
version did you use (the current version is 2.0.18)? I'm asking because
the date field does not have an appearance stream.

Tilman


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to