TOC in the Bookmars

Hi

What i have to do, that the ListSection "Tabel of Contents" is in th Bookmarks?

Regards Markus


This message was posted using Page2Forum from Introduction to DOM - Aspose.Pdf for .NET and Java

Hello Markus,

Thanks for considering Aspose.

Can you please share some details regarding your requirement. Are you creating a document from scratch and need to put the TOC section in the Bookmarks ? If so is the case, then please visit the following link for required information. Adding bookmarks in the PDF document.

You can also create an instance of Bookmark class and specify the Page Number of TOC section using PageNumber Property. Finally add it into Bookmarks collection of the PDF document.

or

You want to edit any existing document for which you need to put the TOC section into Bookmarks. If so is the case, then please try using Aspose.Pdf.Kit which is used to edit/manipulate existing PDF documents. Please visit the following link for information on how to Create Bookmarks.

In case it does not satisfy your requirement or I've not properly understood your requirement, please feel free to contact.

Hi

That is my Code:

ListSection tocSec = new ListSection("Table of Contents");

tocSec.ListType = ListType.TableOfContents;

tocSec.TextInfo.FontName = baseReport.DefaultFont;

HeaderFooter iconHead = baseReport.ExtendHeader(tocSec);

currentDoc.Sections.Add(tocSec);

Table of Contets, works, but it is not in the Bookmarks. How i must change the Code?

Regrards Markus

Hello Markus,

Thanks for the information.

Can you please try using the workaround that I’ve shared in my previous post i.e. Once the PDF file containing TOC information is generated, you can try using Aspose.Pdf.Kit and create a Bookmark to that page.

If it’s not acceptable, please let us know, we would try our level best to figure out a solution to your requirement.

Hi

We do not have a licence for aspose.pdf.kit.

But i dont want to edit a existing pdf document. the document is new generated.

I had try th things with headings, but in ths case the Titel is in the bookmarks, but the Titel 'Tabel of Contents" is 2 times in the document and on sepreate pages.

Createing a Bookmark instance and adding to the Bookmarks of the pdf did also not work, because the Bookmarks Collection is null on that time.

did you have a Code Excample for createing a TOC with the titel in the bookmarks?

Regards

Markus

Hello Markus,

Please take a look over the following code snippet in which I've created a PDF document, containing Title page, Table Of Contents Section containing hyperlinks to the pages within the same PDF document. The resultant PDF is also in attachment, please take a look.

