Different position of text by flatten pdf with iText - java

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.

Related

How to dynamically resize text fields in Adobe Acrobat using Apache PDFBox?

I'm using Apache PDFBox to populate an existing Adobe Acrobat PDF template.
In this template I have fixed sizes of text fields in which I insert data from database.
In one text field the amount of data may vary so it would be great if text field could get bigger or smaller to adjust to the amount of text inserted.
The main problem is that I have another text fields below the one I want to resize dynamically so in case of making it bigger or smaller other text fields have to move relatively
I was searching for the solution but found only offers to resize font dynamically, but it doesn't satisfy my needs. I could create one text field and insert all of my data in it, but some of those text fields must have different color of text so it's important to have different text fields to be able to specify their color in code (as far as I know).
Is that even possible and if not what can I do to achieve the result I'm looking for?
P.S. Our code is based on templates from Adobe Acrobat and Apache PDFBox, so it's more preferable to keep it that way. But if this is not possible with those two, I will be glad to hear your recommendations on what should I use (open source first).
PDF Producer: Adobe Acrobat Pro DC (32-bit) 21.7.20102,
PDF Version: 1.6 (Acrobat 7.x),
Apache PDFBox Version: 2.0.26
You can find an example of my PDF below

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

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.

How to insert content in the middle of a page in a PDF using IText

I have a requirement to insert Content into the middle of the page in a PDF.
The Content may be a Dynamic Table or an Image.
My Concept was to first split the PDF into 2 parts, then get the new Content that is to be added and append by replacing a place holder field.
the Splitting is called Tiling as per IText and here is an example for the same.
http://itextpdf.com/examples/iia.php?id=116
The Code above has 2 drawbacks:
1. It splits the page into 16 parts. but that is part of the example. Still i cant figure out a way to split the file into 2 parts only.
2. secondly the split page is converted to a complete page thus disturbing its proportions.
The Rearranging code is the another problem.
The remaining Content should be re-ordered in append mode. but till yet i have only found codes to add complete new pages rather than just the content.
I have found a code that appends the PDF content by replacing a placeholder:
float[] fieldPosition= pdfTemplate.getAcroFields().getFieldPositions("tableField");
PdfPTable table = buildTable();
PdfContentByte cb = stamper.getOverContent(1);
table.writeSelectedRows(0, -1, fieldPosition[1],fieldPosition[4],cb);
Please help me to solve this requirement.
PDF is a presentation format, not an edition format. In other words, it is not designed to allow content insertion, with the original content reflowing gracefully. As a consequence, no tool (at least, none that I know of, and surely not iText) will enable you to achieve what you were given as a requirement.
My advice :
refuse the assignment since it's not feasible, or
get your hands on the original document, insert the desired extra content, and then convert to PDF.

Categories