Automatic calcul fields don't work with iText - pdf

I have an interactive pdf form that contains totals field, those fields fulfilling automatically when other fields are filled by users, and it works very well manually. but when I fill it programmatically using iText with java, the normal one fills very well but those with the automatic calculation does not work and shows zero.
static String book = "myForm.pdf";
static String bookstamp = "myFormTest.pdf";
PdfReader reader = new PdfReader (book);
PdfStamper stamper = new PdfStamper (reader, new FileOutputStream (String.format (bookstamp)));
stamper.getAcroFields ();
stamper.setField ("ca50", "15000");
stamper.getAcroFields () ;
stamper.setField ("ca30", "150");
stamper.close ();

Automatic calculation of field values in PDFs happens by means of Javascript in the PDF. In case of certain events (e.g. value of a field changed or focus of a field lost) this Javascript is executed and sets the field value to the newly calculated one.
iText, on the other hand, does not run any Javascript. In iText only those parts of the PDF are changed which you explicitly change, side effects are not desired.
Thus, to show appropriately re-calculated fields upon opening your PDF after editing it with iText, you should also trigger the re-calculation of the fields upon opening the PDF.
If you provide the PDF file itself, you can be shown how to do that.
PS: You can trigger re-calculation when the document is opened by adding
stamper.addJavaScript("this.calculateNow();");
reader.removeUsageRights();
before
stamper.close ();
Unfortunately this will disable the Reader-Enabling of the document.

Related

Filling multiple fields in PDF form using PDFBOX and Lock editing of pdf document after filling

I am new to PDF BOX, need to fill the information in PDF Form, which has sections and field names:
PDF form contains information of
NAME
and box to fill the information
Address
and box to fill the information
City
box to fill the information.
if i have the Name, Address and city information how to fill that into the pdf form using pdf box.
Have used the example from PDFbox, acroForm only fills sample fields how to get this filled with particular information at particular field.
once after filling it how to make it locked.
String formTemplate = "C:/FillFormField.pdf";
PDDocument pdfDocument = PDDocument.load(new File(formTemplate))
// get the document catalog
PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
// as there might not be an AcroForm entry a null check is necessary
if (acroForm != null)
{
// Retrieve an individual field and set its value.
PDTextField field = (PDTextField) acroForm.getField( "sampleField" );
field.setValue("Text Entry");
// If a field is nested within the form tree a fully qualified name
// might be provided to access the field.
field = (PDTextField) acroForm.getField( "fieldsContainer.nestedSampleField" );
field.setValue("Text Entry");
}
// Save and close the filled out form.
pdfDocument.save("C:/FillFormField.pdf");
Currently dont have errors above code is working to fill out some junk fields.
need to fill exactly the Name with box info with Name information
Address with box info with Address information
I am able to do it with PDF Debugger from PDFBOX.
Used CMD prompt to get the structure of the file
java -jar pdfbox-app-1.8.10.jar PDFDebugger yourfile.pdf
please get the above jar file from apache PDF box.
once you able to see the structure, find the fields name.
For my purpose of locking, i dont need to use the encryption method. so used Acroform Flatten PDF method.
// flatten() method saves the PDF read only
acroForm.flatten();

Flattening an XFA PDF with iText --> New Pages, Barcodes Split