[C#]

Pdf pdf1 = new Pdf();
pdf1.IsBookmarked = true;
// set the value for Level of headings to be Bookmarked
pdf1.BookMarkLevel = 1;

//==========================================//
// Create a Title page, add it to Bookmarks //
//==========================================//
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
Heading Title_heading = new Heading(pdf1, sec1, 1);
// create a segment for heading object
Segment Title_segment = new Segment(Title_heading);
// add the segment to segments collection of the Heading object
Title_heading.Segments.Add(Title_segment);
Title_segment.Content = "Title Page";
Title_segment.TextInfo.Alignment = AlignmentType.Center;
Title_segment.TextInfo.FontName = "Arial";
Title_segment.TextInfo.FontSize = 20;
// add heading to paragraphs collection of the TOC segment
sec1.Paragraphs.Add(Title_heading);

//============================================//
// Create TOC - section and add to PDF object //
//============================================//
//Create List Section of type TOC
ListSection tocSection = new ListSection(pdf1);
//Set its list type as table of of contents
tocSection.ListType = ListType.TableOfContents;
//Add the list section to the sections collection of the Pdf document
pdf1.Sections.Add(tocSection);
Heading heading1 = new Heading(pdf1, tocSection,1);
// create a segment for heading object
Segment segment1 = new Segment(heading1);
// add the segment to segments collection of the Heading object
heading1.Segments.Add(segment1);
segment1.Content = "Table Of Contents";
segment1.TextInfo.Alignment = AlignmentType.Center;
segment1.TextInfo.FontName = "Arial";
segment1.TextInfo.FontSize = 20;
// add heading to paragraphs collection of the TOC segment
tocSection.Paragraphs.Add(heading1);

for (int i = 1; i <= 10; i++)
{
Heading heading11 = new Heading(pdf1, tocSection, 2);
// create a segment for heading object
Segment segment11 = new Segment(heading11);
segment11.Content = i.ToString() + " - - - - - - - - - - - - - - - - - - - - - - - - Page " + i.ToString();
// set the wordspace between words
segment11.TextInfo.WordSpace = 4;
// set the line space between two lines
segment11.TextInfo.LineSpacing = 8;
}

// Save the resultant PDF document
pdf1.Save(@"D:/pdftest/TOC_Bookmark_Test.pdf");

I would suggest you to visit following links for information on topics covered in above code snippet.

In case it does not satisfy your requirement, or you've any further query, please feel free to contact.

Hi

This Solution has the Problem, the Titel "Table Of Contents" is on one Page and the List of the Contents is on the following Page.

But the Link in the Bookmarks work.

Regrards

Markus

Hello Markus,

Can you please take a look over the resultant PDF attached with previous post, in fact I'm unable to notice the issue that you have mentioned in your recent post.

I've tested the scenario using Aspose.Pdf v4.1.0.0.

Hi

I get your Code from the prevusly post. pleas have a look of the result. What is worng on my Envieroment?

The Version is: 4.1.0.0

Regards

hi

Its me again. I have build a Code-Excampel, that shows you my problems with the TOC:

Pdf pdf1 = new Pdf();

pdf1.IsBookmarked = true;

// set the value for Level of headings to be Bookmarked

pdf1.BookMarkLevel = 1;

//==========================================//

// Create a Title page, add it to Bookmarks //

//==========================================//

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

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

// create a segment for heading object

Segment Title_segment = new Segment(Title_heading);

// add the segment to segments collection of the Heading object

Title_heading.Segments.Add(Title_segment);

Title_segment.Content = "Title Page";

Title_segment.TextInfo.Alignment = AlignmentType.Center;

Title_segment.TextInfo.FontName = "Arial";

Title_segment.TextInfo.FontSize = 20;

// add heading to paragraphs collection of the TOC segment

sec1.Paragraphs.Add(Title_heading);

//============================================//

// Create TOC - section and add to PDF object //

//============================================//

//Create List Section of type TOC

ListSection tocSection = new ListSection(pdf1);

//Set its list type as table of of contents

tocSection.ListType = ListType.TableOfContents;

//Add the list section to the sections collection of the Pdf document

pdf1.Sections.Add(tocSection);

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

// create a segment for heading object

Segment segment1 = new Segment(heading1);

// add the segment to segments collection of the Heading object

heading1.Segments.Add(segment1);

segment1.Content = "Table Of Contents";

segment1.TextInfo.Alignment = AlignmentType.Center;

segment1.TextInfo.FontName = "Arial";

segment1.TextInfo.FontSize = 20;

// add heading to paragraphs collection of the TOC segment

tocSection.Paragraphs.Add(heading1);

for (int i = 1; i <= 10; i++)

{

Heading heading11 = new Heading(pdf1, tocSection, 2);

// create a segment for heading object

Segment segment11 = new Segment(heading11);

segment11.Content = i.ToString() + " - - - - - - - - - - - - - - - - - - - - - - - - Page " + i.ToString();

// set the wordspace between words

segment11.TextInfo.WordSpace = 4;

// set the line space between two lines

segment11.TextInfo.LineSpacing = 8;

}

for (int i = 0; i < 5; i++)

{

Section testSec = pdf1.Sections.Add();

//Titel erstellen

Heading head = new Heading(pdf1, testSec, 1);

Segment seg = new Segment(head);

head.Segments.Add(seg);

//head.IsAutoSequence = true;

seg.Content = string.Format("Chapter {0}", i);

head.IsInList = true;

testSec.Paragraphs.Add(head);

head.IsAutoSequence = true;

}

// Save the resultant PDF document

pdf1.Save(@"Z:/pdftest/TOC_Bookmark_Test.pdf");

System.Diagnostics.Process.Start(@"Z:/pdftest/TOC_Bookmark_Test.pdf");

Result is in the Attachment. you can see, the TOC stars on the next Page after the title "Table of Contens". the elemenst of the TOC has no sequence number.

Regrads Markus

Hello Markus,

The elements of the TOC were appearing over the next page, because you were not adding them to TOCSection. Also you created a new section and added those elements to new section. As by default every new section starts over the new page, so the elements were being displayed over the next page.

Secondly, please try using the simple text objects to specify the text paragraphs over target pages rather than creating a new Heading object. You can assign a specific ID to each text element and hyperlink them to the TOC elements. Please try using the following code snippet and see if it satisfies your requirement. The resultant PDF is also in attachment. There is a little issue in the resultant PDF document whereas it contains an extra blank page after the TOC is displayed. We are looking into the details of this problem and will update you with the status of correction, shortly. Meanwhile I wanted you to have a look over the resulting document in order to be sure that we both are in the same direction.

[C#]

Pdf pdf1 = new Pdf();
pdf1.IsBookmarked = true;
// set the value for Level of headings to be Bookmarked
pdf1.BookMarkLevel = 1;
//==========================================//
// Create a Title page, add it to Bookmarks //
//==========================================//

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
Heading Title_heading = new Heading(pdf1, sec1, 1);
// create a segment for heading object
Segment Title_segment = new Segment(Title_heading);
// add the segment to segments collection of the Heading object
Title_heading.Segments.Add(Title_segment);
Title_segment.Content = "Title Page";
Title_segment.TextInfo.Alignment = AlignmentType.Center;
Title_segment.TextInfo.FontName = "Arial";
Title_segment.TextInfo.FontSize = 20;
// add heading to paragraphs collection of the TOC segment
sec1.Paragraphs.Add(Title_heading);
//============================================//
// Create TOC - section and add to PDF object //
//============================================//

//Create List Section of type TOC
ListSection tocSection = new ListSection(pdf1);
//Set its list type as table of of contents
tocSection.ListType = ListType.TableOfContents;
//Add the list section to the sections collection of the Pdf document
pdf1.Sections.Add(tocSection);

Heading heading1 = new Heading(pdf1, tocSection, 1);
// create a segment for heading object
Segment segment1 = new Segment(heading1);
// add the segment to segments collection of the Heading object
heading1.Segments.Add(segment1);
segment1.Content = "Table Of Contents";
segment1.TextInfo.Alignment = AlignmentType.Center;
segment1.TextInfo.FontName = "Arial";
segment1.TextInfo.FontSize = 20;
// add heading to paragraphs collection of the TOC segment
tocSection.Paragraphs.Add(heading1);

for (int i = 1; i <= 10; i++)
{
Heading heading11 = new Heading(pdf1, tocSection, 2);
// create a segment for heading object
Segment segment11 = new Segment(heading11);
segment11.Content = "Chapter "+i.ToString() + " - - - - - - - - - - - - - - - - - - - - - - - - Page " + i.ToString();
// set the wordspace between words
segment11.TextInfo.WordSpace = 4;
// set the line space between two lines
segment11.TextInfo.LineSpacing = 8;
//Set the link type of the text segment to Local
segment11.Hyperlink.LinkType = HyperlinkType.Local;
//Assign the id of the desired paragraph as a target id for the text segment
segment11.Hyperlink.TargetID = "Chapter "+i.ToString();
heading11.Segments.Add(segment11);
tocSection.Paragraphs.Add(heading11);
}

for (int i = 0; i < 5; i++)
{
Aspose.Pdf.Section testSec = pdf1.Sections.Add();
//Create a text paragraph to be linked with the text segment
Text text3 = new Text(sec1, "Chapter "+i.ToString());
//Add the text paragraph to paragraphs collection of the section
testSec.Paragraphs.Add(text3);
//Set this paragraph to be the first so that it can be displayed in a separate
//page in the document
text3.IsFirstParagraph = true;
//Set the id of this text paragraph to "product1"
text3.ID = "Chapter "+i.ToString();
}

pdf1.Save("d:\\pdftest\\TOC_Bookmark_Test.pdf");

Hi,

I think there is a bug of blank page, I have logged it as PDFNET-10378, I hope we can fix it in about 2~3 weeks.

Thanks.

Hi

Sorry, but that is not that wat i want.

We need the Chaptertitels also in the bookmarks and i like to use the autosequence of the headings.

Regrads

Markus

Hello Markus,

Please try adding the following code lines to accomplish your requirement of displaying chapter numbers in Bookmarks, set the Pdf.BookmarkLevel value to 2. Which indicates that all the headings with level less than or equal to 2 are added as BookMark. In order to set the auto sequence for heading object, set the value of Heading.IsAutoSequene property to true.

I’ve made little change in the code and have marked TOC heading as Level 1 and all subsequent heading objects (chapter 1, chapter 2….) is marked are 1.1, 1.2, 1.3….. In order to accomplish this result, first set the value for IsAutoSequence property of TOC Heading object as True and then set the same property value as true for all subsequent heading objects (Chapter 1, Chapter2,….). The resultant PDF that I’ve generated in this case would be some think like the image “Case1” file displayed below.


In case you don’t need to set the sequence number for TOC heading object, then you can simply set the IsAutoSequence property value to true for Chapter 1, Chapter 2…. Heading objects and set the heading level as 1. The resultant PDF that would be generated in that case would be something like the image “Case2”, displayed below.

I’ve also attached both the PDF files that I’ve generated and I’ve tested it with Aspose.Pdf 4.0.0.0. When using v4.1.0.0, the Auto-Sequence number for heading objects is not starting properly. I have logged it in our issue tracking system as PDFNET-10447. We will investigate this issue in detail and will keep you updated on the status of a correction. We apologize for your inconvenience.

In order to generate the PDF document as specified against case 1, please try using the following code snippet.

[C#]


Pdf pdf1 = new Pdf();
pdf1.IsBookmarked = true;
// set the value for Level of headings to be Bookmarked
pdf1.BookMarkLevel = 2;

//==========================================//
// Create a Title page, add it to Bookmarks //
//==========================================//

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
Heading Title_heading = new Heading(pdf1, sec1, 1);
// create a segment for heading object
Segment Title_segment = new Segment(Title_heading);
// add the segment to segments collection of the Heading object
Title_heading.Segments.Add(Title_segment);
Title_segment.Content = "Title Page";
Title_segment.TextInfo.Alignment = AlignmentType.Center;
Title_segment.TextInfo.FontName = "Arial";
Title_segment.TextInfo.FontSize = 20;
// add heading to paragraphs collection of the TOC segment
sec1.Paragraphs.Add(Title_heading);
//============================================//
// Create TOC - section and add to PDF object //
//============================================//

//Create List Section of type TOC
ListSection tocSection = new ListSection(pdf1);
//Set its list type as table of of contents
tocSection.ListType = ListType.TableOfContents;
//Add the list section to the sections collection of the Pdf document
pdf1.Sections.Add(tocSection);

Heading heading1 = new Heading(pdf1, tocSection, 1);
// create a segment for heading object
Segment segment1 = new Segment(heading1);
// add the segment to segments collection of the Heading object
heading1.Segments.Add(segment1);
segment1.Content = "Table Of Contents";
segment1.TextInfo.Alignment = AlignmentType.Center;
segment1.TextInfo.FontName = "Arial";
segment1.TextInfo.FontSize = 20;
heading1.IsAutoSequence = true;
// add heading to paragraphs collection of the TOC segment
tocSection.Paragraphs.Add(heading1);

for (int i = 1; i <= 10; i++)
{
Heading heading11 = new Heading(pdf1, tocSection,2);
// create a segment for heading object
Segment segment11 = new Segment(heading11);
segment11.Content = "Chapter "+i.ToString() + " - - - - - - - - - - - - - - - - - - - - - - - - Page " + i.ToString();
// set the wordspace between words
segment11.TextInfo.WordSpace = 4;
// set the line space between two lines
segment11.TextInfo.LineSpacing = 8;
//Set the link type of the text segment to Local
segment11.Hyperlink.LinkType = HyperlinkType.Local;
//Assign the id of the desired paragraph as a target id for the text segment
segment11.Hyperlink.TargetID = "Chapter "+i.ToString();
heading11.IsAutoSequence = true;
heading11.Segments.Add(segment11);
tocSection.Paragraphs.Add(heading11);
}

for (int i = 1; i <=5; i++)
{
Aspose.Pdf.Section testSec = pdf1.Sections.Add();
//Create a text paragraph to be linked with the text segment
Text text3 = new Text(sec1, "Chapter "+i.ToString());
//Add the text paragraph to paragraphs collection of the section
testSec.Paragraphs.Add(text3);
//Set this paragraph to be the first so that it can be displayed in a separate
//page in the document
text3.IsFirstParagraph = true;
//Set the id of this text paragraph to "product1"
text3.ID = "Chapter "+i.ToString();
}

pdf1.Save("d:\\pdftest\\TOC_Bookmark_Test.pdf");

Please take a look and if it does not satisfy your requirement, please feel free to share.

Hi

Sorry, but your Solutions are not that what i want. I have build a new piece of Code that build a document in the structur that i need.

LicenseSetter.LicensePath = @"Z:\Entwicklungen\dho\DgvDevelopment\External Libraries";

LicenseSetter.SetLicense();

Pdf pdf1 = new Pdf();

pdf1.IsBookmarked = true;

// set the value for Level of headings to be Bookmarked

pdf1.BookMarkLevel = 2;

//==========================================//

// Create a Title page, add it to Bookmarks //

//==========================================//

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

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

// create a segment for heading object

Segment Title_segment = new Segment(Title_heading);

// add the segment to segments collection of the Heading object

Title_heading.Segments.Add(Title_segment);

Title_segment.Content = "Title Page";

Title_segment.TextInfo.Alignment = AlignmentType.Center;

Title_segment.TextInfo.FontName = "Arial";

Title_segment.TextInfo.FontSize = 20;

sec1.Paragraphs.Add(Title_heading);

//TOC insert

ListSection tocSec = new ListSection("Table Of Contents");

tocSec.ListType = ListType.TableOfContents;

pdf1.Sections.Add(tocSec);

//Build the Documten

Section chapterSec;

int level;

for (int i = 0; i < 10; i++)

{

chapterSec = pdf1.Sections.Add();

//Simulate Levels

if (i % 2 == 0)

level = 1;

else

level = 2;

//Chapter Titel

Heading head = new Heading(pdf1, chapterSec, level);

Segment seg = new Segment(head);

head.Segments.Add(seg);

//head.IsAutoSequence = true;

seg.Content = string.Format("Title Chapter {0}", i);

head.IsInList = true;

//if this line activ, the Sequence starts in the TOC

//head.IsAutoSequence = true;

chapterSec.Paragraphs.Add(head);

head.IsAutoSequence = true;

//Text as Contens of the Chapter

Text desc = new Text("balalalalaly
balalal");

desc.IsHtmlTagSupported = true;

chapterSec.Paragraphs.Add(desc);

}

// Save the resultant PDF document

pdf1.Save(@"Z:/pdftest/TOC_Bookmark_Test_4.pdf");

System.Diagnostics.Process.Start(@"Z:/pdftest/TOC_Bookmark_Test_4.pdf");

This has 2 problems:

  1. no link in the Bookmarks to "Table of Contes".
  2. the elemets in the TOC has no sequence number. I think that is Bug. this elemnts should have the same number in the TOC and on the page

the rest is ok.

Regards markus

Hello Markus,

I've tested the scenario and I'm able to notice both the issues. Regarding "Elements in the TOC has no sequence number" issue that you have reported as #2, I have logged it in our issue tracking system as PDFNET-10517. We will investigate this issue in detail and will keep you updated on the status of a correction. We apologize for your inconvenience.

Whereas concerning to issue #1, we are looking into this matter and will update you with the status of correction, shortly.

Your patience is greatly appreciated in this regard.

Hi Markus,

The issue has been resolved. Please try the attachment. Thanks.

Best regards.

Hello Markus,

Regarding your requirement # 1 to display Table Of Contents in Bookmarks area, please try using the following code snippet.

[C#]

ListSection tocSec = new ListSection(pdf1);
tocSec.ListType = ListType.TableOfContents;
pdf1.Sections.Add(tocSec);

Heading heading1 = new Heading(pdf1, tocSec, 1);
// create a segment for heading object
Segment segment1 = new Segment(heading1);
// add the segment to segments collection of the Heading object

heading1.Segments.Add(segment1);
segment1.Content = "Table Of Contents";
segment1.TextInfo.Alignment = AlignmentType.Center;
segment1.TextInfo.FontName = "Arial";
segment1.TextInfo.FontSize = 20;
segment1.TextInfo.Alignment = AlignmentType.Center;
// add heading to paragraphs collection of the TOC segment
tocSec.Paragraphs.Add(heading1);

Please try using the hotfix shared by Hans at this link

Hi Markus,

The issue #1 and #2 are fixed in the attached hotfix. Please try it.

P.S. This is a slight problem in your code. Please note the following lines in bold. You should set the text’s attribute before adding it to a paragraph set:

C#

Pdf pdf1 = new Pdf();

pdf1.IsBookmarked = true;
/* set the value for Level of headings to be Bookmark */
//==========================================//
// Create a Title page, add it to Bookmarks //
//==========================================//
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
Heading Title_heading = new Heading(pdf1, sec1, 1);

// create a segment for heading object
Segment Title_segment = new Segment.Title_heading);

// add the segment to segments collection of the heading object
Title_heading.Segments.Add(Title_segment);
Title_segment.Content = "Title Page";
Title_segment.TextInfo.Alignment = AlignmentType.Center;
Title_segment.TextInfo.FontName = "Arial";
Title_segment.TextInfo.FontSize = 20;
sec1.Paragraphs.Add(Title_heading);

// TOC insert
ListSection tocSec = new ListSection("Table Of Contents");
tocSec.ListType = ListType.TableOfContents;
pdf1.Sections.Add(tocSec);
tocSec.ListFormatArray.Length = 2;
tocSec.ListFormatArray[0].TextInfo.FontSize = 12;
tocSec.ListFormatArray[1].TextInfo.FontSize = 12;
tocSec.ListFormatArray[1].LeftMargin = 10;

// Build the Document
Aspose.Pdf.Section chapterSec;
int level;
for (int i = 0; i < 10; i++)
{
    chapterSec = pdf1.Sections.Add();

    // Simulate Levels
    if (i % 2 == 0) {
        level = 1;
    } else {
        level = 2;
    }

    // Chapter Title
    Heading head = new Heading(pdf1, chapterSec, level);
    Segment seg = new Segment(head);
    head.Segments.Add(seg);
    seg.Content = System.String.Format("Title Chapter{0}", i);
    head.IsInList = true;

    // if this line is active, the sequence starts in the TOC
    head.IsAutoSequence = true;

    chapterSec.Paragraphs.Add(head);

    // Text as Content of the Chapter
    Text desc = new Aspose.Pdf.Text("balalalalaly\nbalalal");
    desc.IsHtmlTagSupported = true;
    chapterSec.Paragraphs.Add(desc);
}
pdf1.Save(@"E:/temp/tests.pdf");

Best regards.

Hi

Great, it works perfectly.

Thanks a lot for your work.

Regards

Markus