How to fill tables in PDF forms with iTextSharp? - pdf

all
I must fill this PDF form using iTextSharp.
I have no problems with the header fields, but I don't know how to fill the table in the bottom. I can fill its first row with PdfStamper.AcroFields.SetField(), but how can I add more rows, if it is at all possible?

Your form is a form based on AcroForm technology. This means that the form is static. Every field in the form corresponds with a widget annotation for which an absolute position is defined on the page. You can not add any data on coordinates that are not predefined, hence you can not "add more rows". What you are asking for is impossible.
Take a look under the hood of your PDF:
There's an array of /Fields and each field is defined by a dictionary that combines field entries and the entries of a single widget annotation. Each widget annotation has its fixed coordinates on the page.
It seems that you were looking for a dynamic form solution. In that case, you need a form based on the XML Forms Architecture (XFA). Your form is not an XFA form.
If you are in doubt about the difference between AcroForm and XFA technology, please download this free ebook: The Best iText Questions on StackOverflow. In this book you'll find the answer to several questions such as:
What is the difference between iText, JasperReports and Adobe LC?
Generate and design PDF with iTextSharp or similar
How can I flatten a XFA PDF Form using iTextSharp?
...
These and other answers will give you an idea of some alternative options.

Related

Is there an possibility to save a dynamic pdf as static but still have interactive fields within the document?

I have a dynamic pdf which I want to use for DocuSign and thus needs to be static. I cannot simply make use of the print as pdf function as I still want to use the interactive fields within the pdf.
I Tried to use Adobe AEM Forms Designer to save the document as static. But solely the first page of the form is saved.
Regarding your concern, I would like to share the following information, you can learn about PDF form field transformation. It enables you to transform PDF form fields automatically into DocuSign tabs, carrying over all of their existing values. The locations of the created tabs will match the locations of the fields from which they were generated.
To transform PDF form fields into DocuSign tabs, you need to set the transformPdfFields property on the documents whose fields you want to transform.
https://developers.docusign.com/docs/esign-rest-api/esign101/concepts/tabs/pdf-transform/
https://www.docusign.com/blog/developers/the-trenches-pdf-form-field-transformation
Best Regards,
Eric | DocuSign

Determine the Text that can Display in Multiline PDTextField

Is there a way to determine the text that will actually display in a PDTextField when the PDF prints? If I call setValue and then getValue, it returns all of the text even though it will not all display.
I am trying to fill out a form with a limited size multiline text field that has the notation to attach another page for more details. I would like to limit the text to that which will display and generate the added detail page.
Thanks for indulging a PDFbox newbie.
There is no direct way to find that out as the details of the text layout such as line breaks, padding, line spacing are hidden inside the non public class PlainTextFormatter inside the org.apache.pdfbox.pdmodel.interactive.formpackage. So you'd need to replicate that code.
PDFBox tries to resemble the calculations done by Adobe Acrobat and Adobe Reader but the details of such calculations are not part of the PDF specification. So doing your calculation is only valid for a similar layout model. Other form filling applications might have a slightly different layout model and as a result your results will not apply to these.
In addition to that Acrobat (and PDFBox) place text although it might be partially clipped. Look at the results of the AlignmentTest.javaunit test to see what I mean. So one might have a different expectation to what 'fitting' really means.
As I've thought about passing the information about which text fitted back to the calling application anyway I've opened an enhancement request https://issues.apache.org/jira/browse/PDFBOX-3413 for that.

Multipage form using itext with Acrofield and Dynamic table

I'm new at working with itext and I'm looking for help.
I'm creating a PDF document based on a PDF template which contains a form with acrofields. This form is composed by some fields on top page and a table and has only one page.
The PDF document generated will have one or more pages depending on table's size (if size content extends predefined size, table should continue in the second page). Also every page should display fields on the top with the same information.
I've tried to solve it following "AddExtraTable" example, but I didn't get to keep the fields on top in all pages, only in the first one.
I've tried also to solve it with PdfCopy. This approach displays the fields on top in all pages but table content is not properly displayed.
Can someone help me?
PS: I have to use itext 2.1.7 because of project requirements.

PDFtk and number formatted PDF form field

I'm using pdftk to fill in the form with the generated fdf.
In the PDF form, the form field is configured as a number field with 2 decimal point, and the negative value will be showing with parentheses, for example, -4444 will become (4444.00)
Using any PDF viewers and changing the value on form did make the form display the value correctly with the behaviour explained above (negative value will become value with parentheses)
Tested also with the FDF (by importing to the form), the negative value will be displayed correctly as well.
But when using the pdftk fill-form action, the negative value remains as it is without changing the display, which is still showing -4444 and not (4444.00)
Is anyone experienced this before / has a solution for this?
Update #1
I've also tested Apache PDFBox, it has the same issue :(
And now I'm trying to achieve this by using the PDF's javascript, any clue that this way will works?
Update #2
came across this thread How to refresh Formatting on Non-Calculated field and refresh Calculated fields in Fillable PDF form and so gave it a try with iText as well. However still unable to make it works
Finally, i've found some ways to get the formatting works in the PDF form fields.
Approach #1
Requirements
pdftk
PDF form with javascript
In the PDF, create a "Document Javascript" (see how) and re-assign the form's value to make it dirty as mentioned by Denis in the thread of Update #2. The script could be as simple as below:
var text1 = this.getField('text1');
text1.value = text1.value;
Downside
Javascript will only be triggered when you open the PDF, and if you would like to set the PDF with some ownerPassword to prevent user edit the file, the javascript will just failed because of read-only form fields. Otherwise, imagine you have 100 form fields in the PDF, re-assign each of them is a nightmare.
Approach #2
iText
PDF form
I personally will prefer this approach. The powerful iText already has the API to set the form field and at the same time formatting the field display (see how). The generated PDF is ready to print as well with the correct format.
Downside
Either using the iText API to find out the existing format of the form field, or you will hard code the format in your codes. Require more efforts than using the pdftk.
I'm using a simpler version of ChinKang's #1.
In Tools -> Document Javascripts:
this.calculateNow();
One thing to check is in Tools -> Set field calculation order that any calculations are ordered appropriately.
I then use https://github.com/ccnmtl/fdfgen and pdftk to fill the forms in.
The only gotcha is that you cannot flatten the PDF

Modify character spacing in a PDF form field

I'm trying to build a web app to programmatically fill out a PDF form. I am going to configure my form first in Adobe Acrobat, then write a Java app with iText to fill out all the form fields via user input from the web. The base form I need to fill out comes from the US government. They created form fields with extremely large kerning (character spacing) values I need to change. However, there appears to be no way to modify this value in the Acrobat UI.
Does anyone know how to manipulate character spacing on form fields in Acrobat 8.0 for Windows? I could try to use iText to programmatically manipulate the kerning of the original document, but this would be much more tedious.
I believe I figured this out: kerning is called "combing" in acrobat, and each of the form fields have been "combed". The strange thing is this option isn't checked when I view the properties of the form field, but "combing" is the behaviour I was attempting to replicate.