|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
Can't save PDF after filling in form fields with iTextSharpHi!
Working on a project using itextsharp and vb.net where I want to produce a partially filled out PDF with editable forms and the option to sign with a digital signature.
I had it working just fine until last week when I started getting an error when trying to save the final document in Adobe Reader. The message says:
"The document could not be saved. There was a problem reading this document (26). "
I have a template PDF with forms and extended features enabled which I want to make an editable copy of with some of the forms filled out with data from database. The idea is to be able to take the pre-filled document out on a laptop to be filled out completely and signed offline.
The problem seems to occur as soon as I open the document with PdfStamper, set the value of a field and then close the document, using the code below. I even tried it with a blank PDF with just one field called "test" and still get the same error.
' --
Dim reader As PdfReader = New PdfReader(sDocTemplate)
Dim stamper As New PdfStamper(reader, New FileStream(sOutputFile, FileMode.Append))
stamper.AcroFields.SetField("test", "hello")
stamper.Close()
' --
When I open the produced PDF there is a small window saying something about the document being damaged and repaired, then it opens and everything seems fine. The forms contain the proper data and everything, but when trying to save it the mentioned error message is displayed.
As I said, it was working fine right up until this error appeared out of the blue. I haven't updated any relevant software that I know of and I think I've eliminated all sources of error right down to itextsharp when testing with a blank pdf.
Any ideas of what could be wrong would be highly appreciated!
/ Fredrik
------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference _______________________________________________ iText-questions mailing list iText-questions@... https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://www.1t3xt.com/docs/book.php Check the site with examples before you ask questions: http://www.1t3xt.info/examples/ You can also search the keywords list: http://1t3xt.info/tutorials/keywords/ |
|
|
Re: Can't save PDF after filling in form fields with iTextSharpHi, Fredrik,
The example http://www.1t3xt.info/examples/browse/?page=example&id=348 will save you, as it can be used to prefill a PDF and preserve the usage rights (namely, further filling and saving by the user). But you have to pay attention to address the fields to set values to. I used the example http://1t3xt.info/examples/browse/?page=example&id=440 to get the XFA of the PDF file, after I examined the datasets element and adrressed the desired element by using the the its parent axis starting below data child element of datasets element. Now I am looking for a method to lookup within the datasets element and automatically generate a Java array containing (field name (appropriately addressed as above), value). I don't know Java, so that I will try to force a XSLT transformation of the datasets element pushed out the PDF file, in order to obtain this array. The most adequate and elegant way to solve this is by using XFA of the PDF file. I managed to replace the XFA with but the usage rights aren't preserved (I've got an answer on this ML that this is possible, but with some work; I would gladly do it, but I don't know Java). This way is simple elegant. When I will solve the lookup, I will tell you. Good luck, Claudius
|
|
|
Re: Can't save PDF after filling in form fields with iTextSharpHello Claudius and thanks a ton for your answer!
I found the solution in your first example, the problem was the way I declared my PdfStamper. I changed this row: Dim stamper As New PdfStamper(reader, New FileStream(sOutputFile, FileMode.Append))
to this: Dim stamper As New PdfStamper(reader, New FileStream(sOutputFile, FileMode.Open), Chr(0), True)
and now it works perfectly!
I hope you find the answers you need as well, I'm not that into XML so I'm afraid I'm not much help there. Thanks again! / Fredrik
2009/10/27 claud108 <claud108@...>
------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference _______________________________________________ iText-questions mailing list iText-questions@... https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://www.1t3xt.com/docs/book.php Check the site with examples before you ask questions: http://www.1t3xt.info/examples/ You can also search the keywords list: http://1t3xt.info/tutorials/keywords/ |
|
|
Re: Can't save PDF after filling in form fields with iTextSharpThis will solve simple PDFs.
For PDFs with more complicated form inside will be very hard to standardize the class you use. For instance, if you would have repetitive structures, as rows in a table, you would have to address each field, for instance in a loop, and so on ... Too complicated. This is why I said that using XFA is simple and elegant. Claudius |
|
|
Re: Can't save PDF after filling in form fields with iTextSharpWell, it happens to be that I have a table of text fields and yes, I loop through them all to fill them with data.
The loop itself isn't too complicated, the tricky part is parsing the xml from database and converting it to a data structure with names matching the fields in the PDF.
' -------- This is the loop going through the fields and matching them to the parsed data in cData Dim formFields As AcroFields = stamper.AcroFields Dim ofield As New DictionaryEntry
For Each ofield In stamper.AcroFields.Fields If cData.ItemExist(ofield.Key) Then stamper.AcroFields.SetField(ofield.Key, cData.Item(ofield.Key).Value) End If
Next stamper.Close() ' ---------- This of course requires that I know the names of the fields beforehand, which I do since I created the PDF myself.
Perhaps the XFA solution is simpler, but as I said I'm no expert in XML and this does the job. Still interested in seeing your solution though! / Fredrik 2009/10/28 claud108 <claud108@...>
------------------------------------------------------------------------------ Come build with us! The BlackBerry(R) Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9 - 12, 2009. Register now! http://p.sf.net/sfu/devconference _______________________________________________ iText-questions mailing list iText-questions@... https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://www.1t3xt.com/docs/book.php Check the site with examples before you ask questions: http://www.1t3xt.info/examples/ You can also search the keywords list: http://1t3xt.info/tutorials/keywords/ |
| Free embeddable forum powered by Nabble | Forum Help |