PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Merging files remove bookmarks on them
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2326
Page 1 of 1

Author:  JunaidAnwar [ Fri Feb 08, 2013 7:23 pm ]
Post subject:  Merging files remove bookmarks on them

I have a collection of PDF Files. Each PDF File has different number of multiple pages. I add bookmark to all PDF Files. After that i have to merge them in a single file but after doing so all the bookmarks gets removed. Please help me where im doing wrong. Im a newbie in development and found PDFSharp very easy to use My code is written below:


//Adding bookmarks to all the documents
foreach (string pdfFile in pdfFiles)
{
PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Modify);
XFont font = new XFont("Verdana", 12);

PdfPage page = inputPDFDocument.Pages[0];
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawString(PageName, font, XBrushes.Black, 40, 30, XStringFormats.Default);
// Create the root bookmark. You can set the style and the color.
PdfOutline outline = inputPDFDocument.Outlines.Add(PageName, page, true, PdfOutlineStyle.Bold, XColors.Red);
inputPDFDocument.Save(pdfFile);
}


//Merge all the docs into one doc
PdfDocument outputPDFDocument = new PdfDocument();

foreach (string pdfFile in pdfFiles)
{
PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);

outputPDFDocument.Version = inputPDFDocument.Version;
foreach (PdfPage page in inputPDFDocument.Pages)
{
outputPDFDocument.AddPage(page);
}
}

Author:  Thomas Hoevel [ Tue Feb 12, 2013 8:54 am ]
Post subject:  Re: Merging files remove bookmarks on them

Hi!
JunaidAnwar wrote:
Please help me where im doing wrong.
I don't know.
But it would be more efficient to merge the documents first and then add the bookmarks in a single program. This would also avoid the problem you have now.

I'm not sure whether this can be done with the Import option. It may require two steps (import all pages and save the file; open file, add labels and bookmarks, and save the file again (remember filenames and page counts during first step)).
It'll work using the technique shown in the Two Pages on One sample.

Author:  JunaidAnwar [ Tue Feb 12, 2013 2:21 pm ]
Post subject:  Re: Merging files remove bookmarks on them

Thanks Thomas. I have already done it in the same way. Can we add bookmarks to nested n level. I mean child bookmarks to some level like minimum 3 or 4. As i have not found any help regarding this on PDFSharp. One can only add title bookmark and Children upto 1 level. i want to go for 3 or 4 levels. Is it possible?

Author:  Thomas Hoevel [ Tue Feb 12, 2013 2:44 pm ]
Post subject:  Re: Merging files remove bookmarks on them

You can create as many levels as you need.

Here's a code snippet from MigraDoc:
Code:
internal void AddOutline(int level, string title, PdfPage destinationPage)
{
  if (level < 1 || destinationPage == null)
    return;

  PdfDocument document = destinationPage.Owner;

  if (document == null)
    return;

  PdfOutline.PdfOutlineCollection outlines = document.Outlines;
  while (--level > 0)
  {
    int count = outlines.Count;
    if (count == 0)
    {
      // You cannot add empty bookmarks to PDF. So we use blank here.
      PdfOutline outline = outlines.Add(" ", destinationPage, true);
      outlines = outline.Outlines;
    }
    else
      outlines = outlines[count - 1].Outlines;
  }
  outlines.Add(title, destinationPage, true);
}

Author:  hemasekhar [ Mon May 20, 2013 7:33 am ]
Post subject:  Re: Merging files remove bookmarks on them

Hi Thomas/JunaidAnwar,

Could you please provide a complete sample on this.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/