PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Jul 04, 2024 10:23 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Feb 08, 2013 7:23 pm 
Offline

Joined: Fri Feb 08, 2013 7:12 pm
Posts: 2
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);
}
}


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 12, 2013 8:54 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 12, 2013 2:21 pm 
Offline

Joined: Fri Feb 08, 2013 7:12 pm
Posts: 2
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 12, 2013 2:44 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
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);
}

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon May 20, 2013 7:33 am 
Offline

Joined: Mon Apr 08, 2013 6:18 am
Posts: 5
Hi Thomas/JunaidAnwar,

Could you please provide a complete sample on this.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 126 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