You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Robert Pepersack <ro...@itgfirm.com> on 2019/12/23 21:09:16 UTC

Date Field Contents Not Visible

Hi,
I have used PDFBox to create a .pdf file from a .pdf file that I created in
Adobe Acrobat Pro DC version 2019.021.20061.

Here is the process I've been working on:
1.  Create "template" .pdf file in Adobe Acrobat Pro DC
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.
3.  Open the new .pdf file in Adobe Acrobat Reader.

The .pdf file that I created has a date field that doesn't display the date
after I run PDFBox to set all of the fields in the file.  The other fields,
including a text field, a list box, a combo box, a check box, and a radio
button group all get populated, and display correctly in Adobe Acrobat
Reader DC version 2019.021.20061 (same version as Acrobat Pro).

When I click in the date field, the date appears.  But, when I move the
focus away from the date field, the date disappears.

So, it tried setting the AcroForm's "NeedAppearances" property to true,
like this:  acroForm.setNeedAppearances(true).  That makes the date in the
date field display when I open the new .pdf file, but it has an unwanted
side effect.  When I close the document, without making any changes, I get
a message box that asks me if I want to save the changes before closing.

Does anyone have any insight?  I have attached the output file, which
doesn't have "NeedAppearances" set to true (test.pdf).  I have also
attached my "template" file (Acrobat_DC.pdf).

Thanks!

Re: Date Field Contents Not Visible

Posted by Robert Pepersack <ro...@itgfirm.com>.
I agree with you, Maruan:   We should be able to remove the test for a
JavaScript action and generate the appearance regardless.

On Tue, Dec 24, 2019 at 12:56 PM Robert Pepersack <
robert.pepersack@itgfirm.com> wrote:

> Adding actions.setF(null) made it work!  Thank you!!
>
>
>
> On Tue, Dec 24, 2019 at 12:32 PM Maruan Sahyoun <sa...@fileaffairs.de>
> wrote:
>
>> 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 <
>> THausherr@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 <
>> THausherr@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-unsubscribe@pdfbox.apache.org
>> > > > > > For additional commands, e-mail: users-help@pdfbox.apache.org
>> > > > > >
>> > > > > >
>> > > >
>> > > >
>> ---------------------------------------------------------------------
>> > > > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> > > > For additional commands, e-mail: users-help@pdfbox.apache.org
>> > > >
>> > > >
>> --
>> Maruan Sahyoun
>>
>> FileAffairs GmbH
>> Josef-Schappe-Straße 21
>> 40882 Ratingen
>>
>> Tel: +49 (2102) 89497 88
>> Fax: +49 (2102) 89497 91
>> sahyoun@fileaffairs.de
>> www.fileaffairs.de
>>
>> Geschäftsführer: Maruan Sahyoun
>> Handelsregister: AG Düsseldorf, HRB 53837
>> UST.-ID: DE248275827
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>>

Re: Date Field Contents Not Visible

Posted by Robert Pepersack <ro...@itgfirm.com>.
Adding actions.setF(null) made it work!  Thank you!!



On Tue, Dec 24, 2019 at 12:32 PM Maruan Sahyoun <sa...@fileaffairs.de>
wrote:

> 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 <
> THausherr@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 <
> THausherr@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-unsubscribe@pdfbox.apache.org
> > > > > > For additional commands, e-mail: users-help@pdfbox.apache.org
> > > > > >
> > > > > >
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> > > > For additional commands, e-mail: users-help@pdfbox.apache.org
> > > >
> > > >
> --
> Maruan Sahyoun
>
> FileAffairs GmbH
> Josef-Schappe-Straße 21
> 40882 Ratingen
>
> Tel: +49 (2102) 89497 88
> Fax: +49 (2102) 89497 91
> sahyoun@fileaffairs.de
> www.fileaffairs.de
>
> Geschäftsführer: Maruan Sahyoun
> Handelsregister: AG Düsseldorf, HRB 53837
> UST.-ID: DE248275827
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Date Field Contents Not Visible

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
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 <TH...@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 <TH...@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-unsubscribe@pdfbox.apache.org
> > > > > For additional commands, e-mail: users-help@pdfbox.apache.org
> > > > > 
> > > > > 
> > > 
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> > > For additional commands, e-mail: users-help@pdfbox.apache.org
> > > 
> > > 
-- 
Maruan Sahyoun

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

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

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


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


