PDF generated with iTextSharp always prompts to save changes when closing. And has missing pages when viewed with non-Acrobat PDF readers.


I've recently used iTextSharp to create a PDF by importing the 20 pages from an existing PDF and then adding a dynamically generated link to the bottom of the last page.  It works fine... kind of.  Viewing the generated PDF in Acrobat Reader on a windows PC displays everything as expected although when closing the document it always asks "Do you want to save changes?".  Viewing the generated PDF on a Surface Pro with PDF Reader displays the document without the first and last pages.  Apparently on a mobile device using Polaris Office the first and last pages are also missing.

I'm wondering if when the new PDF is generated it's not getting closed off quite properly and that's why it asks "Do you want to save changes?" when closing it.  And maybe that's also why it doesn't display correctly in some PDF reader apps.

Here's the code:

using (var reader = new PdfReader(HostingEnvironment.MapPath("~/app/pdf/OriginalDoc.pdf")))

            using (
                var fileStream =
                    new FileStream(
                        HostingEnvironment.MapPath("~/documents/attachments/DocWithLink_" + id + ".pdf"),
                        FileMode.Create, FileAccess.Write))
            	var document = new Document(reader.GetPageSizeWithRotation(1));
                var writer = PdfWriter.GetInstance(document, fileStream);

                using (PdfStamper stamper = new PdfStamper(reader, fileStream))
                    var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252,
                    Font linkFont = FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLUE);

                    for (var i = 1; i <= reader.NumberOfPages; i++)

                        var importedPage = writer.GetImportedPage(reader, i);
                        // Copy page of original document to new document.

                        var contentByte = writer.DirectContent;
                        contentByte.AddTemplate(importedPage, 0, 0);

                        if (i == reader.NumberOfPages) // It's the last page so add link.
                            PdfContentByte cb = stamper.GetOverContent(i);

                            //Create a ColumnText object
                            var ct = new ColumnText(cb);
                            //Set the rectangle to write to
                            ct.SetSimpleColumn(100, 30, 500, 90, 0, PdfContentByte.ALIGN_LEFT);

                            //Add some text and make it blue so that it looks like a hyperlink
                            var c = new Chunk("Click here!", linkFont);

                            var congrats = new Paragraph("Congratulations on reading the eBook!    ");
                            congrats.Alignment = PdfContentByte.ALIGN_LEFT;

                            c.SetAnchor("http://www.domain.com/pdf/response/" + encryptedId);
                            //Add the chunk to the ColumnText

                            //Tell the system to process the above commands

Open in new window

My question is, how do I modify the above so that when closing the generated PDF in Acrobat Reader there's no "Do you want to save changes?" prompt.  The answer to that may solve the problems with missing pages on Surface Pro etc but if you know anything else about what might be causing that I'd like to hear about it.

Any suggestions would be very welcome!  Thanks!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DanziggerAuthor Commented:
Here's the answer to this question on SO in case it's useful to someone: http://stackoverflow.com/a/32566304/5331772

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.