Tutorial - Bookmarks

Top  Previous  Next

This tutorial explains about inserting new bookmarks and manipulating the existing bookmarks from the PDF documents.

 

There are three examples in this section:

 

a) Insert new bookmarks

 

b) Load existing bookmark into a TreeView Control

 

 

 

Insert new bookmarks

 

Use CreateNewBookmark to create a new bookmark for the document. To manipulate an existing bookmark, call GetBookmarkRoot to obtain the root bookmark node of the document.         

 

[VB]         

 

Dim PDFDest As gtPDFDestinationX

Dim Outline1, Outline2, Outline3 As gtPDFOutlineX

Dim LAttribute As gtBookmarkAttributeX

 

Set LAttribute = gtPDFDocumentX1.CreateBookmarkAttribute

LAttribute.FontColor = RGB(255, 0, 0)

LAttribute.IsBold = True

LAttribute.IsItalic = True

 

With gtPDFDocumentX1

    'Load the document

    .LoadFromFile (InputFile1)

 

    'Set the neccessary bookmark destination settings

    Set PDFDest = .GetDestination

    PDFDest.Page = 1

    PDFDest.DestinationType = dtXYZ

    PDFDest.Left = 0

    PDFDest.Top = 0

    PDFDest.Zoom = 200

 

    ' Create new bookmark with title 'Chapter 1'

    Set Outline1 = .CreateNewBookmark("Chapter 1", PDFDest, LAttribute)

 

    ' Add 'Section 1' as child of 'Chapter 1'.

    PDFDest.Page = 2

    PDFDest.DestinationType = dtFitV

    Call Outline1.AddChild("Section 1", PDFDest, LAttribute)

 

    ' Insert 'Chapter 2' as next bookmark node.

    PDFDest.Page = 3

    PDFDest.DestinationType = dtFit

    Set Outline2 = Outline1.AddNext("Chapter 2", PDFDest, LAttribute)

 

    ' Insert 'Section 1' as child of 'Chapter 2' and

    ' 'Section 2' as the next node of 'Section 1'

    PDFDest.Page = 4

    PDFDest.DestinationType = dtFit

    Set Outline3 = Outline2.AddChild("Section 1", PDFDest, LAttribute)

 

    PDFDest.Page = 5

    PDFDest.DestinationType = dtFit

    Call Outline3.AddNext("Section 2", PDFDest, LAttribute)

 

    PDFDest.Page = 6

    PDFDest.DestinationType = dtFitV

    Call Outline2.AddNext("Chapter 3", PDFDest, LAttribute)

 

    'Save the document

    .SetPageMode pmUseOutlines

    .ShowSetupDialog = False

    .OpenAfterSave = True

    .SaveToFile (OutputFile)

End With

               

 

[VC++]         

       

   CgtPDFDestinationX PDFDest;

   CgtPDFOutlineX Outline1, Outline2, Outline3;

   CgtBookmarkAttributeX LAttribute;

   LAttribute = m_PDFDoc.CreateBookmarkAttribute();

   LAttribute.SetIsBold(1);

   LAttribute.SetIsItalic(1);

 

   //Load the document

   m_PDFDoc.LoadFromFile (InputFile1);

 

   //Set the neccessary bookmark destination settings

   PDFDest = m_PDFDoc.GetDestination();

   PDFDest.SetPage(1);

   PDFDest.SetDestinationType(dtXYZ);

   PDFDest.SetLeft(0);

   PDFDest.SetTop(0);

   PDFDest.SetZoom(200);

 

   //Create new bookmark with title "Chapter 1"

   Outline1 = m_PDFDoc.CreateNewBookmark("Chapter 1", PDFDest,LAttribute);

 

   //Add "Section 1" as child of 'Chapter 1'.

   PDFDest.SetPage(2);

   PDFDest.SetDestinationType(dtFitV);

   Outline1.AddChild("Section 1", PDFDest,LAttribute);

 

   // Insert 'Chapter 2' as the next bookmark node.

   PDFDest.SetPage(3);

   PDFDest.SetDestinationType(dtFit);

   Outline2 = Outline1.AddNext("Chapter 2", PDFDest,LAttribute);

 

   // Insert 'Section 1' as child of 'Chapter 2' and

   // 'Section 2' as the next node of 'Section 1'

   PDFDest.SetPage(4);

   PDFDest.SetDestinationType(dtFit);

   Outline3 = Outline2.AddChild("Section 1", PDFDest,LAttribute);

 

   PDFDest.SetPage(5);

   PDFDest.SetDestinationType(dtFit);

   Outline3.AddNext("Section 2", PDFDest,LAttribute);

 

   PDFDest.SetPage(6);

   PDFDest.SetDestinationType(dtFitV);

   Outline2.AddNext("Chapter 3", PDFDest,LAttribute);

 

   // Save the document

   m_PDFDoc.SetPageMode(pmUseOutlines);

   m_PDFDoc.SaveToFile (OutputFile);                

 

 

 

