Free Support Forum - aspose.com

TOC starting with number 1 always

Hello,

I am using following sample code. In which I set start number to be 2 , but it is showing number 1 in TOC, I have attached Output pdf for refernce.

Hello Sameer,


Thanks for contacting support.

Could you please a sample code snippet which you are using to add TOC inside PDF document, which you have shared. This way we can test the scenario in our environment and get back to you accordingly.

Best Regards,

Hello Asad,
Please have look ata following code

Document doc =new Document();
Page toc = doc.Pages.Add();
TocInfo info=new TocInfo();
info.FormatArrayLength = 3;
toc.TocInfo=info;

        Page testPage = doc.Pages.Add();
        Heading testHeading = new Heading(1)
        {
            Text = "Hello",
            //UserLabel = new TextSegment("B"),
            StartNumber = 2,
            IsAutoSequence = true,
            TocPage = doc.Pages[1],
            IsInList = true
        };
        
        //testHeading.Style = NumberingStyle.None;
        testPage.Paragraphs.Add(testHeading);
        //toc.Paragraphs.Add(testHeading);

Hi Sameer,


Thanks for sharing code snippet.

I have tried to add TOC inside a PDF document by using your code snippet with Aspose.Pdf for .NET 17.4.0 and was unable to notice any issue. The first heading was started from number, which was set as StartNumber in the code. Please check final code snippet which I have used to generate PDF with TOC. For your reference, I have also attached an output document, generated by below code snippet.

Document doc2 = new Document();<o:p></o:p>

Page toc = doc2.Pages.Add();

TocInfo info = new TocInfo();

info.FormatArrayLength = 3;

toc.TocInfo = info;

Page testPage = doc2.Pages.Add();

Heading testHeading = new Heading(1)

{

Text = "Hello", //UserLabel = new TextSegment("B"),

StartNumber = 3,

IsAutoSequence = true,

TocPage = doc.Pages[1],

IsInList = true

}; //

testHeading.Style = NumberingStyle.NumeralsRomanUppercase;

testPage.Paragraphs.Add(testHeading);

toc.Paragraphs.Add(testHeading);

doc2.Save(dataDir + "TOC2_out.pdf");



Please try using above code snippet with latest version of the API and in a case if you still face any issue, please share more details with us regarding scenario, so that we can try to replicate the issue in our environment and address it accordingly.

Best Regards,

Document doc2 = new Document();



Page toc = doc2.Pages.Add();



TocInfo info = new TocInfo();



info.FormatArrayLength = 3;



toc.TocInfo = info;



Page testPage = doc2.Pages.Add();



Heading testHeading = new Heading(1)



{



Text = “Hello”, //UserLabel = new TextSegment(“B”),



StartNumber = 3,



IsAutoSequence = true,



TocPage = doc.Pages[1],//here error is shown as we have not declared any member with doc in above code. Could you please check



IsInList = true



}; //



testHeading.Style = NumberingStyle.NumeralsRomanUppercase;



testPage.Paragraphs.Add(testHeading);



toc.Paragraphs.Add(testHeading);



doc2.Save(dataDir + “TOC2_out.pdf”);





After replacing doc with doc2 attached output is their.

Hi Sameer,


Thanks for writing back and I apologize for the inconvenience faced. Please check following modified code snippet which worked fine over my end.

Document doc2 = new Document();<o:p></o:p>

Page toc = doc2.Pages.Add();

TocInfo info = new TocInfo();

info.FormatArrayLength = 3;

toc.TocInfo = info;

Page testPage = doc2.Pages.Add();

testPage.Paragraphs.Add(new TextFragment("Hello World"));

Heading testHeading = new Heading(1)

{

Text = "Hello", //UserLabel = new TextSegment("B"),

StartNumber = 3,

IsAutoSequence = true,

TocPage = doc2.Pages[1],

IsInList = true,

DestinationPage = doc2.Pages[2]

}; //

testHeading.Style = NumberingStyle.NumeralsRomanUppercase;

toc.Paragraphs.Add(testHeading);

doc2.Save(dataDir + "TOC2_out.pdf");



The problem which you have faced earlier, was due to adding same TOC heading in second page of the document. However, if you want to add heading in the pages, other than TOC Page, you can do that like following:

Heading testHeading = new Heading(1)

{

Text = "Hello", //UserLabel = new TextSegment("B"),

StartNumber = 3,

IsAutoSequence = true,

TocPage = doc2.Pages[1],

IsInList = true,

DestinationPage = doc2.Pages[2]

}; //

testHeading.Style = NumberingStyle.NumeralsRomanUppercase;

toc.Paragraphs.Add(testHeading);

testPage.Paragraphs.Add(new Heading(1) { Text="Hello" });


In case of any further assistance, please feel free to contact us.


Best Regards,