Skip to content
Guides

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:

java
public ArrayList<CPDFOutline> childOutline() {
    ArrayList<CPDFOutline> outlineList = new ArrayList<>();
    CPDFOutline pdfOutlineRoot = document.getOutlineRoot();
    if (pdfOutlineRoot == null){
        return outlineList;
    }
    CPDFOutline[] outlines = pdfOutlineRoot.getChildList();
    int numberOfOutline = outlines.length;
    for (int i = 0; i < numberOfOutline; i++) {
        outlineList.add(outlines[i]);
    }
    return outlineList;
}
kotlin
fun childOutline(): MutableList<CPDFOutline> {
    val pdfOutlineRoot: CPDFOutline = document.getOutlineRoot() ?: return mutableListOf()
    return if (pdfOutlineRoot.childList.isNotEmpty()) {
        pdfOutlineRoot.childList.toMutableList();
    } else {
        mutableListOf()
    }
}

Add a New Outline

The following are the steps for adding a new outline:

  1. Locate the parent outline where the new outline needs to be added.
  2. Create a new outline.
  3. Add actions to the outline, such as jumping to a page.
  4. Set properties.

This example shows how to add a new outline:

java
// Locate the parent outline where the new outline needs to be added.
CPDFOutline pdfOutlineRoot = document.getOutlineRoot();
if (pdfOutlineRoot == null){
   return;
}
CPDFOutline insertOutline = pdfOutlineRoot.insertChildAtIndex(0);
// Set properties
insertOutline.setTitle("New outline");
// Add outline action; in this case, it is to navigate to the first page.
int pageIndex = 0;
CPDFDestination destination = new CPDFDestination(pageIndex, 0F, 0, 1F);
insertOutline.setDestination(destination);
kotlin
// Locate the parent outline where the new outline needs to be added.
val pdfOutlineRoot: CPDFOutline = document.outlineRoot ?: return
pdfOutlineRoot.insertChildAtIndex(0).apply { 
  // Set properties.
  title = "insert0"
  // Add outline action; in this case, it is to navigate to the first page.
  destination = CPDFDestination(0, 0F, 0F, 1F)
}

Adjust the Order of the Outlines

Adjust the outline order using the function CPDFOutline.insertChildAtIndex(index). When moving an item in the outline hierarchy, ensure to retain the item and first call CPDFOutline.removeFromParent().

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:

java
CPDFOutline.removeFormParent();