PDFtoolkit VCL
Edit, enhance, secure, merge, split, view, print PDF and AcroForms documents
Compatibility
Delphi C++Builder

How To Add Bookmarks To A PDF Document In Delphi

Learn how to use PDFtoolkit VCL to quickly add bookmarks to a document.
By V. Subhash

In PDF, a bookmark is considered to be an outline item. Most outline items link to a particular page. However, outline items can do more than that.

An outline item can be linked to a particular rectangular area on a specific page. It can also be set to link to a particular distance down from the top edge or the left edge of a page.

In PDFtoolkit, you need to use an TgtPDFOutline object for creating an outline item. To specify where a TgtPDFOutline object links to, you need to create a TgtPDFDestination object.

You also need to create a TgtBookmarkAttribute to specify the text style that needs to be used for displaying the bookmark in the bookmark panel of PDF viewer applications.

If you want to add bookmarks to a document, begin with the method TgtPDFDocument.CreateNewBookmark(). This returns the bookmark that was created and added by the method.

function CreateNewBookmark(
   // Text displayed by the bookmark in the bookmark panel
   Title: WideString;          
   // Destination
   Destination: TgtPDFDestination; 
   // Title text style
   OutLineAttribute: TgtBookmarkAttribute
):TgtPDFOutline; // new bookmark

Next, with the above bookmark, you can use the following TgtPDFOutline methods to add other bookmarks.

function AddChild(
  Title: WideString;
  Destination: TgtPDFDestination;
  OutLineAttribute: TgtBookmarkAttribute
): TgtPDFOutline; overload;

function AddPrev(
  Title: WideString;
  Destination: TgtPDFDestination;
  OutLineAttribute: TgtBookmarkAttribute
): TgtPDFOutline; overload;

function AddNext(
  Title: WideString;
  Destination: TgtPDFDestination;
  OutLineAttribute: TgtBookmarkAttribute
): TgtPDFOutline; overload;

If a document already has bookmarks, then you can use the following TgtPDFDocument method to get the bookmark root, which is a hidden bookmark.

function GetBookmarkRoot: TgtPDFOutline;

One you access the bookmark root, you need to use method TgtPDFOutline.GetFirstChild() to access the first bookmark in the document. After that, you can use the following TgtPDFOutline methods to parse the bookmark tree.

function GetFirstChild: TgtPDFOutline;
function GetNextNode: TgtPDFOutline;
function GetPrevNode: TgtPDFOutline;

In the code example below, you will see how to add bookmarks to all pages in a document.

program AddBookmarks;

{$APPTYPE CONSOLE}

uses
  SysUtils, gtCstPDFDoc, 
  gtClasses, gtExPDFDoc, 
  gtExProPDFDoc,
  gtPDFDoc, Graphics;

var
 I: Integer;
 gtPDFDocument1: TgtPDFDocument;
 // Bookmark
 gtPDFOutline1: TgtPDFOutline;
 // Destination linked by a bookmark
 gtPDFDestination1: TgtPDFDestination;
 // Display style of a bookmark in bookmark panel
 gtBookmarkAttribute1: TgtBookmarkAttribute;
begin
 gtPDFDocument1 := TgtPDFDocument.Create(Nil);
 try
  gtPDFDocument1.LoadFromFile('sample_doc.pdf');
  if gtPDFDocument1.IsLoaded then
  begin
   // For each page in the document
   for I := 1 to gtPDFDocument1.PageCount do
   begin
    // Create a bookmark that links to the top-left
    // corner of the page in the current iteration
    gtPDFDestination1 :=
       TgtPDFDestination.Create(
       I,      // Number of the page
       dtXYZ,  // Destination type (use x-y coords & zoom)
       0,      // X-coordinate of the destination
       0,      // Y-coordinate of the destination
       100);   // Zoom

    // Create bookmarks with maroon-colored, bold-italic text
    gtBookmarkAttribute1 :=
        TgtBookmarkAttribute.Create([
	    fsBold, fsItalic],
	    clMaroon);


    if I = 1 then
    begin
      // If it's the first page, then create a new bookmark
      gtPDFOutline1 
         := gtPDFDocument1.CreateNewBookmark(
               'Page #' + IntToStr(I),  // Bookmark title text
                gtPDFDestination1,
                gtBookmarkAttribute1);
    end
    else
    begin
     // For other pages, add a bookmark next to the
     // previously created bookmark
     gtPDFOutline1
        := gtPDFOutline1.AddNext(
                'Page #' + IntToStr(I),  // Bookmark title text
                gtPDFDestination1,
                gtBookmarkAttribute1);
    end;
   end;
  end;
  // Save the modified document
  gtPDFDocument1.SaveToFile('modified_doc.pdf');
 except on Err:Exception do
  Writeln(Err.Classname, ': ', Err.Message);
 end;
 // Free resources
 gtPDFDocument1.Reset;
 // Destroy PDF document object
 FreeAndNil(gtPDFDocument1);
end.

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.