PDF forms in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a set of forms designed using Word. I need these forms to be filled and saved as PDF. All of that programatically in Java.
I explored the following possibilities:
Word empty -> PDF AcroForm -> Fill with PDFBox
Word filled programatically -> Converted to PDF
Word empty -> XHTML -> Filled and saved as PDF using Flying Saucer
Word empty -> Converted in special XML for Apache FOP
Until now I'm encountering the following difficulties with the first possibility:
Very difficult to create dynamic table
PDFBox not able to manage RTL text correctly
Any good experience with these solutions?

I did a similar implementation for one of my customers few years back. I used open office to create the template and generated PDF using iText. See if it helps.
Steps:
Creating the template:
Open a new document in open office and create a new document. Go to “view” and make the “Form Design” mode
Enter the text which will represent the static portion of the template.
Once the “Form Design” mode is turned on, the corresponding toolbar will be added as highlighted in the screenshot above. Now click the “Text Box” and insert a text box in the document. Double click the text box and insert the required inputs there.
Once the template document is created, save and the export the document as PDF and this document will be used as the template for the PDF creation
Fill up the template using iText:
The next step is to read the template and populate the template to generate PDF dynamically.
iText provides a PdfReader to read PDF files and PdfStamper to modify or add content to a PDF document. Since the template is a PDF file, the PdfReader will read the template and PdfStamper will populate the fields dynamically with real time data. Following are the steps
1.Read the PDF template using the iText PdfReader. The template can be put anywhere in the file system. But in a real life project scenario, it should be placed inside the project folder.
Create a PdfStamper with the PdfReader and a ByteArrayOutputStream as parameter.
Populate the template fields using the instance of the PdfStamper object. The setField () method takes the key and the value as argument. The “key” should be the “Name” of the text box that was given during the template creation. Please make sure that the “key” is same as the “Name” field. Otherwise the field will not be populated.
After populating the required fields, close the PdfStamper with the PdfReader objects.
Reference : http://itext-pdfcreation-template.blogspot.in/

Related

iText and AcroFields styling fonts doubles the fields text

i'm facing a problem usintg iText together with java in order to populate form fields.
i've created the form field in a PDF Document using Acrobat 11 Pro and i've been able to populate it with all the values i needed using iText and AcroFields.
Troubles startet to raise when i modified fonts style, size and alignment using Acrobat 11. Now, for each input i modified the style, when i'm using the setField method i get twice the content one that has the aspect i've set in Acrobat 11 and the other that has the default style
Has anyone already tackled that issue?

Retaining usage rights in the pdfs

I want to populate interactive pdf form with the xml using itext:
//build the xmlString, convert it to InputStream
AcroFields fields = pdfStamper.getAcrofields();
XfaForm form = fields.getXfa();
form.fillXfaForm(xmlStream);
The fields getting populated, however the resulting *.pdf is now not editable in the Adobe Reader. That is the document property has "Filling of form fields: Now Allowed" now. So, I was digging around for a solution and read somewhere (I believe the itext in action book), that the itext does, indeed, break the usage rights. After, I tried the solutions described here:
http://itextpdf.com/examples/iia.php?id=166
That is, remove usage rights completely or create stamper in the append mode, e.g.,
reader.removeUsageRights();
or
PdfStamper stamper = PdfStamper(reader, new FileOutputStream(dest), '\0', true)
The pdf is now editable. However, the PDF form I am populating has a drop-down menu - if a particular option in that drop-down is selected, the pdf form activates one of its own text field. This does not work anymore - the drop-down is selected, but the text field it supposed to activate remains inactivate and blank.
I've read that the usage rights are part of the pdf own encryption, I was wondering if it is possible to get that encryption and set it into into a populated copy of the pdf form?
Thank you

Different position of text by flatten pdf with iText

I have a problem with iText and flatten form fields in pdfs.
I submit a pdf with form fields created in Acrobat to my java method. On a website i have created a form to fill the form fields in the pdf. The form fields are filled correctly, but as soon as i flatten the document the text is moved to a little different position. The biggest difference is seen in multiline form fields. There the text is at the border of the field in the upper left. In Acrobat and before flattening the text has a padding to the top.
Here is my java code to call the methods of iText:
PdfReader template = new PdfReader(templ);
XfdfReader xfdfReader = new XfdfReader(xfdf);
OutputStream outputStream = new FileOutputStream(output);
PdfStamper stamper = new PdfStamper(template, outputStream, '\0');
AcroFields form = stamper.getAcroFields();
Set<String> fields = form.getFields().keySet();
form.setFields(xfdfReader);
stamper.setFreeTextFlattening(true);
stamper.setFormFlattening(true);
stamper.close();
template.close();
Anyone has an idea why the text is moving when i flatten the pdf? How can I avoid this?
I allready tried different versions from iText. From version 4.X to 5.X. The difference appears in all versions.
Although i tried to move the form fields in the code of iText, but then the whole field is moving and the difference of the position is much bigger and not predictable.
In my project the text must be at the exact same position as in Acrobat, so i must find a workaround for this misbehavior. I hope somebody can help me.
Thanks for your help in advance.
The position of the baseline of a field in a PDF file has been changed over the years. You'll even see differences depending on the version of Acrobat you are using.
There is no solution for your problem unless you know the exact offset. If you do, you can use the setExtraMargin() method to change the offset of all fields when flattening the document.
We created this method to deal with specific forms that have a baseline that is different from what is to be expected. Which values you choose can be different for different forms.

PDF Renderer with PrintDialog

Is this possible?
I am getting the directory and filename of a existing PDF document.
C:\temp\FileName.pdf
Use PDF Renderer to either view the document or print the document
I have PDF Renderer included in my Eclipse Project, just not sure how to use it
Then use PrintDialog to print out the PDF Document.
I think that PDF Renderer has to do something to make the PDF document printable
I want have 2 buttons on my form
View PDF -> opens the pdf document in read only
Print PDF -> gets the pdf document ready to print then calls PrintDialog to select a local printer and then prints
I am not having much luck finding any PDFRenderer sample code that shows how to open the PDF document in view mode.
I haven't seen code on how to send the PDF Renderer result to a PrintDialog to print.
You certainly can implement PDF rendering in your application. The question is, if it is really necessary or not. Since you dindn't specify which library you want to include, I can't help you with the PDFRenderer.
However, for purpose of launching an external PDF viewer and how to implement a print button, those API classes are useful:
org.eclipse.swt.program.Program
org.eclipse.swt.printing.PrintDialog
At least it'll help to (partially) solve your problem.

Editing a PDF file with Java

I'm trying to edit a existing PDF file. It is a file where I need to fill in some addresses and other stuff. I want to connect an addressbook to the application so the user can select a user from the addressbook and a part get filled in automatic.
My questions are:
Is it possible to edit a existing PDF file and fill in some fields (+/- 20), because I know there is Itext (http://www.itextpdf.com) but I read that the possiblities are very small.
Or can I better convert the PDF to JPG and get it as background. And create JLabels on the places where I need to fill in the fields. And then print the whole Frame on a A4.
Or are there better posibilities?
So what I need to do, step by step:
Select one of the PDF's (they are in the program)
Fill some fields with content/addresses
Print the PDF/Form with a printer
There is a toolkit given by Adobe named as Acrobat Forms Data Format (FDF) Toolkit which gives API for different languages to fill forms.
You can get the java code at the bottom of that page or check this link
We didn't edit existing PDF's but created totally new letters/reports/doco from our java app using iReport
You could use pdf form and edit field values programmatically using IText or Apache PDFBox (download pdfbox and see SetField.java example)

Categories