Link to home
Start Free TrialLog in
Avatar of Danzigger
Danzigger

asked on

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

Hi

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,
                        BaseFont.NOT_EMBEDDED);
                    Font linkFont = FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLUE);
                    document.Open();

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

                        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
                            congrats.Add(c);
                            ct.AddElement(congrats);

                            //Tell the system to process the above commands
                            ct.Go();
                        }
                    }
                }
            }
        }

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!
ASKER CERTIFIED SOLUTION
Avatar of Danzigger
Danzigger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial