Filling Acrofield does not work as intended - vb.net

I'm having hard times with this issue. I've a PDF File with fields so the user can fill them.
I'm using iTextSharp (on vb.net) to open the PDF file, read the fields and write the data back (and show it to the user).
Doc is not locked nor does it have any security measure.
I read it as follows:
Dim readerPDF As New PdfReader(Application.StartupPath & "\PDF\" & filename)
Dim pathTemp = IO.Path.GetTempFileName & ".pdf"
Dim stamperPDF As New PdfStamper(readerPDF, New IO.FileStream(pathTemp, IO.FileMode.Create))
Dim pdfFormFields As AcroFields = stamperPDF.AcroFields
then I simply loop pdfFormFields (I already have the key for each field saved in the database.
For each f as field_keys
pdfFormFields.SetField(f, "dummy text")
Next
This works great for every text input, no problems there. When I have a multiline field this thing goes nasty.
Filling using this approach gives me:
But If I open the original PDF file with Adobe Reader and I write the same I get:
So... I don't even know how to look this up on the internet. Is iTextSharp not supporting this or am I missing something?
EDIT: Link to PDF
EDIT: The itext dll was outdated. Download the lastest one and the problem still occurs.

The issue is likely with the properties of the multi-line field. One issue could be you have a fixed font and that would clip the text on screen when the field overflows. If the font is Auto it shrinks the font as the control fills up.

One thing to understand is that in PDF, the value of a field and the visual appearance of a field are two very different things. The field appearance is there to display the field in cases where the PDF viewer doesn't know what a field or it's value is. I believe the reason you're seeing What you're seeing is that there are some minor... and major... differences between how Adobe Reader generates appearances and how iText generates appearances. To test my theory, simply edit the field in Adobe Reader and see if the problem corrects itself. If it does, the the appearance generator in iText is the problem.

Related

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

PDF with fillable form fields + "Save As" = PDF with fixed text - how?

I'm creating a PDF using iTextSharp and it contains some fillable form fields. What I need, is to somehow set it up so that when those form fields are filled in and the resulting PDF is saved (in one of the commercially-available PDF readers like Adobe's Reader), I need those form fields to be fixed text (no longer editable).
Is there any way to do this?
As a comment suggests, this sounds like "flattening the document".
The issue with that process is that it is not available in (Adobe) Reader; it would require Acrobat, or server-side help.
On the other hand, some mobile PDF viewers do actually offer flattening when saving.
The workaround for Reader is to set the fields to read-only when saving the document. You would do this in the willSave Document Action by looping through the fields and setting them to readonly.
Simply Print your document as PDF. This will flatten the file.
For this
1. Install Adobe PDF Printer or CutePDF or some similar tool.
2. From your document. Select File -> Print.
3. Select Printer as the tool you installed in Step 1.
4. Your document will be a flattened, non-editable PDF now.

Can I watermark XFA PDF using iText (commercial or not)?

Well, after spending quite some time playing with this, I think I may have found a solution soon after I posted the question. Bit embarrassing but hey, it seems to work now.
I don't want to delete the question yet, in case someone wants the details. Basically, I tried again point (c) but in the XML I put back I used an embedded image. So, it seems I can change the XFA template master page using iText. More testing to follow ...
=========================
The problem: Apply watermark to a PDF XFA (dynamic or not). At the end the PDF should still be an XFA and should have all its security settings intact. I don't have control over the PDFs coming in.
The question: Can I use iText 5 to do that? If yes, is it via PdfStamper.getUnderContent() or via XfaForm.setXfa() or something else ? The first 2 didn't work for me yet.
I wonder if it's some XFA detail that I am missing (i.e. when I try to replace the XML), not sure how the XFA is actually protected from changes. Do I need to generate some UUID, encrypt some things, something about signatures ...
Btw, if I take the PDF generated by iText after replacing the XML, then open and save in LC, the watermark shows
thanks,
Cristian
========================
Anyway, that's the short version of my question. If you think you can help and/or are interested in more details ...
I know this or similar questions came up before but what I have tried so far didn't work for me. I also admit I am not a IText, XFA or PDF Standard expert. I browsed a number of forums posts, the Itext book, browsed the specs, no luck yet.
The PDF input has no regular usage rights, no security. When opened in Acrobat it will show the restrictions on Changing Document/Document Assembly/Adding templates but I have a feeling all XFAs have that. Nothing comes back from the following ...
System.out.println(" permissions " + reader.getPermissions());
System.out.println(" usage rights " + reader.hasUsageRights());
System.out.println(" viewer pref" + reader.getSimpleViewerPreferences());
Here are some of the things I have been through:
a) doing in it LC (https://forums.adobe.com/thread/496558)
This works. If I try to place an image on the master page, then it shows as watermark on all pages when saved from LC.
b) trying to write under using iText (https://sourceforge.net/p/itext/mailman/message/17225398/)
I found a post from '07 suggesting using pdfStamper.getUnderContent(). The thread does not seem to conclude if it did work eventually for the person asking
For me, the code works for a simple PDF, but not for XFA
PdfContentByte under = pdfStamper.getUnderContent(1);
under.beginText();
BaseFont FONT = BaseFont.createFont("c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
under.setFontAndSize(FONT, 40);
under.showTextAligned(Element.ALIGN_CENTER, "TEST_TEXT", 200, 600, 45);
under.endText();
c) generate the XML and replace it using iText (Some pdf file watermark does not show using iText)
After reading the post above and section 8.6 from itext in action, it seemed this is the correct path, so:
I create a simple XFA using LiveCycle
Save as xfa1.pdf and extract xml (using iText) to xfa1.xml
in LC add an image to the masterpage and save as xfa2.pdf
open xfa2.pdf and notice the watermark is present
extract XML from xfa2 to xfa2.xml and compare to xfa1.xml - notice the image element
either place the image element in original XML or use xfa2.xml and replace the XFA in original xfa1.pdf using iText, let's call it xfa3_itext;
Relevant code:
XfaForm xfa = new XfaForm(reader);
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
fact.setNamespaceAware(true);
DocumentBuilder db = fact.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(xml));
xfa.setDomDocument(doc);
xfa.setChanged(true);
XfaForm.setXfa(xfa, stamper.getReader(), stamper.getWriter());
I have tried with a sample PDF that was provided by a customer and with a simple XFA form created in LiveCycle Designer, no luck. If I open xfa3_itext in LiveCycle it does the watermark and if I save it again from LiveCycle as dynamic XFA PDF, the new PDF shows the watermark.
phew, that is a long post ... sorry.
thanks for reading and for any feedback

