BTW,

what you might also want to check is if the JavaScript in the form and your 
input provide a valid result i.e. the value passed
doesn't lead to a JavaScript error.

BR
Maruan

 
> Hi,
> 
> your are missing to set the action to null before the setValue call!
> 
> actions.setF(null);
> textField.setValue(formMap.get(key));
> actions.setF(action);
> 
> BR
> Maruan
> 
>  
> > This didn't work for me.  Here is the code snippet that I modified:
> > 
> >       if (field instanceof PDTextField) {
> > PDTextField textField = PDTextField.class.cast(field);
> > 
> > PDFormFieldAdditionalActions actions = textField.getActions();
> > PDAction action = null;
> > if (actions != null) {
> > action = actions.getF();
> > }
> > textField.setValue(formMap.get(key));
> > if (action != null) {
> > actions.setF(action);
> > }
> > }
> > 
> > I debugged it in my IDE.  I see that the action variable is getting set by
> > actions.getF(),  and that setF() is getting executed with a non-null action.
> > 
> > Thanks!
> > 
> > On Tue, Dec 24, 2019 at 11:27 AM Tilman Hausherr <thaush...@t-online.de>
> > wrote:
> > 
> > > 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
> > > 
> > > 
-- 
Maruan Sahyoun

FileAffairs GmbH
Josef-Schappe-Straße 21
40882 Ratingen

Tel: +49 (2102) 89497 88
Fax: +49 (2102) 89497 91
sahy...@fileaffairs.de
www.fileaffairs.de

Geschäftsführer: Maruan Sahyoun
Handelsregister: AG Düsseldorf, HRB 53837
UST.-ID: DE248275827


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

Reply via email to