Load existing bookmark into a TreeView Control

 

The bookmark items can be added to a TreeView Control using recursion. First, obtain the Outline root's child node, and then traverse through its child nodes. Then, traverse each nodes to 'next' (sibling) node.        

 

[VB]        

 

Private Sub cmdLoadOutlines_Click()

Dim LOutline As gtPDFOutlineX

Dim LChild As gtPDFOutlineX

Dim LNode As Node         

gtPDFDocumentX1.LoadFromFile ("D:\\PDFs\\outline1.pdf")

Set LOutline = gtPDFDocumentX1.GetBookmarkRoot

If Not LOutline Is Nothing Then

'Obtain the first child of the root.

Set LChild = LOutline.GetFirstChild 

If Not LChild Is Nothing Then

Set LNode = TreeView1.Nodes.Add(, , LChild.Title, LChild.Title)

If Not LChild.Child Is Nothing Then

Call BuildOutlineChild(LNode, LChild.Child)

End If

If Not LChild.Next Is Nothing Then

Call BuildOutlineNext(LNode, LChild.Next)

End If

End If

End If

TreeView1.Style = tvwTreelinesPlusMinusText ' Style 6.

TreeView1.LineStyle = tvwRootLines

End Sub         

 

               

 

Private Sub BuildOutlineChild(aNode As Node, ByRef aOutline As gtPDFOutlineX)

Dim LNode As Node

Dim Outline As gtPDFOutlineX 

'Add aOutline as Child to aNode in the TreeView.

If Not aOutline Is Nothing Then

Set LNode = TreeView1.Nodes.Add(aNode.Text, tvwChild, aOutline.Title, aOutline.Title)

LNode.EnsureVisible 

End If

If Not aOutline.Child Is Nothing Then

Call BuildOutlineChild(LNode, aOutline.Child)

End If

If Not aOutline.Next Is Nothing Then

Call BuildOutlineNext(LNode, aOutline.Next)

End If

End Sub        

 

               

 

Private Sub BuildOutlineNext(aNode As Node, aOutline As gtPDFOutlineX)

Dim LNode As Node

'Add aOutline as Child to aNode in the TreeView.

Set LNode = TreeView1.Nodes.Add(aNode.Text, tvwNext, aOutline.Title, aOutline.Title)

LNode.EnsureVisible

If Not aOutline.Child Is Nothing Then

Call BuildOutlineChild(LNode, aOutline.Child)

End If

If Not aOutline.Next Is Nothing Then

Call BuildOutlineNext(LNode, aOutline.Next)

End If

End Sub        

 

                       

 

[VC++]         

 

       

void OnLoadOutlines() 

CgtPDFOutline LOutline, LChild;

m_PDFDOC1.LoadFromFile ("D:\\PDFs\\outline1.pdf");

LOutline = m_PDFDOC1.GetBookmarkRoot();

If (LOutline != Null

{         

       //Obtain the first child of the root. 

       LChild = LOutline.GetFirstChild();

       If (LChild != Null

       

       HTREEITEM LNode = m_TreeCtrl.InsertItem(LChild.GetTitle(), 0, 0, TVI_ROOT,TVI_LAST);

               If (LChild.GetChild() != Null)

               BuildOutlineChild(LNode, LChild.GetChild());

               If (LChild.GetNext() != Null)

               BuildOutlineNext(LNode, LChild.GetNext());

       

       

void BuildOutlineChild(HTREEITEM Node, CgtPDFOutline aOutline)

HTREEITEM LNode;

CgtPDFOutline Outline;

//Add aOutline As Child To aNode In the TreeView.

If (aOutline != Null)

LNode = m_TreeCtrl.InsertItem(aOutline.GetTitle(), 0, 0, Node, TVI_LAST);

If (aOutline.GetChild() != Null)

BuildOutlineChild(LNode, aOutline.GetChild());

If (aOutline.GetNext() != Null)

BuildOutlineNext(LNode, aOutline.GetNext());

}         

void BuildOutlineNext(HTREEITEM Node, CgtPDFOutline aOutline) 

HTREEITEM LNode;

//Add aOutline As Child To aNode In the TreeView.

LNode = m_TreeCtrl.InsertItem(aOutline.GetTitle(), 0, 0, Node, TVI_LAST);

If (aOutline.GetChild() != Null

BuildOutlineChild(LNode, aOutline.GetChild());

If (aOutline.GetNext() != Null

BuildOutlineNext(LNode, aOutline.GetNext()); 

}