Asked to save PDF file after flattening the forms with iTextSharp

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

Who is Participating?
Carlos VillegasConnect With a Mentor Full Stack .NET DeveloperCommented:
Also you can try use PdfSharp instead:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.