Setting
a) I have a reader extended XFA form (that contains 2 barcodes)
b) I have prepopulation XML
Intended Solution
- Prepopulate the XFA form with XML and then flatten the PDF (so that it is view-able in browsers, etc)
What's working
- Prepopulating of the XFA form is working and all fields & barcodes are populated as required (1 pager is generated) - looks perfect
The Problem / What's not working
- The second part of my code pickups the prepopulated PDF and then flattens the XFA form, the resultant pdf is now magically a 2 pager.
- The inbody barcode appears on the first page and all the other contents are incorrectly pushed to the second page.
- The positioning of all the elements are relatively correct, however split over two pages - they should all be on 1 page.
- The form & fields is flattened and views in a browser
For interest sake
I've 'flattened' the form using various forms of code, examples below
PDF FLATTENING CODE A
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new
FileOutputStream(dest));
stamper.setFormFlattening(true);
stamper.close();
PDF FLATTENING CODE B
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(dest)));
XFAFlattener xfaf = new XFAFlattener(document, writer);
xfaf.flatten(new PdfReader(fileToByteArray(src)));
document.close();
QUESTIONS
- Am I perhaps using the wrong 'flattening' code?
- Has any body experienced something similar?
- Why is this happening
- How can this be solved
Super appreciate any input here.
REF: SUP-1858
Just to keep the community updated
Solution (not quite ideal)
We removed a mysterious blank subform from the XFA and the flattened iText solution worked - 1 beautiful page!
My Interpretation:
How iText handles/interprets a XFA blank subform
VS
How AEM Forms (under the covers Livecycle Forms) handles/interprets a blank XFA subform
seems to be slightly different.
If anything else comes us with further findings, we'll (8 BIT PLATOON) update this page and let you know.
Thanks to the iText team assisting in the background.

Itext - reader.NumberOfPages returning - 1 - wrong page count

I am working on this pdf,
https://www.irs.gov/pub/irs-prior/f1095c--2015.pdf.
It's a 3 page - Acro pdf form
I open this pdf in Adobe Live Cycle designer, which suggests to make this document as flowable interactive form. As per my understanding it converts this acro form to XFA, then I save it as Dynamic (it also has a option of static), but I need to fill this form multiple times. Now, in my itextSharp project, when I try to get count of pages in pdf,
PdfReader reader = new PdfReader(pdfPath);
int formPageCount = reader.NumberOfPages; //its returns 1 (wrong)
reader.Close();
This returns count as 1(wrong). When I open PDF in normal Adobe pdf reader, in properties, it shows number of pages as 3(correct).
Any ideas on why itext is not returning a correct page count? Same code works for original copy of form and returns 3(correct).

ABC PDF version 6.0 Form Fields Read Only

We have a VB.NET program that is using Supergoo's ABCPDF version 6.1.1.2. Our program takes standard XML strings and places values in a corresponding PDF form field on the template PDF.
Problem:
We have over 3000 PDF files that have all been "tagged" with form fields. There could be up to 50 form fields on the template PDFs for a total of roughly 150,000 form fields in use potentially. We have noticed that some of these form fields have their form field common properties set to hidden by mistake. (see screenshot)
The issue is that the PDF coming back after the string values have been added are not showing up. Duh right? Fix the form field property and call it done. However, there is no way to know how many other of the other 150,000 form fields have been improperly tagged like this.
Does anyone know if I can adjust the PDF generation program to forcefully ignore that form field common property? Here is a sample of the vb.net code I am hoping to slightly alter...
Dim theDoc As Doc = New Doc
theDoc.Form.Fields("SampleFieldName").?????? 'can we set something here to ignore the hidden property?
According to the docs at
http://www.websupergoo.com/helppdfnet/source/6-abcpdf.objects/field/2-properties/page.htm
The .Page property of a Field object will tell you the page the field is on. Since Page is a class, if the result 'Is Nothing' then you know that the field is not visible since it doesn't appears on any page in the PDF document.
Please note that there are a few caveats when using fields that are not hidden but not actually visible when rendered (being too small, being spread on two pages, etc). If you need need to handle that you may be interested in http://www.websupergoo.com/helppdfnet/source/6-abcpdf.objects/field/2-properties/rect.htm depending on your use cases.
For the ABCPDF v6 software, I have discovered through Mihai's suggestion that it is possible. I have coded this C# example in the hopes that it helps someone down the road...
static void SetFillableFieldsToWriteableExample(string origFileLocation, string newFileLocation)
{
Doc theDoc = new Doc();
theDoc.Read(origFileLocation);
var theFields = theDoc.Form.GetFieldNames();
foreach (string theField in theFields)
{
Field theFieldInstance = theDoc.Form[theField];
theDoc.SetInfo(theFieldInstance.ID, "/F", "4");
}
theDoc.Save(newFileLocation);
}
I have tested this and it works when all fields are text fields on the PDF. Not sure on the other field types.
This code should not be used in a production environment as written here. There is no guarantee that the origFileLocation or newFileLocation references a PDF and no error handling among other issues. This is for demonstration purposes only.

