Aspose.Pdf

Adding bookmarks in the PDF document

Adding bookmarks for the headings in the PDF document is supported. You only need to specify two properties of the Pdf class. The property IsBookmarked is used to specify if some headings will be tagged as bookmarks. Only if IsBookmarked is set as true, another property BookMarkLevel will work, which default value is 0, that means all the headings in the Pdf will be tagged as bookmarks. If BookMarkLevel is a value greater than 0, it means only the headings which level is less than or equal to BookMarkLevel will be tagged as bookmarks.

 

The following example demonstrates how to add bookmarks to the headings of the Pdf.It only creates bookmarks for headings of Level one.

 

[C#]

 

 

Pdf pdf1 = new Pdf();

 

 

pdf1.IsBookmarked = true;

pdf1.BookMarkLevel = 1; 

 

 

Section sec1 = pdf1.Sections.Add();

Heading heading1 = new Heading(pdf1,sec1,1);

Segment segment1 = new Segment(heading1);

heading1.Segments.Add(segment1);

heading1.IsAutoSequence = true;

segment1.Content = "this is heading of level 1";

sec1.Paragraphs.Add(heading1);

Heading heading2 = new Heading(pdf1,sec1,2);

Segment segment2 = new Segment(heading2);

heading2.Segments.Add(segment2);

heading2.IsAutoSequence = true;

segment2.Content = "this is heading of level 2";

sec1.Paragraphs.Add(heading2);

Heading heading3 = new Heading(pdf1,sec1,1);

Segment segment3 = new Segment(heading3);

heading3.Segments.Add(segment3);

heading3.IsAutoSequence = false;

heading3.LabelWidth = 60;

heading3.UserLabel = "bullet1";

segment3.Content = "this is bullet style 1";

sec1.Paragraphs.Add(heading3);

 

 

pdf1.Save("testHeading.pdf");

 

 

[VB .NET]

 

 

Dim pdf1 As Pdf = New Pdf()

 

 

pdf1.IsBookmarked = true;

pdf1.BookMarkLevel = 1;

 

 

Dim sec1 As Section = pdf1.Sections.Add()

Dim heading1 As Heading = New Heading(pdf1, sec1, 1)

Dim segment1 As Segment = New Segment(heading1)

heading1.Segments.Add(segment1)

heading1.IsAutoSequence = True

segment1.Content = "this is heading of level 1"

sec1.Paragraphs.Add(heading1)

 

 

Dim heading2 As Heading = New Heading(pdf1, sec1, 2)

Dim segment2 As Segment = New Segment(heading2)

heading2.Segments.Add(segment2)

heading2.IsAutoSequence = True

segment2.Content = "this is heading of level 2"

sec1.Paragraphs.Add(heading2)

 

 

Dim heading3 As Heading = New Heading(pdf1, sec1, 1)

Dim segment3 As Segment = New Segment(heading3)

heading3.Segments.Add(segment3)

heading3.IsAutoSequence = False

heading3.LabelWidth = 60

heading3.UserLabel = "bullet1"

segment3.Content = "this is bullet style 1"

sec1.Paragraphs.Add(heading3)

 

 

pdf1.Save("testHeading.pdf")

 

[JAVA]

 

Pdf pdf1 = new Pdf();

 

pdf1.setBookmarked(true);                                         

pdf1.setBookMarkLevel(1);

 

Section sec1 = pdf1.getSections().add();

Heading heading1 = new Heading(pdf1,sec1,1);

Segment segment1 = new Segment(heading1);

heading1.getSegments().add(segment1);

heading1.setIsAutoSequence(true);

segment1.setContent("this is heading of level 1");

sec1.getParagraphs().add(heading1);

Heading heading2 = new Heading(pdf1,sec1,2);

Segment segment2 = new Segment(heading2);

heading2.getSegments().add(segment2);

heading2.setIsAutoSequence(true);

segment2.setContent("this is heading of level 2");

sec1.getParagraphs().add(heading2);

Heading heading3 = new Heading(pdf1,sec1,1);

Segment segment3 = new Segment(heading3);

heading3.getSegments().add(segment3);

heading3.setIsAutoSequence(false);

heading3.setLabelWidth(60);

heading3.setUserLabel("bullet1");

segment3.setContent("this is bullet style 1");

sec1.getParagraphs().add(heading3);                           

 

     

FileOutputStream out = new FileOutputStream(new File("testHeading.pdf"));

pdf1.save(out);

 

[XML]

 

 

    <?xml version="1.0" encoding="utf-8" ?>

    <Pdf xmlns="Aspose.Pdf" IsBookmarked="true" BookMarkLevel="1">

        <Section>

            <Heading Level="1" IsAutoSequence="true">

                <Segment>this is heading of level 1</Segment>

            </Heading>

            <Heading Level="2" IsAutoSequence="true">

                <Segment>this is heading of level 2</Segment>

            </Heading>

            <Heading Level="1" IsAutoSequence="false" LabelWidth="60" UserLabel="bullet1">

                <Segment>this is bullet style 1</Segment>

            </Heading>

         </Section>

    </Pdf>