Aspose.Pdf comes up with more control over specifying customized levels of headings and managing their sequence automatically. Aspose.Pdf provides Heading class that contains all related properties and methods to deal with such things.
Level of a Heading can be configured in two ways:
Auto sequencing can simply be applied by setting IsAutoSequence , a boolean property of Heading class, to true.
The following figure shows auto sequenced headings with different levels:
|
Figure: Levels of headings with auto sequencing |
The headings output in the above figure can be achieved using the source code given below in the example.
Code Snippet
[C#]
//Instntiate the Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create the section in the Pdf object
Section sec1 = pdf1.Sections.Add();
/*
* Create 1st heading in the Pdf object's section with level=1. Then create
* a text segment and add it in the heading. Set its StartNumber=6 to start
* the numbering from 6 and onwards. And don't forget to set IsAutoSequence=true.
* If IsAutoSeguence property is set to true then the heading's sequence is
* controlled automatically by Aspose.Pdf. After setting all properties, add
* heading into the paragraphs collection of the section
*/
Heading heading1 = new Heading(pdf1,sec1,1);
Segment segment1 = new Segment(heading1);
heading1.Segments.Add(segment1);
segment1.Content = "Level 1";
heading1.IsAutoSequence=true;
heading1.StartNumber=6;
sec1.Paragraphs.Add(heading1);
/*
* Create 2nd heading in the Pdf object's section with level=2. Then create
* a text segment and add it in the heading. And don't forget to set
* IsAutoSequence=true.If IsAutoSeguence property is set to true then the
* heading's sequence is controlled automatically by Aspose.Pdf. After setting
* all properties, add heading into the paragraphs collection of the section
*/
Heading heading2 = new Heading(pdf1,sec1,2);
Segment segment2 = new Segment(heading2);
heading2.Segments.Add(segment2);
segment2.Content = "Level 2";
heading2.IsAutoSequence=true;
sec1.Paragraphs.Add(heading2);
/*
* Create 3rd heading in the Pdf object's section with level=3. Then create
* a text segment and add it in the heading. And don't forget to set
* IsAutoSequence=true.If IsAutoSeguence property is set to true then the
* heading's sequence is controlled automatically by Aspose.Pdf. After setting
* all properties, add heading into the paragraphs collection of the section
*/
Heading heading3 = new Heading(pdf1,sec1,3);
Segment segment3 = new Segment(heading3);
heading3.Segments.Add(segment3);
segment3.Content = "Level 3";
heading3.IsAutoSequence=true;
sec1.Paragraphs.Add(heading3);
/*
* Prepare HttpResponse object and save the Pdf to HttpResponse object to open in
* Acrobat reader
*/
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType="application/pdf";
pdf1.Save("headings.pdf",SaveType.OpenInAcrobat,Response);
Response.End();
[VB.NET]
'Instntiate the Pdf object by calling its empty constructor
Dim pdf1 As Pdf = New Pdf()
'Create the section in the Pdf object
Dim sec1 As Section = pdf1.Sections.Add()
'*********************************************************************************
' * Create 1st heading in the Pdf object's section with level=1. Then create a
' * text segment and add it in the heading. Set its StartNumber=6 to start
' * the numbering from 6 and onwards. And don't forget to set IsAutoSequence=true.
' * If IsAutoSeguence property is set to true then the heading's sequence is
' * controlled automatically by Aspose.Pdf. After setting all properties, add
' * heading into the paragraphs collection of the section
'*********************************************************************************
Dim heading1 As Heading = New Heading(pdf1,sec1,1)
Dim segment1 As Segment = New Segment(heading1)
heading1.Segments.Add(segment1)
segment1.Content = "Level 1"
heading1.IsAutoSequence=True
heading1.StartNumber=6
sec1.Paragraphs.Add(heading1)
'*********************************************************************************
' * Create 2nd heading in the Pdf object's section with level=2. Then create a
' * text segment and add it in the heading. And don't forget to set
' * IsAutoSequence=true. If IsAutoSeguence property is set to true then the
' * heading's sequence is controlled automatically by Aspose.Pdf. After setting
' * all properties, add heading into the paragraphs collection of the section
'*********************************************************************************
Dim heading2 As Heading = New Heading(pdf1,sec1,2)
Dim segment2 As Segment = New Segment(heading2)
heading2.Segments.Add(segment2)
segment2.Content = "Level 2"
heading2.IsAutoSequence=True
sec1.Paragraphs.Add(heading2)
'*********************************************************************************
' * Create 3rd heading in the Pdf object's section with level=3. Then create a
' * text segment and add it in the heading. And don't forget to set
' * IsAutoSequence=true. If IsAutoSeguence property is set to true then the
' * heading's sequence is controlled automatically by Aspose.Pdf. After setting
' * all properties, add heading into the paragraphs collection of the section
'*********************************************************************************
Dim heading3 As Heading = New Heading(pdf1,sec1,3)
Dim segment3 As Segment = New Segment(heading3)
heading3.Segments.Add(segment3)
segment3.Content = "Level 3"
heading3.IsAutoSequence=True
sec1.Paragraphs.Add(heading3)
'*********************************************************************************
' * Prepare HttpResponse object and save the Pdf to HttpResponse object to open in
' * Acrobat reader
'*********************************************************************************
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType="application/pdf"
pdf1.Save("headings.pdf",SaveType.OpenInAcrobat,Response)
Response.End()
[JAVA]
//Instntiate the Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create the section in the Pdf object
Section sec1 = pdf1.getSections().add();
/*
* Create 1st heading in the Pdf object's section with level=1. Then create
* a text segment and add it in the heading. Set its StartNumber=6 to start
* the numbering from 6 and onwards. And don't forget to set IsAutoSequence=true.
* If IsAutoSeguence property is set to true then the heading's sequence is
* controlled automatically by Aspose.Pdf. After setting all properties, add
* heading into the paragraphs collection of the section
*/
Heading heading1 = new Heading(pdf1, sec1, 1);
Segment segment1 = new Segment(heading1, "Level 1");
heading1.getSegments().add(segment1);
heading1.setStartNumber(6);
heading1.setIsAutoSequence(true);
sec1.getParagraphs().add(heading1);
/*
* Create 2nd heading in the Pdf object's section with level=2. Then create
* a text segment and add it in the heading. And don't forget to set
* IsAutoSequence=true.If IsAutoSeguence property is set to true then the
* heading's sequence is controlled automatically by Aspose.Pdf. After setting
* all properties, add heading into the paragraphs collection of the section
*/
Heading heading2 = new Heading(pdf1, sec1, 2);
Segment segment2 = new Segment(heading2, "Level 2");
heading2.getSegments().add(segment2);
heading2.setIsAutoSequence(true);
sec1.getParagraphs().add(heading2);
/*
* Create 3rd heading in the Pdf object's section with level=3. Then create
* a text segment and add it in the heading. And don't forget to set
* IsAutoSequence=true.If IsAutoSeguence property is set to true then the
* heading's sequence is controlled automatically by Aspose.Pdf. After setting
* all properties, add heading into the paragraphs collection of the section
*/
Heading heading3 = new Heading(pdf1, sec1, 3);
Segment segment3 = new Segment(heading3, "Level 3");
heading3.getSegments().add(segment3);
heading3.setIsAutoSequence(true);
sec1.getParagraphs().add(heading3);
[XML]
<?xml version="1.0" encoding="utf-8" ?>
<Pdf xmlns="Aspose.Pdf">
<Section>
<Heading Level="1" StartNumber="6" IsAutoSequence="true">
<Segment>Level 1</Segment>
</Heading>
<Heading Level="2" IsAutoSequence="true">
<Segment>Level 2</Segment>
</Heading>
<Heading Level="3" IsAutoSequence="true">
<Segment>Level 3</Segment>
</Heading>
</Section>
</Pdf>