• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

PDFBox save issue

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My equipment management software currently reads and writes equipment information to a DA3161.xfdl form.
With the XFDL version being replaced in the near future with the DA3161.pdf version I have been attempting to
create a new da3161 manager to allow me the ability to automatically fill in the pdf version of this form.

I have run into an issue with saving the changed pdf form. No field values I change are being applied to the newly saved form.

I hope this just an issue with me missing some step in the process and not an issue with these digitally signable forms.
Any help would be greatly appreciated.


 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The only line where anything is changed is line 105 - have you ensured (via logging) that that is actually executed?
 
Mark Strein
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Welcome to the Ranch.
The only line where anything is changed is line 105 - have you ensured (via logging) that that is actually executed?



Thanks for the welcome.

For this test code, I just need to change one field to verify I can change it and save the change.
There will be another class that inherits from this class that actually does the processing of the form.

The field(form1[0].Page1[0].SEND_TO[0]) in my test da3161.pdf has been pre filled with "wwww".

The output at line 107, at least on the surface, seems to make me believe I changed the field value... at least in memory.

I am not familiar with "logging", if you would be so kind, could you expand on that a bit?

Mark....

 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The output at line 107, at least on the surface, seems to make me believe I changed the field value... at least in memory.


But line 105 is not executed every time line 107 is executed, due to the if statement around the former.

I am not familiar with "logging", if you would be so kind, could you expand on that a bit?


By logging I mean printing out information, like you're already doing with the System.err.println statements. Put another one of those right after line 105, so that you know it is being executed whenever you think it should be executed.
 
Mark Strein
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But line 105 is not executed every time line 107 is executed, due to the if statement around the former.



And in this case, line 105 should only be executed once, there is only one field in this pdf that is named form1[0].Page1[0].SEND_TO[0].
In the output on line 107, it does show that the value of the form1[0].Page1[0].SEND_TO[0] field is "Your Name Here".
If I print the value of field form1[0].Page1[0].SEND_TO[0] prior to line 105, its value is "wwww".

Mark....
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what is going on, but the entire logic seems rather roundabout, what with looping through all fields. It seems you could replace lines 61 to 65 (and thus the entire processField method) with a few lines of code as shown in https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java, starting in line 49.
 
Mark Strein
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Not sure what is going on, but the entire logic seems rather roundabout, what with looping through all fields. It seems you could replace lines 61 to 65 (and thus the entire processField method) with a few lines of code as shown in https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/fdf/SetField.java, starting in line 49.



You are correct, line 49 in your example, will work in the "finished" code(this is test code for setting a value and saving to a new file).

The loop is just there to supply me with a list of all fields in the pdf file. If you don't have the filed names, line 49 in your example, doesn't do me any good at this point.

The field in memory does change. But when the save occurs, the pdDocument still retains the old values, thus my assumption that I am missing a step in the process of saving to a new file.

Mark....

 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I understand. You do have the name - it's "form1[0].Page1[0].SEND_TO[0]".
 
Mark Strein
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I don't think I understand. You do have the name - it's "form1[0].Page1[0].SEND_TO[0]".



I know I have it now, but I didn't before I put the loop in.. I need the field names, but I am not ready to create the process class that needs these field names yet... since I can't get the save to work.

This is not final code, it is just a test to change the value of one field, and then once proven that it did change, to save this change to a new pdf document.

I can change the value of that field, but when saved to a new file.... the new file contains the old value.

I added a print statement before the setValue(line 105):
---> | | |--form1[0].Page1[0].SEND_TO[0] form1[0].Page1[0].SEND_TO[0] = wwww, type=org.apache.pdfbox.pdmodel.interactive.form.PDTextbox

This is after the setValue:
| | |--form1[0].Page1[0].SEND_TO[0] form1[0].Page1[0].SEND_TO[0] = Your Name Here, type=org.apache.pdfbox.pdmodel.interactive.form.PDTextbox

When I open the new file that is saved.... the "send to" field contains "wwww".

So... back to my first assumption, what step am I missing?
Is there something special about digitally signable forms?

Mark....
 
Mark Strein
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Different is good....

I created a List and in the processField method when the kid is null, I add that field to the list.

Once it is done with the loop, just before the save I execute pdAcroForm.setFields(newList);

The save happens and the "send to" field is now blank! Not what I want, but different from no change!

Any Ideas why it may be blank now?
 
Mark Strein
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason the field is now blank is due to a bug in apache/pdfbox/pdmodel/interactive/form/PDAppearance.java calculateFontSize method.

Apparently it defaults the font size to zero and then miscalculates the position of text in the form field, rendering it invisiable unless you click in the form field.
See here for patch:

Issue with above patch:
I am currently using pdfbox-1.8.4.jar and version 1.5 of BouncyCastle(source code).
When I add the pdfbox-1.8.4 source code to my project(and remove the reference to the jar) I get missing BounceyCastle imports in pdfbox-1.8.4 source code.

So... any ideas which version of BouncyCastle should be used with pdfbox-1.8.4 source code?

Mark....

 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.44 of bcmail-jdk15 and bcprov-jdk15. You can find release drops of the PDFBox sources at https://github.com/apache/pdfbox/releases. Maven's pom.xml files will tell you all the dependencies.
 
Mark Strein
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, Ulf thank for your assistants.

Second, would like to pass on my solution to the issues address in these posts.
Hope it is helpful to others.

A) When fields changed and document saved, no changes appear in documnet.
My Solution:
Create a list(line 24) to store PDFields
Add the PDFields I am interested in to this list(line 119)
Replace the acroForm field list with my list(line 89)



B) Field being changed was/is blank.
Bug/problem/incomplete code in PDAppearance.java calculateFontSize method.
daTokens is null(line 16) which causes the fontSize to never be set.

I could of fixed the incomplete code between line 29 and 35 in two ways, hard code a fontSize(not ideal) or calculate a fontSize(left that for another day).
My Solution:
Since tokens was not null I used it to get the fontSize like daTokens is supposed to do. see lines 7-14



Mark....
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reporting back with the solution. Having to track down a bug in an underlying library is certainly frustrating. If you're feeling like contributing to the PDFBox project, you could create a bug ticket in the PDFBox JIRA and post your patch there. I'm certain that would be appreciated
 
I just had the craziest dream. This tiny ad was in it.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!