Retaining usage rights in the pdfs - java

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

Related

PDF forms in Java [closed]

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/

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.

adding text to a each page of a pdf using Itext

how to add a paragraph to each page using itextwithout using ColumnText.showTextAligned because the text added to each page is different and can span multiple lines.
is it possible to add text using PdfStamper ? Or should i use PdfWriter . i prefer using PdfStamper to get this done because PdfStamper was already used to add annotations and this functionality can be integrated easily.
You can add paragraphs straight into the document object
(http://tutorials.jenkov.com/java-itext/paragraph.html)
Or I have personally used a PdfTable with 100% width. It easily lines up all the paragraph objects (http://itextpdf.com/examples/iia.php?id=76)
Both of those methods support either formatted text or a plain string which will instead use defaults.
You can do it with a stamper, but it requires the ColumnText.showTextAligned method.

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.

How to add a hidden image to a PDF document?

I have a program. It outputs to pdf, but that is close to impossible to read from again. So i need a additional file attached to my PDF in order to be able to make it editable in my program. Attaching a file to PDF is a good idea, but that is visible to the user, which i don't wan't it to be.
An alternative is to hide my readable file format inside an image which would be added to the PDF somewhere to the top of the first page, before everything else... Even to metadata if that's possible...
That way I can extract image from pdf using a PDF library (iText), and read from it.
My question is how to add image to PDF to be as well hidden as it could be (visually and by accesibility). And it has to be in a place which would be same for any created document (somewhere on the top, or on the very bottom of the document, or to the part of the document which isn't displayed at all... I'm really guessing here, I'm not really familliar with PDF file format)...
Any ideas?
P.S. It's not really important which image is it, I could be a e.g. completly transparent image, 1x1 pixels.
I'm not sure what you mean by Image, but you can "extend" the PDF reference.
A PDF consists of objects: PDF numbers, PDF names, PDF strings, PDF arrays, PDF dictionaries, PDF streams. What you probably want, is to add an entry to a dictionary (pick one: the root dictionary, the info dictionary, the root of the page tree,...) that isn't defined in the PDF reference, so that it isn't rendered in a PDF viewer.
The key of such an entry must be a PDF name. To avoid clashes with existing names (names that are part of a current PDF spec, or will be part of a future spec), it is advised to register a four-letter key with ISO. For instance, Adobe registered adbe, iText registered ITXT and use that name with an underscore. For instance, ITXT_OriginalData would be a good name if we needed the functionality you describe.
The value of such an entry will be a PDF stream. In iText, you need the PdfStream class for this.

Categories