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

Reading existing Outlines
https://forum.pdfsharp.net/viewtopic.php?f=2&t=307
Page 1 of 1

Author:  dsmeltz [ Thu Jan 31, 2008 2:57 pm ]
Post subject:  Reading existing Outlines

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.

Author:  cvanling [ Tue Aug 12, 2008 2:04 pm ]
Post subject:  Reading outlines from existing document

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/

Author:  BillE [ Tue Aug 12, 2008 8:07 pm ]
Post subject: 

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;
}

}

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