How to add an extra page to an existing pdf while keeping bookmarks? (PDFSharp etc.)

I have a PDF file and I would like to add an additional page to it, ideally, as the first page. I was able to achieve this using PDFSharp, but the problem is that there are bookmarks in the original PDF that I would like to support. Using PDFSharp seems to remove bookmarks, or at least I don't know any options or commands to save the original TOC with a newly created PDF that contains an extra page.

Does anyone know how to save TOC using PDFSharp or possibly any other .NET libraries, ideally free, that will allow me to add a page to an existing PDF file and maintain its bookmarks? (I know that adding a page as the first page will invalidate the links to the pages, so adding a page as the last page is also fine.)

Thanks everyone!

+6
source share
1 answer

It turned out that the PDF uses bookmarks, not TOC.

The solution that works with bookmarks is shown here:
http://forum.pdfsharp.net/viewtopic.php?p=6660#p6660

An existing file is opened for modifications, a new page is inserted at the beginning of the document - and all bookmarks still work.

Here's the code snippet:

static void Main(string[] args) { const string filename = "sample.pdf"; File.Copy(Path.Combine("D:\\PDFsharp\\PDFfiles\\sample\\", filename), Path.Combine(Directory.GetCurrentDirectory(), filename), true); // Open an existing document for editing and loop through its pages PdfDocument document = PdfReader.Open(filename); var newPage = document.InsertPage(0); // Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(newPage); // Create a font XFont font = new XFont("Times New Roman", 20, XFontStyle.BoldItalic); // Draw the text gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, newPage.Width, newPage.Height), XStringFormats.Center); document.Save(filename); // ...and start a viewer. Process.Start(filename); } 
+5
source

All Articles