Re: Date Field Contents Not Visible

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
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 <TH...@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 <TH...@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-unsubscribe@pdfbox.apache.org
> > > > For additional commands, e-mail: users-help@pdfbox.apache.org
> > > > 
> > > > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> > For additional commands, e-mail: users-help@pdfbox.apache.org
> > 
> > 
-- 
Maruan Sahyoun

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

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

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


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


Re: Date Field Contents Not Visible

Posted by Robert Pepersack <ro...@itgfirm.com>.
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 <TH...@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 <TH...@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-unsubscribe@pdfbox.apache.org
> >> For additional commands, e-mail: users-help@pdfbox.apache.org
> >>
> >>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Date Field Contents Not Visible

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 24.12.2019 um 22:27 schrieb Robert Pepersack:
> The file from Foxit causes an exception to be thrown.

What exception? Please include the stack trace. Also make sure to update 
to 2.0.18.

Tilman


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


Re: Date Field Contents Not Visible

Posted by Robert Pepersack <ro...@itgfirm.com>.
Scratch that.  I realized that I was still using the file from Acrobat, not
Foxit.  The file from Foxit causes an exception to be thrown.

On Tue, Dec 24, 2019 at 4:13 PM Robert Pepersack <
robert.pepersack@itgfirm.com> wrote:

> I created a new PDF file with Foxit PhantomPDF that's the same as the one
> I created with Adobe Acrobat DC.  Without the work-around you gave me, the
> PDF I created with Foxit PhantomPDF has the same problem as the one I
> created with Acrobat DC.  The date field is empty until I click on it.
> When I move the focus away, the date field content disappears.  With the
> work-around the PDF I created with Foxit PhantomPDF works correctly.
>
> So, it appears that it's not a defect in the document that I created with
> Adobe Acrobat DC.
>
> On Tue, Dec 24, 2019 at 3:35 PM Maruan Sahyoun <sa...@fileaffairs.de>
> wrote:
>
>> I think the reasoning is that setting a different string will not ensure
>> the proper format as has been intentef by the forms designer
>>
>> > Am 24.12.2019 um 20:18 schrieb Tilman Hausherr <TH...@t-online.de>:
>> >
>> > Hi,
>> >
>> > In the meantime I found the file FreedomExpressions.pdf and amusingly,
>> the JS content is the same:
>> >
>> > AFDate_FormatEx("mm/dd/yyyy");
>> >
>> > I agree that we should create the appearance stream. But I still wonder
>> why the decision was made a long time ago not to.
>> >
>> > Tilman
>> >
>> >> Am 24.12.2019 um 18:07 schrieb Maruan Sahyoun:
>> >> We should be able to remove the test for a JavaScript action and
>> generate the appearance regardless. If there is a JavaScript
>> >> action that should take the value and recalculate the entry. What
>> happens here (I think as I don't have the form) is that the
>> >> JavaScript is attached to an interactive event which is not triggered
>> which results in an empty field.
>> >>
>> >> OTOH this is also a defect of the form itself as the scripting assumes
>> interactive use.
>> >>
>> >> If we remove the check we will take the provided string as is and
>> won't execute the JavaScript (as we can't) which might lead to
>> >> a visual representation which is not intented e.g. the JavaScript
>> typically does date formatting in a way that differs from the
>> >> string being passed.
>> >>
>> >> BTW that's the reasoning why we don't set the appearance if there is a
>> JavaScript action attached.
>> >>
>> >> WDYT?
>> >>
>> >> BR
>> >> Maruan
>> >>
>> >>
>> >>
>> >>> 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 <
>> THausherr@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-unsubscribe@pdfbox.apache.org
>> >>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>> >>>>>
>> >>>>>
>> >>> ---------------------------------------------------------------------
>> >>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> >>> For additional commands, e-mail: users-help@pdfbox.apache.org
>> >>>
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> > For additional commands, e-mail: users-help@pdfbox.apache.org
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>>

