Dynamically generated table of contents formatting issues

Hello,

I’m trying to dynamically create a table of contents and I am getting the oddest styling error.

What I’m seeing is that a few of the rows are out of alignment. For example,

Title

Page 1

Page 2

Page 3

instead of

Title

Page 1

Page 2

Page 3

I’ve tried a bunch of different settings to try and get it to align but it doesn’t want to play ball (such as setting default margins, removing character spacing etc). Is anyone else getting this problem? I attached the function used to create the TOC below.

Thanks :=)

public void InsertIndexPage(string sourcePath, string destPath, Dictionary<int, string> pageIndexes)

{

PdfFileEditor pdfEditor = new PdfFileEditor();

Aspose.Pdf.

Document pdfDocument = new Aspose.Pdf.Document(sourcePath);

// Get access to first page of PDF file

Page tocPage = pdfDocument.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;

// Set the title for TOC

tocInfo.Title = title;

tocPage.TocInfo = tocInfo;

// loop through pageIndexes

foreach (var pageIndex in pageIndexes)

{

// Create Heading object

Aspose.Pdf.

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

TextSegment segment2 = new TextSegment();

heading2.TocPage = tocPage;

heading2.Segments.Add(segment2);

// Specify the destination page for heading object

heading2.DestinationPage = pdfDocument.Pages[pageIndex.Key + 1];

// Destination page

heading2.Top = pdfDocument.Pages[pageIndex.Key + 1].Rect.Height;

// Destination coordinate

segment2.Text =

"Whatever"

// format

heading2.Margin.Top = 5;

// Add heading to page containing TOC

tocPage.Paragraphs.Add(heading2);

}

pdfDocument.Save(destPath);

}

Hi Dan,

Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 8.6.0 where I have used the same code snippet which you have shared and I am unable to notice any problem. The TOC elements are properly appearing. For your reference, I have also attached the resultant PDF generated over my end.

C#

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();

// Get access to first page of PDF file
Page tocPage = pdfDocument.Pages.Add();

// 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;

// Set the title for TOC
tocInfo.Title = title;
tocPage.TocInfo = tocInfo;

// loop through pageIndexes
for(int i =0; i<=3; i++)
{
  // Create Heading object
  Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
  TextSegment segment2 = new TextSegment();
  heading2.TocPage = tocPage;
  heading2.Segments.Add(segment2);

  // Specify the destination page for heading object
  heading2.DestinationPage = pdfDocument.Pages[1];

  // Destination page
  heading2.Top = pdfDocument.Pages[1].Rect.Height;

  // Destination coordinate
  segment2.Text = "Whatever";

  // format
  heading2.Margin.Top = 5;

  // Add heading to page containing TOC
  tocPage.Paragraphs.Add(heading2);
}

pdfDocument.Save("c:/pdftest/TOCFormattingIssue.pdf");

Interesting, thank you for the speedy reply. It looks like we are using 8.3.0.

I'll have to try it with a few other text strings, perhaps it has something to do with how they are created (the actual text strings aren't "whatever").

I'll keep you guys updated with what I find.

It looks like it is something to do with the string lengths...I've updated the method below to use the string lengths that I have on my version, I've blanked them out because the string values I use are propriety, however, it should still show the problem when you run it on your version.

Let me know how it goes and thanks in advance.

P.S I noticed that the code I'm pasting is formatted terribly. Is there a way of making it nice to view, such as a tag that I can add to it?

public void InsertIndexPage(string sourcePath, string destPath, Dictionary<int, string> pageIndexes)

{

PdfFileEditor pdfEditor = new PdfFileEditor();

Aspose.Pdf.

Document pdfDocument = new Aspose.Pdf.Document(sourcePath);

// Get access to first page of PDF file

Page tocPage = pdfDocument.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;

// Set the title for TOC

tocInfo.Title = title;

tocPage.TocInfo = tocInfo;

string[] arrItems = new string[15];

arrItems[0] =

"Xxx xxxx xxxxxx x Xxxxxxxxx";

arrItems[1] =

"Xxx xxxx xxxxxx x Xxxxxxx";

arrItems[2] =

"Xx xxxxx xxxxxxxx xXXXx";

arrItems[3] =

"XXX Xxxxx Xxxxxxxxxxx";

arrItems[4] =

"Xxxxxxxxx xx xxxxx xXXXx";

arrItems[5] =

"XXX Xxxxx Xxxxxxxxxxx";

arrItems[6] =

"Xxxxxx Xxxxxx";

arrItems[7] =

"Xxxxxx Xxxxxxxx xx Xxxxxxxxx";

arrItems[8] =

"Xxxxxx Xxxxxxxx xx Xxxxxxx";

arrItems[9] =

"Xxxxx Xxxxxxxxxxx";

arrItems[10] =

"Xxxxx xxxxxxxx";

arrItems[11] =

"XxxxXxx";

arrItems[12] =

"Xxxxxx Xxxxx Xxxxxxx";

arrItems[13] =

"Xxxxxx Xxxxxx Xxxxxxxxx";

arrItems[14] =

"Xxxxxx Xxxxxxxxxx Xxxxxx";

// loop through pageIndexes

foreach (var pageIndex in pageIndexes)

{

// Create Heading object

Aspose.Pdf.

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

TextSegment segment2 = new TextSegment();

heading2.TocPage = tocPage;

heading2.Segments.Add(segment2);

// Specify the destination page for heading object

heading2.DestinationPage = pdfDocument.Pages[pageIndex.Key + 1];

// Destination page

heading2.Top = pdfDocument.Pages[pageIndex.Key + 1].Rect.Height;

// Destination coordinate

segment2.Text = arrItems[(pageIndex.Key -1)];

// format

heading2.Margin.Top = 5;

// Add heading to page containing TOC

tocPage.Paragraphs.Add(heading2);

}

pdfDocument.Save(destPath);

}

Hi Dan,


Thanks for our feedback. I have tested the scenario with your source code and Aspose.Pdf for .NET 8.6.0, but I am afraid I am unable to notice the issue. Please find enclosed sample output. Please do let me know if there is any difference in your requirements and my understanding.

Please feel free to contact us for any further assistance.

Best Regards,

Hello,

Sorry to be a pest, I've just downloaded the attachment that you uploaded. I can see the error. I've attached a screenshot showing it.

Just for my sanity, TOC_Test.pdf is the pdf you added isn't it?

Thanks again,

Dan

My colleague just suggested that maybe you are using a different pdf viewer. What are you using to test it on?

Hi Dan,


My apologies, I have also noticed the TOC elements alignment issue and logged it as PDFNEWNET-36123 for further investigation and resolution. We will notify you via this forum thread as soon as its resolved.

We are sorry for the inconvenience caused.

Best Regards,

Hello again, while you are looking into the TOC maybe you could see why the following happens as well. When you click a hyperlink in the TOC I noticed that it isn't aligned with the element. I think if you add more padding between each entry it gets more noticeable.

Perhaps it is applying the padding to the text but not the hyperlink?

Anyway a screenshot is attached for your consideration.

Hi Dan,


Thanks for sharing your findings. We have shared it with our development team also. Definitely they will also consider your findings in investigation and resolution of the issue. We will notify you as soon as it is resolved.

We are sorry for the inconvenience caused.

Best Regards,

The issues you have found earlier (filed as PDFNEWNET-36123) have been fixed in Aspose.Pdf for .NET 8.8.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.