Filling pdf fields with Chinese characters garbled

I am trying to fill a pdf field with Chinese characters from an fdf or xfdf.
So far I have tried, pdftk, mcpdf, pdfbox and fpdm.
They can all get the characters into the field, but they don't display. When I click on the field to edit, the characters show as expected, but when I click out of the field again they disappear. If I input English they are displayed incorrectly, eg "hello" becomes "IFMMP".
This has all lead me to suspect it's an issue with fonts/character maps, I have tried embedding the full font into the pdf and it made no difference. I have installed the fonts on the machine to no avail.
If I edit the pdf and fill the field in Acrobat it accepts the Chinese characters without a problem and I can view the pdf in a reader. I have tried using pdftk from the command line on the same Windows machine and I am having the same problem.
I need this to work in a Linux environment, and preferably in python or through a command-line script, but really at this point I'd just like to see it work at all! I have attached the sample pdf, fdf, xfdf and the output it is creating, any help would be greatly appreciated as I've run out of ideas. I have been using the command:
"pdftk test_form.pdf fill_form test.xfdf output output.pdf verbose"
https://drive.google.com/folderview?id=0B6ExNaWGFzvnfnJHSC1ZdXhSU2RQVENjYW56UkZyYWJMdWhZTkpQYkZBcUs0Tjhjb0NITVE&usp=sharing
When a form field is filled the fields value is populated and (optional) a visual appearance for the form field is generated reflecting the newly set value. So the reason that you are seeing the value when you click into the form field is that the fields value will be displayed but as long as the field is not activated the fields appearance is used.
If you tried setting the value with PDFBox 1.8 you might try using PDFBox 2.0 as this now has unicode support and the appearance generation is redone.
You also need to ensure that the font you are using in the form is available on the system you are filling your form with. Otherwise with PDFBox 2.0 you might get an error message similar to
Warning: Using fallback font 'TimesNewRomanPSMT' for 'MingLiU'
Exception in thread "main" java.lang.IllegalArgumentException: No glyph for U+5185 in font MingLiU
Which is as MingLiU is not available on the system it has been replaced by TimesNewRomanPSMT which doesn't have the character needed.
As another solution you can also direct the Adobe Reader to calculate the appearance for you when the form is opened using
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
form.setNeedAppearances(true);
again using PDFBox 2.0
I've created a little sample using PDFBox 2 but creating a form from scratch to test if it can handle the Chinese text
// create a new PDF document
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
// add a new AcroForm and add that to the document
PDAcroForm form = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(form);
// Add and set the resources and default appearance at the form level
PDFont font = PDType0Font.load(doc, new File("/Library/Fonts/Arial Unicode.ttf"));
PDResources res = new PDResources();
COSName fontName = res.add(font);
form.setDefaultResources(res);
String da = "/" + fontName.getName() + " 12 Tf 0 g";
form.setDefaultAppearance(da);
// add a page to the document
doc.addPage(page);
// add a form field to the form
PDTextField textBox = new PDTextField(form);
textBox.setPartialName("Chinese");
form.getFields().add(textBox);
// specify the annotation associated with the field
// and add it to the page
PDAnnotationWidget widget = textBox.getWidget();
PDRectangle rect = new PDRectangle(100f,300f,120f,350f);
widget.setRectangle(rect);
page.getAnnotations().add(widget);
// set the field value
textBox.setValue("木兰辞");
doc.save("ChineseOut.pdf");
which works fine. I also tested with the font you are using unfortunately this had an error as MingLiU is a TrueType collection which PDFBox can not handle at that point in time.