PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 4:43 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Wed May 29, 2013 7:15 pm 
Offline

Joined: Wed May 08, 2013 10:56 am
Posts: 17
Below is the code we are using to "bundle" PDFs. For each document we add to the "source" bundle we want to add a Bookmark. The issue is that the root outline is not preserved the next time we open the bundle.

Code:
        public static void Concatenate(Stream source, Stream[] inputs)
        {
            PdfDocument sourceDoc = null;
            PdfOutline outline;

            try
            {
                sourceDoc = PdfReader.Open(source, PdfDocumentOpenMode.Modify);
            }
            catch // create the source doc (bundle) if it doesn't exist
            {
                sourceDoc = new PdfDocument();

                // create first page so we can add outline
                var rootPage = sourceDoc.Pages.Count > 0 ? sourceDoc.Pages[0] : sourceDoc.AddPage();
                XGraphics gfx = XGraphics.FromPdfPage(rootPage);
                gfx.DrawString("Bundle", new XFont("Verdana", 16), XBrushes.Black, 20, 50, XStringFormats.Default);
            }

            // for some reason not detecting outlines
            if (sourceDoc.Outlines.HasOutline)
            {
                outline = sourceDoc.Outlines[0];
            }
            else
            {
                outline = sourceDoc.Outlines.Add("Root", sourceDoc.Pages[0], true, PdfOutlineStyle.Bold);
            }

            foreach (var input in inputs)
            {
                var inputDoc = PdfReader.Open(input, PdfDocumentOpenMode.Import);

                bool addBookmark = true;

                foreach (PdfPage page in inputDoc.Pages)
                {
                    var newPage = sourceDoc.AddPage(page);

                    if (addBookmark)
                    {
                        // Add a bookmark for first page in this Document
                        outline.Outlines.Add("Document Added " + DateTime.UtcNow.ToString(), newPage, true);
                        addBookmark = false;
                    }
                }
            }

            sourceDoc.Save(source);
        }


Also is there a better way to check if the source document exists/is valid than wrapping in a try/catch?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 141 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group