How could I get the correct page number of each heading?

Hi there ,


I am using Aspose.pdf 11.3.

I have got a problem with getting the correct page number of each heading.

For example, I have a page with many headings. The page number is 5,

after the pdf created, the headings could be on page 5,6,7, but from the api, I could only see the

page number is still 5.

Because I need to show them on TOC.

Is there any way to do that?

Thank you!

Hi Patrick,


Thanks for using our API’s.

Can you please share the code snippet which you are using and the problematic PDF documents generated at your end, so that we can test the scenario in our environment. We are sorry for your inconvenience.

Hi Nayyer,
I got the code here:

public static void Create()
{
Document pdfDoc = new Document();
pdfDoc.PageInfo.Width = 612.0;
pdfDoc.PageInfo.Height = 792.0;
pdfDoc.PageInfo.Margin = new Aspose.Pdf.MarginInfo();
pdfDoc.PageInfo.Margin.Left = 72;
pdfDoc.PageInfo.Margin.Right = 72;
pdfDoc.PageInfo.Margin.Top = 72;
pdfDoc.PageInfo.Margin.Bottom = 72;
Aspose.Pdf.Page pdfPage = pdfDoc.Pages.Add();
pdfPage.PageInfo.Width = 612.0;
pdfPage.PageInfo.Height = 792.0;
pdfPage.PageInfo.Margin = new Aspose.Pdf.MarginInfo();
pdfPage.PageInfo.Margin.Left = 72;
pdfPage.PageInfo.Margin.Right = 72;
pdfPage.PageInfo.Margin.Top = 72;
pdfPage.PageInfo.Margin.Bottom = 72;
var contentPage = pdfDoc.Pages.Add();
//For toc
Aspose.Pdf.FloatingBox floatBox = new Aspose.Pdf.FloatingBox();
floatBox.Margin = pdfPage.PageInfo.Margin;
pdfPage.Paragraphs.Add(floatBox);
var margin = new MarginInfo();
margin.Top = 20;
margin.Left = 200;
var heading = new TextFragment();
//heading.IsInList = true;
//heading.StartNumber = 1;
heading.Text = “List 1”;
//heading.Style = NumberingStyle.NumeralsArabic;
heading.HorizontalAlignment = HorizontalAlignment.FullJustify;
//heading.IsAutoSequence = true;
heading.Margin = margin;
floatBox.Paragraphs.Add(heading);
var headingNumber = new TextFragment((contentPage.Number).ToString(CultureInfo.InvariantCulture));
headingNumber.TextState.HorizontalAlignment = HorizontalAlignment.Right;
headingNumber.Margin.Top = 20;
headingNumber.IsInLineParagraph = true;
floatBox.Paragraphs.Add(headingNumber);
var heading2 = new TextFragment();
//heading2.IsInList = true;
//heading2.StartNumber = 13;
heading2.Text = “List 2”;
//heading2.Style = NumberingStyle.NumeralsArabic;
heading2.HorizontalAlignment = HorizontalAlignment.FullJustify;
//heading2.IsAutoSequence = true;
heading2.Margin = margin;
floatBox.Paragraphs.Add(heading2);
var headingNumber2 = new TextFragment((contentPage.Number).ToString(CultureInfo.InvariantCulture));
headingNumber2.TextState.HorizontalAlignment = HorizontalAlignment.Right;
headingNumber2.Margin.Top = 20;
headingNumber2.IsInLineParagraph = true;
floatBox.Paragraphs.Add(headingNumber2);
var heading3 = new TextFragment();
//heading3.IsInList = true;
//heading3.StartNumber = 1;
heading3.Text = “List 3”;
//heading3.Style = NumberingStyle.NumeralsArabic;
heading3.HorizontalAlignment = HorizontalAlignment.FullJustify;
//heading3.IsAutoSequence = true;
heading3.Margin = margin;
floatBox.Paragraphs.Add(heading3);
var headingNumber3 = new TextFragment((contentPage.Number).ToString(CultureInfo.InvariantCulture));
headingNumber3.TextState.HorizontalAlignment = HorizontalAlignment.Right;
headingNumber3.Margin.Top = 20;
headingNumber3.IsInLineParagraph = true;
floatBox.Paragraphs.Add(headingNumber3);
//For content
var content = @“Aspose.Pdf for .NET is a PDF document creation and manipulation component that enables your .NET applications to read, write and manipulate existing PDF documents without using Adobe Acrobat. It also allows you to create forms and manage form fields embedded in a PDF document.
Aspose.Pdf for .NET is affordable and offers an incredible wealth of features including PDF compression options; table creation and manipulation; support for graph objects; extensive hyperlink functionality; extended security controls; custom font handling; integration with data sources; add or remove bookmarks; create table of contents; add, update, delete attachments and annotations; import or export PDF form data; add, replace or remove text and images; split, concatenate, extract or inset pages; transform pages to image; print PDF documents and much more.
Aspose.Pdf for .NET supports the creation of PDF files directly through its API or via XML templates and is incredibly simple to use. The product comes with fully featured demos written in C# and Visual Basic and comprehensive documentation to help get you started. Aspose.Pdf for .NET is a sophisticated product and integrates perfectly with your application to add PDF capabilities.”;
var text = new TextFragment();
text.Text = content;
text.Margin.Top = 200;
var text2 = new TextFragment();
text2.Text = content;
text2.Margin.Top = 200;
var text3 = new TextFragment();
text3.Text = content;
text3.Margin.Top = 200;
contentPage.Paragraphs.Add(heading);
contentPage.Paragraphs.Add(text);
contentPage.Paragraphs.Add(heading2);
contentPage.Paragraphs.Add(text2);
contentPage.Paragraphs.Add(heading3);
contentPage.Paragraphs.Add(text3);
pdfDoc.Save(“c:/pdftest/PageNumber.pdf”);
}

In the pdf, you could see that the heading “List 3” is on the page 3, but in the toc it’s still page2, how could I get the page number ?

Cheers

Hi Patrick,


Thanks for sharing your sample code. Please note when we add large contents on same Page object then API adjusts contents between pages automatically, populate a page first and moves extra contents to next page. As per my understanding you wants to add different contents on different pages, then you can force API for it by using different page objects for differnt contents as following. Hopefully it will resolve the issue.

…<o:p></o:p>

var contentPage = pdfDoc.Pages.Add();

contentPage.Paragraphs.Add(heading);

contentPage.Paragraphs.Add(text);

contentPage = pdfDoc.Pages.Add();

contentPage.Paragraphs.Add(heading2);

contentPage.Paragraphs.Add(text2);

contentPage = pdfDoc.Pages.Add();

contentPage.Paragraphs.Add(heading3);

contentPage.Paragraphs.Add(text3);

.....

Best Regards,

Hi Tilal


Thanks for your solution.

However, in my cases I have to put all the contents one by one. As my content always changes sometimes the page will leave a big blank block if I start a new page for each content.

Is there any api or solution to get the page number of the extra contents?


Regards

Hi Patrick,


Thanks for your feedback. I am afraid while we are creating a document by scratch then we can not get page number of text contents.

However you may break your task in two steps. Initially create and save a new PDF document, and also keep the list of the heading text. Later search the heading text form existing PDF document and use page number from searched TextFragments to create TOC in second step. Hopefully it will help you to accomplish the task.

Best Regards,