A Delphi report that lists only top-level PDF bookmarks can miss an entire chapter of entries, while a report that treats every bookmark as a page link can send readers to the wrong place. Read the outline recursively with PDFium Component and record a local page number only when the bookmark resolves to a page in the current PDF.
Where do PDF bookmarks come from?
PDF bookmarks are the entries in the document outline. The outline is a tree. It lives in the PDF catalog, and an item can have a title, children, and a target. ISO 32000-1 §12.3.3, Table 153 describes how sibling items link to each other and how a parent points to its immediate children. A heading printed at the top of a page belongs to the page content, while its outline entry, if the PDF producer created one, lives in a different part of the file. That explains a familiar sight. A report can have a perfectly good printed table of contents and an empty bookmark panel.
In a three-page sample, "Chapter 2 - Main Body" and its child "Section 2.1 - Overview" both point to page 2. Its other child, "Section 2.2 - Details," shares page 3 with "Chapter 3 - Appendix." A map keyed by page number would retain at most three of these five entries. A map keyed by title would also be a poor fit in a longer manual where every chapter has an "Overview." Keep the sibling order provided by the document and record depth separately from the title.
Walking the bookmark tree in a Delphi console program
TPdf.Bookmarks returns the top-level entries in outline order. For every item, TPdf.HasBookmarkChildren tells you whether to read TPdf.BookmarkChildren. The following console program prints the title, a 1-based page number when available, and two spaces for each level of nesting. Create a Delphi console project with PDFium on the search path and put the PDFium runtime matching the executable's architecture beside the executable.
program ListPdfBookmarks;
{$APPTYPE CONSOLE}
uses
System.SysUtils, PDFium;
function TryGetLocalBookmarkPage(const Item: TBookmark;
PageCount: Integer; out PageNumber: Integer): Boolean;
var
Candidate: Integer;
begin
Result := False;
PageNumber := 0;
case Item.Action of
acGoto:
Candidate := Item.ActionPageNumber;
acUnsupported:
begin
if Item.ActionDestination.Handle <> nil then
Exit;
Candidate := Item.PageNumber;
end;
else
Exit;
end;
Result := (Candidate >= 1) and (Candidate <= PageCount);
if Result then
PageNumber := Candidate;
end;
procedure WriteBookmarks(Pdf: TPdf; const Items: TBookmarks;
Depth: Integer);
const
MaxDepth = 32;
var
Item: TBookmark;
Indent: string;
PageNumber: Integer;
begin
Indent := StringOfChar(' ', Depth * 2);
for Item in Items do
begin
if TryGetLocalBookmarkPage(Item, Pdf.PageCount, PageNumber) then
Writeln(Indent, '- ', string(Item.Title), ' (page ',
PageNumber, ')')
else
Writeln(Indent, '- ', string(Item.Title));
if Pdf.HasBookmarkChildren[Item] then
begin
if Depth < MaxDepth then
WriteBookmarks(Pdf, Pdf.BookmarkChildren[Item], Depth + 1)
else
Writeln(Indent, ' [deeper entries omitted]');
end;
end;
end;
procedure ListPdfBookmarks(const FileName: string);
var
Pdf: TPdf;
Roots: TBookmarks;
begin
Pdf := TPdf.Create(nil);
try
Pdf.FileName := FileName;
Pdf.Active := True;
if not Pdf.Active then
raise Exception.CreateFmt('Unable to open PDF: %s', [FileName]);
Roots := Pdf.Bookmarks;
if Length(Roots) = 0 then
Writeln('No bookmarks found')
else
WriteBookmarks(Pdf, Roots, 0);
finally
Pdf.Free;
end;
end;
begin
if ParamCount = 1 then
ListPdfBookmarks(ParamStr(1))
else
Writeln('Usage: ListPdfBookmarks.exe input.pdf');
end.
Download the three-page sample PDF with nested bookmarks, save it beside the executable, and pass its filename as the only argument. The expected output below preserves the indentation under chapter 2. The program prints "No bookmarks found" when the document opens successfully with an empty outline. A missing file, unreadable document, or missing runtime is an open failure and gets reported before the outline is read.
.\ListPdfBookmarks.exe PDFium-NavigationSample.pdf
- Chapter 1 - Introduction (page 1)
- Chapter 2 - Main Body (page 2)
- Section 2.1 - Overview (page 2)
- Section 2.2 - Details (page 3)
- Chapter 3 - Appendix (page 3)
The depth ceiling is a policy in the program above. It limits recursive descent on unfamiliar input and leaves a visible marker where a branch was shortened, and a production inventory with a strict output budget should also cap the number of entries it reports, because a shallow outline may have many siblings. For a viewer tree, populating children on demand usually makes more sense. A PDF can ask the viewer to display an item collapsed. The descendants are still present. Read the actual children for an inventory and use the open state only to decide what to expand in the interface.
Which bookmarks lead to a page in the current PDF?
TBookmark.PageNumber gives the page number returned by the bookmark destination lookup. A zero means there was no resolved page in that field. Check the action type before treating a positive value as a local page: the underlying lookup can also return a destination carried by an action. ISO 32000-1 Table 153 defines the outline item's destination and action entries. Section 12.6.4.2 describes local GoTo, while §12.6.4.3 describes remote GoTo. To see that fallback in the engine code itself, open the PDFium source for bookmarks, actions, and destinations and read FPDFBookmark_GetDest, which returns the item's own destination first and, when the item has none, the destination carried by its action.
| Bookmark form | How to treat it in a local page report |
|---|---|
| Direct destination | Use the bookmark page when the action check allows it and the page is within the current document |
| Local GoTo action | Use ActionPageNumber when it falls within the current document |
| Remote GoTo, URI, or launch action | Keep the title as an outline entry and omit the local page |
| Unrecognized action with a destination | Keep the title and leave the local page unassigned |
| Group with no resolved target | Print the title without a page and continue through its children |
In TryGetLocalBookmarkPage, one case does double duty. The acUnsupported result also covers the default value for bookmarks with no action. If that case carries an action destination, the function returns False. The page stays blank because the target type is unknown. A failed lookup still sets the output to zero and lets the caller descend into the branch, so the helper can be reused in a viewer that marks which bookmarks have a local page jump available.
A remote GoTo bookmark can point at page 2 of an appendix in another PDF, and if the current file has a page 2 too, a local jump from that number lands on unrelated content. The helper prints such a title without a page. A bookmark can also be a heading in the outline panel with no action at all. Both titles belong in the exported tree. For a clickable outline in an app, keep the local page jump as its own operation and route any external action through the app's existing handling for links and files.
Page numbers, labels, and view settings
TDestination.PageNumber is a 1-based position in the PDF. A page label such as "iv" or "A-1" comes from the document's page labeling scheme. A page number printed on the paper comes from page content. Those three values can disagree. A manual with a cover, front matter, and body pages shows it at once. Keep the numeric position in exported data so another program can open the page reliably, and add a display label in a distinct field if the report will also be read alongside a viewer.
A destination says two things. It says where on a page to land and how to frame it. ISO 32000-1 §12.3.2.2 describes explicit destinations such as /Fit and /XYZ. For local navigation that honors the original view, read the corresponding Destination or ActionDestination record and use ViewMode. Check HasX, HasY, and HasZoom before interpreting the optional XYZ values. If an input already provides a destination name, use TPdf.DestinationByName on the active PDF, with the stored destination name as the key, since it can differ from the title visible in the bookmark panel.
function TryGetNamedPdfPage(Pdf: TPdf; const Name: string;
out PageNumber: Integer): Boolean;
var
Destination: TDestination;
begin
Destination := Pdf.DestinationByName[WString(Name)];
Result := (Destination.PageNumber > 0) and
(Destination.PageNumber <= Pdf.PageCount);
PageNumber := 0;
if Result then
PageNumber := Destination.PageNumber;
end;
A report that stays useful when pages move
For a read-only outline report, title, depth, target type, and local page position are enough to explain where the entries go. In a structured export, give entries a traversal position or parent index so titles and pages can repeat. Capture values while the document is open and build a new inventory when the input PDF changes. Page positions describe the version you inspected. If an application goes on to insert, delete, or split pages, consult PDF outline editing and page remapping and the guide to splitting a PDF into multiple files for cases where destination pages must be carried across to new output.
- For a nested navigation panel, store a parent reference in each exported row. A row for "Section 2.2 - Details" can then be restored beneath chapter 2 even when the receiving application sorts the rows by title or page for another display.
- For a page-only index, group entries by page after the tree walk and retain every matching row inside its group. The receiving application can show both "Section 2.2 - Details" and "Chapter 3 - Appendix" in its list of shortcuts to page 3.
- For a PDF review report, keep entries whose destination could not be resolved. A group title can have child links with usable targets, while an external URI can be meaningful to the reviewer even though it has no local page. A blank page column leaves room for the target type to tell that story.
Start with an indented report when checking what an exported document actually contains. The PDFium Component for Delphi product page links to the installation and API documentation for adding the same outline reader to an application.