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.
Privacy | Legal | Feedback | Newsletter © 2002-2010 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.