PDF downloading instead of opening in new tab

This is not a back-end programming question. I can only modify the markup or script (or the document itself). The reason I'm asking here is because all my searches for appropriate terms inevitably lead to questions and solutions about programming this functionality. I'm not trying to force it via progrmaming; I have to find out why this PDF is behaving differently.
So:
I have a bunch of links to PDFs on a page. Most of them open in new tabs, but one of them, the most recent, starts to open in a tab, but then the tab closes and the PDF gets downloaded as a file instead. All markup is consistent - there's nothing differnt about the odd-man-out except the actual URL.
You can see this here:
http://calwater.mwnewsroom.com/Investor-Relations/Financial-Reports/Annual-Reports
All annual reports up to 2012 open in a new tab, but 2013 downloads instead.
This leads me to believe that there is some meta-data property of the PDF itself that tells it how to open, and that, in this case, the 2013 PDF was created using different settings.
Apparently, the PDF was saved out to PDF from InDesign.
Does anyone have any insight?
Problem solved. There was simply an error in the string (like an extra period) that references the attachment such that it couldn't tell it was a PDF. Fixing the reference fixed the problem.

Concatenating PDF

After concatenating 2 pdf's using PdfCopyFields, I am able to edit the PDF fields, but I can't save the merged PDF. Why am I not able to save the PDF after doing this?
A form that has been Reader Enabled must be saved in append mode in order to continue working as such. PdfCopyFields creates a new PDF from scratch.
To get this working, you'll have to duplicate most of PdfCopyField's code via a PdfStamper set to append. Hurray for open source.
And keep in mind that iText 5.x is AGPL. Anyone with access to the program output must also have access to the source.
(which is why quite a few people are still using 2.1.7)