Asked to save PDF file after flattening the forms with iTextSharp

Posted on 2011-04-23
Last Modified: 2012-05-11
Visual Studio 2008
Adobe Acrobat Reader v10.0.1
iTextSharp v5.0.6

Hi there,
I have written a small piece of code to open an existing PDF that has some fields in it using iTextSharp, fill in the fields, flatten the file to remove the field placeholders and then save it to a file.

The problem I have is when closing the file after viewing it in Acrobat Reader I am prompted with "Do you want to save changes to 'Test.pdf' before closing?"

If I view the template file it closes without any prompting so I know the problem didn't pre-exist.

If I omit the part in the code about flattening the form then it closes successfully without the prompting but the form fields are still visible in the document when you view it.  This is what has led me to believe that it has something to do with iTextSharps FormFlattening that is causing the problem.

I think the prompt is because Acrobat Reader has fixed something that iTextSharp has screwed up and is prompting to save a fixed copy of the document.

Any ideas how I can fix this?
' read the template document
Dim r as New PdfReader("C:\Template.pdf")

' Create a FileStream to the output PDF document
Dim FS as New IO.FileStream("C:\Test.pdf", IO.FileMode.Create)

Dim ps as New PdfStamper(r, FS)

' Retrieve the properties of PDF form with AcroFields object
Dim af As AcroFields = ps.AcroFields

' Fill in the fields
af.SetField("FirstName", "Blah")
af.SetField("LastName", "Blah2")

' Make resultant PDF Read-Only for end user
ps.FormFlattening = True

ps = Nothing

Open in new window

Question by:SoLost
    LVL 11

    Expert Comment

    You are probably correct that iTextSharp does not flatten properly. Have you tried to open the resultant file with an older version Acrobat Reader?

    You could manually remove the form fields after you've set them. This is really hacky since flattening should do the job for you.
    LVL 17

    Accepted Solution

    Also you can try use PdfSharp instead:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    PDF files have been in the limelight due to its unmatched features.  Personal documents, emails, business reports and eBooks are all converted into PDF files owing to peerless features provided by it. Adding watermark to a PDF file is a method to se…
    The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
    In this video, we show how to perform Bates Numbering/Stamping of PDF documents using Power PDF Advanced, the newest product from the Document Imaging division of Nuance Communications. There are two editions of Power PDF — Standard and Advanced. Th…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now