TOC formatting issues

Hi Tilal,

We are using the code mentioned in this link (http://www.aspose.com/docs/display/pdfnet/Add+TOC+to+Existing+PDF) for generating TOC.

We can format the title of TOC Page i.e. we can apply color…font size and we can Bold the title.

But we are unable to apply color, unable to change font size of headings.

We have collection of list containing headers and sub-headers. We want headers to bold and sub-headers to be normal text.

For example something like this…
Heading 1…
Sub-Heading 1-1
Sub-Heading 1-2
Heading 2…
Sub-Heading 2-1
Sub-Heading 2-2

Regards,
Siddesh

Hi Siddesh,


Thanks for your inquiry. Please check following code snippet to format TOC entries. Hopefully it will help you to accomplish the task.

// Load an existing PDF files<o:p></o:p>

Document doc = new Document(myDir + "input.pdf");

// Get access to first page of PDF file

Page tocPage = doc.Pages.Insert(1);

// Create object to represent TOC information

TocInfo tocInfo = new TocInfo();

TextFragment title = new TextFragment("Table Of Contents");

title.TextState.FontSize = 20;

title.TextState.FontStyle = FontStyles.Bold;

title.TextState.ForegroundColor = Aspose.Pdf.Color.Red;

// Set the title for TOC

tocInfo.Title = title;

tocPage.TocInfo = tocInfo;

// Create string objects which will be used as TOC elements

string[] titles = new string[5];

titles[0] = "English - Testing Header one";

titles[1] = "English - Testing Header two";

titles[2] = "English - Testing Header three";

titles[3] = "English - Testing Header four";

titles[4] = "English - Testing Header five";

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

{

// Create Heading object

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

TextSegment segment2 = new TextSegment();

segment2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic;

segment2.TextState.ForegroundColor = Aspose.Pdf.Color.Beige;

heading2.TocPage = tocPage;

heading2.Segments.Add(segment2);

// Specify the destination page for heading object

heading2.DestinationPage = doc.Pages[i + 2];

// Destination page

heading2.Top = doc.Pages[i + 2].Rect.Height;

// Destination coordinate

segment2.Text = titles[i];

// Add heading to page containing TOC

tocPage.Paragraphs.Add(heading2);

}

tocInfo.FormatArrayLength = titles.Length;

LevelFormat[] leveformat = tocInfo.FormatArray;

leveformat[0].TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;

leveformat[3].TextState.ForegroundColor = Aspose.Pdf.Color.Blue;

// Save the updated document

doc.Save(myDir + "TOC_Output.pdf");

Please feel free to contact us for any further assistance.


Best Reegards,

Thanks a lot Tilal,


We made some modifications in code according to our requirement and found that I didn't work in our case.

Please check the modified code below.


// Load an existing PDF files

Document doc = new Document(myDir + "input.pdf");

// Get access to first page of PDF file

Page tocPage = doc.Pages.Insert(1);

// Create object to represent TOC information

TocInfo tocInfo = new TocInfo();

TextFragment title = new TextFragment("Table Of Contents");

title.TextState.FontSize = 20;

title.TextState.FontStyle = FontStyles.Bold;


title.TextState.ForegroundColor = Aspose.Pdf.Color.Red;

//Align : Left didn't work in our case

title.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;

// Set the title for TOC

tocInfo.Title = title;

tocPage.TocInfo = tocInfo;

// Create string objects which will be used as TOC elements

string[] titles = new string[5];

titles[0] = "English - Testing Header one";

titles[1] = "English - Testing Header two";

titles[2] = "English - Testing Header three";

titles[3] = "English - Testing Header four";

titles[4] = "English - Testing Header five";

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

{

// Create Heading object

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

TextSegment segment2 = new TextSegment();

segment2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic;

segment2.TextState.ForegroundColor = Aspose.Pdf.Color.Beige;

heading2.TocPage = tocPage;

heading2.Segments.Add(segment2);

// Specify the destination page for heading object

heading2.DestinationPage = doc.Pages[i + 2];

// Destination page

heading2.Top = doc.Pages[i + 2].Rect.Height;

// Destination coordinate

segment2.Text = titles[i];

// Add heading to page containing TOC

tocPage.Paragraphs.Add(heading2);

}

tocInfo.FormatArrayLength = titles.Length;

LevelFormat[] leveformat = tocInfo.FormatArray;

leveformat[0].TextState.FontSize = 20.0f;

leveformat[1].TextState.FontSize = 10.0f;

leveformat[2].TextState.FontSize = 20.0f;

leveformat[3].TextState.FontSize = 10.0f;

leveformat[4].TextState.FontSize = 10.0f;

// Save the updated document

doc.Save(myDir + "TOC_Output.pdf");


We are facing issues like....align left for TOC title Is not working
and header and sub-headers of different size are getting overlapped.

Please find the attached folder containing screenshot of our requirement for TOC.

Regards,
Siddesh

Hi Siddesh,


Thanks for sharing the details.

In order to have proper formatting of TOC, please try setting line spacing between TOC contents. Please take a look over following code snippet.

[C#]

//
Load an existing PDF files
<o:p></o:p>

Document doc = new Document("c:/pdftest/source.pdf");

// Get access to first page of PDF file

Page tocPage = doc.Pages.Insert(1);

// Create object to represent TOC information

TocInfo tocInfo = new TocInfo();

TextFragment title = new TextFragment("Table Of Contents");

title.TextState.FontSize = 20;

title.TextState.FontStyle = FontStyles.Bold;

title.TextState.ForegroundColor = Aspose.Pdf.Color.Red;

//Align : Left didn't work in our case

title.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;

// Set the title for TOC

tocInfo.Title = title;

tocPage.TocInfo = tocInfo;

// Create string objects which will be used as TOC elements

string[] titles = new string[5];

titles[0] = "English - Testing Header one";

titles[1] = "English - Testing Header two";

titles[2] = "English - Testing Header three";

titles[3] = "English - Testing Header four";

titles[4] = "English - Testing Header five";

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

{

// Create Heading object

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

TextSegment segment2 = new TextSegment();

segment2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic;

segment2.TextState.ForegroundColor = Aspose.Pdf.Color.Beige;

heading2.TocPage = tocPage;

segment2.TextState.LineSpacing = 12;

heading2.Segments.Add(segment2);

// Specify the destination page for heading object

heading2.DestinationPage = doc.Pages[i + 2];

// Destination page

heading2.Top = doc.Pages[i + 2].Rect.Height;

// Destination coordinate

segment2.Text = titles[i];

// Add heading to page containing TOC

tocPage.Paragraphs.Add(heading2);

}

tocInfo.FormatArrayLength = titles.Length;

LevelFormat[] leveformat = tocInfo.FormatArray;

leveformat[0].TextState.FontSize = 20.0f;

leveformat[1].TextState.FontSize = 10.0f;

leveformat[2].TextState.FontSize = 20.0f;

leveformat[3].TextState.FontSize = 10.0f;

leveformat[4].TextState.FontSize = 10.0f;

// Save the updated document

doc.Save(“c:/pdftest/TOC_Formatting_Issue_Output.pdf”);

Thanks Nayyer for your guidance.

Is there any provision in Aspose to add line after "Table of Contents" title....???



And we tried to align TOC title at left using title.TextState.HorizontalAllignment = Aspose.Pdf.HorizontalAllignment.Left but it is not reflected in our TOC page.
It is getting center aligned in our case.
Can your team just verify this scenario and please share your code if the title is getting left aligned.


Regards,
Siddesh

Siddeshb:
And we tried to align TOC title at left using title.TextState.HorizontalAllignment = Aspose.Pdf.HorizontalAllignment.Left but it is not reflected in our TOC page.

It is getting center aligned in our case.
Can your team just verify this scenario and please share your code if the title is getting left aligned.

Hi Siddesh,


Thanks for sharing the details.


I
have tested the scenario and I am able to reproduce the same problem. For the
sake of correction, I have logged it in our issue tracking system as PDFNEWNET-38327. We
will investigate this issue in details and will keep you updated on the status
of a correction. <o:p></o:p>

We apologize for your inconvenience.

Hey Nayyer,

Is there any provision in Aspose to add line after Table of Content title…???
I have also asked this in my above post.

Regards,
Siddesh

Siddeshb:
Is there any provision in Aspose to add line after “Table of Contents” title…???
Hi Siddesh,

In order to accomplish this requirement, please try adding TextFragment instance to page containing TocInfo. Please take a look over following code snippet.

[C#]

//
Load an existing PDF files
<o:p></o:p>

Document doc = new Document("c:/pdftest/source.pdf");

// Get access to first page of PDF file

Page tocPage = doc.Pages.Insert(1);

// Create object to represent TOC information

TocInfo tocInfo = new TocInfo();

TextFragment title = new TextFragment("Table Of Contents");

title.TextState.FontSize = 15;

title.TextState.FontStyle = FontStyles.Bold;

title.TextState.ForegroundColor = Aspose.Pdf.Color.Red;

//Align : Left didn't work in our case

title.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;

// Set the title for TOC

tocInfo.Title = title;

tocPage.TocInfo = tocInfo;

tocPage.Paragraphs.Add(new TextFragment("--------------------------------"));

tocPage.Paragraphs.Add(new TextFragment(" "));

tocPage.Paragraphs.Add(new TextFragment(" "));

// Create string objects which will be used as TOC elements

string[] titles = new string[5];

titles[0] = "English - Testing Header one";

titles[1] = "English - Testing Header two";

titles[2] = "English - Testing Header three";

titles[3] = "English - Testing Header four";

titles[4] = "English - Testing Header five";

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

{

// Create Heading object

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

TextSegment segment2 = new TextSegment();

segment2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic;

segment2.TextState.ForegroundColor = Aspose.Pdf.Color.Beige;

heading2.TocPage = tocPage;

segment2.TextState.LineSpacing = 12;

heading2.Segments.Add(segment2);

// Specify the destination page for heading object

heading2.DestinationPage = doc.Pages[i + 2];

// Destination page

heading2.Top = doc.Pages[i + 2].Rect.Height;

// Destination coordinate

segment2.Text = titles[i];

// Add heading to page containing TOC

tocPage.Paragraphs.Add(heading2);

}

tocInfo.FormatArrayLength = titles.Length;

LevelFormat[] leveformat = tocInfo.FormatArray;

leveformat[0].TextState.FontSize = 20.0f;

leveformat[1].TextState.FontSize = 10.0f;

leveformat[2].TextState.FontSize = 20.0f;

leveformat[3].TextState.FontSize = 10.0f;

leveformat[4].TextState.FontSize = 10.0f;

// Save the updated document

doc.Save("c:/pdftest/TOC_Formatting_Issue_Output.pdf");

Thanks for your quick response.