Re: Date Field Contents Not Visible

Posted by Robert Pepersack <ro...@itgfirm.com>.
I created a new PDF file with Foxit PhantomPDF that's the same as the one I
created with Adobe Acrobat DC.  Without the work-around you gave me, the
PDF I created with Foxit PhantomPDF has the same problem as the one I
created with Acrobat DC.  The date field is empty until I click on it.
When I move the focus away, the date field content disappears.  With the
work-around the PDF I created with Foxit PhantomPDF works correctly.

So, it appears that it's not a defect in the document that I created with
Adobe Acrobat DC.

On Tue, Dec 24, 2019 at 3:35 PM Maruan Sahyoun <sa...@fileaffairs.de>
wrote:

> I think the reasoning is that setting a different string will not ensure
> the proper format as has been intentef by the forms designer
>
> > Am 24.12.2019 um 20:18 schrieb Tilman Hausherr <TH...@t-online.de>:
> >
> > Hi,
> >
> > In the meantime I found the file FreedomExpressions.pdf and amusingly,
> the JS content is the same:
> >
> > AFDate_FormatEx("mm/dd/yyyy");
> >
> > I agree that we should create the appearance stream. But I still wonder
> why the decision was made a long time ago not to.
> >
> > Tilman
> >
> >> Am 24.12.2019 um 18:07 schrieb Maruan Sahyoun:
> >> We should be able to remove the test for a JavaScript action and
> generate the appearance regardless. If there is a JavaScript
> >> action that should take the value and recalculate the entry. What
> happens here (I think as I don't have the form) is that the
> >> JavaScript is attached to an interactive event which is not triggered
> which results in an empty field.
> >>
> >> OTOH this is also a defect of the form itself as the scripting assumes
> interactive use.
> >>
> >> If we remove the check we will take the provided string as is and won't
> execute the JavaScript (as we can't) which might lead to
> >> a visual representation which is not intented e.g. the JavaScript
> typically does date formatting in a way that differs from the
> >> string being passed.
> >>
> >> BTW that's the reasoning why we don't set the appearance if there is a
> JavaScript action attached.
> >>
> >> WDYT?
> >>
> >> BR
> >> Maruan
> >>
> >>
> >>
> >>> 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 <
> THausherr@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-unsubscribe@pdfbox.apache.org
> >>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
> >>>>>
> >>>>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> >>> For additional commands, e-mail: users-help@pdfbox.apache.org
> >>>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> > For additional commands, e-mail: users-help@pdfbox.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Date Field Contents Not Visible

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
I think the reasoning is that setting a different string will not ensure the proper format as has been intentef by the forms designer 

> Am 24.12.2019 um 20:18 schrieb Tilman Hausherr <TH...@t-online.de>:
> 
> Hi,
> 
> In the meantime I found the file FreedomExpressions.pdf and amusingly, the JS content is the same:
> 
> AFDate_FormatEx("mm/dd/yyyy");
> 
> I agree that we should create the appearance stream. But I still wonder why the decision was made a long time ago not to.
> 
> Tilman
> 
>> Am 24.12.2019 um 18:07 schrieb Maruan Sahyoun:
>> We should be able to remove the test for a JavaScript action and generate the appearance regardless. If there is a JavaScript
>> action that should take the value and recalculate the entry. What happens here (I think as I don't have the form) is that the
>> JavaScript is attached to an interactive event which is not triggered which results in an empty field.
>> 
>> OTOH this is also a defect of the form itself as the scripting assumes interactive use.
>> 
>> If we remove the check we will take the provided string as is and won't execute the JavaScript (as we can't) which might lead to
>> a visual representation which is not intented e.g. the JavaScript typically does date formatting in a way that differs from the
>> string being passed.
>> 
>> BTW that's the reasoning why we don't set the appearance if there is a JavaScript action attached.
>> 
>> WDYT?
>> 
>> BR
>> Maruan
>> 
>>  
>>  
>>> 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 <TH...@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-unsubscribe@pdfbox.apache.org
>>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>>> 
>>>>> 
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


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


Re: Date Field Contents Not Visible

Posted by Tilman Hausherr <TH...@t-online.de>.
Hi,

In the meantime I found the file FreedomExpressions.pdf and amusingly, 
the JS content is the same:

AFDate_FormatEx("mm/dd/yyyy");

I agree that we should create the appearance stream. But I still wonder 
why the decision was made a long time ago not to.

Tilman

Am 24.12.2019 um 18:07 schrieb Maruan Sahyoun:
> We should be able to remove the test for a JavaScript action and generate the appearance regardless. If there is a JavaScript
> action that should take the value and recalculate the entry. What happens here (I think as I don't have the form) is that the
> JavaScript is attached to an interactive event which is not triggered which results in an empty field.
>
> OTOH this is also a defect of the form itself as the scripting assumes interactive use.
>
> If we remove the check we will take the provided string as is and won't execute the JavaScript (as we can't) which might lead to
> a visual representation which is not intented e.g. the JavaScript typically does date formatting in a way that differs from the
> string being passed.
>
> BTW that's the reasoning why we don't set the appearance if there is a JavaScript action attached.
>
> WDYT?
>
> BR
> Maruan
>
>   
>
>   
>> 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 <TH...@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-unsubscribe@pdfbox.apache.org
>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>>
>>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>


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


Re: Date Field Contents Not Visible

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
We should be able to remove the test for a JavaScript action and generate the appearance regardless. If there is a JavaScript
action that should take the value and recalculate the entry. What happens here (I think as I don't have the form) is that the
JavaScript is attached to an interactive event which is not triggered which results in an empty field.

OTOH this is also a defect of the form itself as the scripting assumes interactive use.

If we remove the check we will take the provided string as is and won't execute the JavaScript (as we can't) which might lead to
a visual representation which is not intented e.g. the JavaScript typically does date formatting in a way that differs from the
string being passed.

BTW that's the reasoning why we don't set the appearance if there is a JavaScript action attached.

WDYT?

BR
Maruan

 

 
> 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 <TH...@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-unsubscribe@pdfbox.apache.org
> > > For additional commands, e-mail: users-help@pdfbox.apache.org
> > > 
> > > 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 
-- 
Maruan Sahyoun

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

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

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


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


Re: Date Field Contents Not Visible

Posted by Tilman Hausherr <TH...@t-online.de>.
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 <TH...@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-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>>


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


Re: Date Field Contents Not Visible

Posted by Robert Pepersack <ro...@itgfirm.com>.
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 <TH...@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-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Date Field Contents Not Visible

Posted by Tilman Hausherr <TH...@t-online.de>.
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-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Date Field Contents Not Visible

Posted by Tilman Hausherr <TH...@t-online.de>.
Please upload the PDF to a sharehoster, most attachments are blocked.
Tilman

Am 23.12.2019 um 22:09 schrieb Robert Pepersack:
> Hi,
> I have used PDFBox to create a .pdf file from a .pdf file that I 
> created in Adobe Acrobat Pro DC version 2019.021.20061.
>
> Here is the process I've been working on:
> 1.  Create "template" .pdf file in Adobe Acrobat Pro DC
> 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.
> 3.  Open the new .pdf file in Adobe Acrobat Reader.
>
> The .pdf file that I created has a date field that doesn't display the 
> date after I run PDFBox to set all of the fields in the file.  The 
> other fields, including a text field, a list box, a combo box, a check 
> box, and a radio button group all get populated, and display correctly 
> in Adobe Acrobat Reader DC version 2019.021.20061 (same version as 
> Acrobat Pro).
>
> When I click in the date field, the date appears.  But, when I move 
> the focus away from the date field, the date disappears.
>
> So, it tried setting the AcroForm's "NeedAppearances" property to 
> true, like this:  acroForm.setNeedAppearances(true).  That makes the 
> date in the date field display when I open the new .pdf file, but it 
> has an unwanted side effect.  When I close the document, without 
> making any changes, I get a message box that asks me if I want to save 
> the changes before closing.
>
> Does anyone have any insight?  I have attached the output file, which 
> doesn't have "NeedAppearances" set to true (test.pdf).  I have also 
> attached my "template" file (Acrobat_DC.pdf).
>
> Thanks!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org