Outlines
The outline is a structured navigation tool in PDF documents, typically displayed in the sidebar or panel of a document reader. It is often automatically generated based on the document's headings and chapter information, but can also be manually edited and adjusted.
The outline provides a hierarchical structure of the document, enabling users to locate and navigate content more easily. Additionally, users can use the outline to quickly navigate to different sections of the document.
Display the Outlines
Each heading or subheading in a PDF document is represented as a node in the outline tree, with branches connecting the nodes. The main title serves as the root node, and subheadings act as branches stemming from the root, forming a tree-like structure.
The outline is formed by recursively nesting nodes and subnodes, presenting the organizational framework of the document in a hierarchical manner. Nodes typically represent major sections, while subnodes represent subsections or chapters.
This example shows how to display the outline of a PDF recursively:
static private void TraverseOutline(List<CPDFOutline> outlineList)
{
foreach (var outline in outlineList)
{
// Print indentation, add 4 spaces for each nested level.
for (var i = 0; i < outlineCounter; i++)
{
Console.Write(" ");
}
// Print outline titles.
Console.Write("-> " + outline.Title + "\n");
// Increase outline numbering.
outlineNumber++;
// Retrieve the list of child outlines for the current outline.
var childList = outline.ChildList;
if (childList != null && childList.Count != 0)
{
// If there are child outlines, increase the indentation count.
outlineCounter++;
// Recursively traverse child outlines.
TraverseOutline(childList);
}
else
{
// Current level traversal completed, return to the parent outline.
if (outlineList.IndexOf(outline) + 1 == outlineList.Count)
{
// If the current outline is the last one at the current level, decrease the indentation count.
outlineCounter--;
}
}
}
}
Add a New Outline
The following are the steps for adding a new outline:
- Locate the parent outline where the new outline needs to be added.
- Create a new outline.
- Add actions to the outline, such as jumping to a page.
- Set properties.
This example shows how to add a new outline:
// Locate the parent outline where the new outline needs to be added.
CPDFOutline outline = document.GetOutlineRoot();
// Create the new outline.
CPDFOutline childOutline = null;
outline.InsertChildAtIndex(document, 0, ref childOutline);
// Add outline action; in this case, it is to navigate to the first page.
CPDFGoToAction gotoAction = new CPDFGoToAction();
CPDFDestination dest = new CPDFDestination();
dest.PageIndex = 0;
gotoAction.SetDestination(document, dest);
childOutline.SetAction(gotoAction);
// Set properties
childOutline.SetTitle("New outline");
Adjust the Order of the Outlines
Move a specific outline to be a child outline of the target outline.
The steps for moving an outline are as follows:
- Obtain the outline object for the target outline.
- Specify the position index of the child outline under the target outline, and move the outline to the corresponding position.
This example shows how to adjust the order of the outlines:
// Retrieve the outline object for the target outline.
CPDFOutline targetOutline = document.GetOutlineList()[1];
// Specify the index of the child outline to move to and move the designated outline to the specified position.
targetOutline.MoveChildAtIndex(document, outline, targetOutline.ChildList.Count);
Delete the Outline
Delete the target outline, after deletion, the child outlines of the target outline will also be removed.
This example shows how to delete the outlines:
CPDFOutline outline = document.GetOutlineList()[0];
outline.RemoveFromParent(document);