PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 10:13 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jan 31, 2008 2:57 pm 
Offline

Joined: Thu Jan 24, 2008 8:52 pm
Posts: 13
Has anybody had any success trying to get PDFSharp to read Outlines from an existing PDF document?

I have no problem creating new outlines and saving them to a new document, but reading existing outlines seems to be a missing feature.


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 12, 2008 2:04 pm 
Offline

Joined: Fri Jan 11, 2008 6:15 pm
Posts: 5
Yes, I have written some code to do this. It is posted on sourceforge, look for PdfMerge.

This required using some of the lower level features of PdfMerge, but it is certainly possible.

https://sourceforge.net/projects/pdfmerge/


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Aug 12, 2008 8:07 pm 
Offline

Joined: Sun Mar 02, 2008 3:17 pm
Posts: 6
Here is a quicky snippet from some code that walks the outline collection and renders it into a Windows Tree Control.

Oh, and PS did you ever work for GHR?

void DoWork(object obj)
{
PdfDocument doc = PdfReader.Open(Path);
using (doc)
{
ParseOutline(doc);
}
}

void ParseOutline(PdfDocument doc)
{
Trace.WriteLine(doc.FullPath + " is open.");
PdfOutline rootOutline = doc.Outlines.Parent;

//PdfItem keysValue = rootOutline.Elements["/First"];
//PdfDictionary titleDictionary = doc.Internals.AllObjects.OfType<PdfDictionary>().Where
// ((PdfDictionary d) => d.Elements["/Title"] != null).First<PdfDictionary>();

//PdfObjectID objID = PdfInternals.GetObjectID(titleDictionary);
//Trace.WriteLine(objID.ToString() + " " + titleDictionary);

//PdfOutline outline = new PdfOutline(titleDictionary);

Form.Invoke(Form.addNode, new object[] { ProcessNode(doc, null, rootOutline) });
}

TreeNode AddOutlineTreeNode(TreeNode parent, PdfOutline outline)
{
TreeNode tn = new TreeNode(outline.Title);
tn.Tag = outline;

if (parent != null )
parent.Nodes.Add(tn);

return tn;
}

TreeNode ProcessNode(PdfDocument doc, TreeNode parent, PdfOutline outline )
{
TreeNode returnNode = null;

PdfReference first = outline.Elements.GetReference("/First");
PdfReference last = outline.Elements.GetReference("/Last");

if (first == null || last == null)
return returnNode;

PdfOutline firstOutline = new PdfOutline(first.Value as PdfDictionary);
PdfOutline lastOutline = new PdfOutline(last.Value as PdfDictionary);
PdfOutline iterator = firstOutline;

do
{
PdfReference next = iterator.Elements.GetReference("/Next");
if (next != null)
{
PdfOutline nextOutline = new PdfOutline(next.Value as PdfDictionary);
returnNode = AddOutlineTreeNode(parent, nextOutline);
ProcessNode(doc, returnNode, nextOutline);
iterator = nextOutline;
}
else
{
// special case only called from the root outline.
returnNode = AddOutlineTreeNode(parent, iterator);
ProcessNode(doc, returnNode, iterator);
}
}
while (PdfInternals.GetObjectID(iterator) != PdfInternals.GetObjectID(lastOutline));
return returnNode;
}

}


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

All times are UTC


Who is online

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