We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table of Contents placement

i am creating a TOC as I merge PDF’s together. I want to have the TOC start half way down the page but can’t figure out how to do so.
static private void TOCCreationTest()
{
GetAsposeLic();

        var TOC_Doc = new Aspose.Pdf.Document(@"F:\Template.pdf");
        var files = Directory.GetFiles(@"F:\pdf test\", "*.pdf");

        //add TOC to existing DOc
        var tocPage = TOC_Doc.Pages[1];

        // Create object to represent TOC information
        TocInfo tocInfo = new TocInfo();
        Aspose.Pdf.Text.TextFragment title = new Aspose.Pdf.Text.TextFragment("Table Of Contents");
        title.TextState.FontSize = 22;
        title.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
        var h = tocPage.PageInfo.Height;
        double yIndent = (tocPage.PageInfo.Height / 4) * 3;
        title.Position.YIndent = yIndent;
        // Set the title for TOC
        tocInfo.Title = title;
        tocInfo.FormatArrayLength = 3;
        LevelFormat lf = new LevelFormat();
        lf.Margin = new MarginInfo(0, 0, 0, yIndent);

        tocPage.TocInfo = tocInfo;
        OutlineItemCollection pdfOutline = new OutlineItemCollection(TOC_Doc.Outlines);
        pdfOutline.Title = "Table of Contents";
        pdfOutline.Italic = true;
        pdfOutline.Bold = true;
        

        //set the destination page number
        pdfOutline.Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(tocPage);

        //add bookmark in the document's outline collection.
        TOC_Doc.Outlines.Add(pdfOutline);

        foreach (var file in files)
        {
            var doc = new Aspose.Pdf.Document(file);
            int pageNum = TOC_Doc.Pages.Count +1;
            TOC_Doc.Pages.Add(doc.Pages);
            pdfOutline = new OutlineItemCollection(TOC_Doc.Outlines);
            pdfOutline.Title = Path.GetFileName(file);
            pdfOutline.Italic = true;
            pdfOutline.Bold = true;

            //set the destination page number
            pdfOutline.Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(TOC_Doc.Pages[pageNum]);

            //add bookmark in the document's outline collection.
            TOC_Doc.Outlines.Add(pdfOutline);

            //add TOC
            Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);
            Aspose.Pdf.Text.TextSegment segment2 = new Aspose.Pdf.Text.TextSegment();
            heading2.TocPage = tocPage;
            heading2.Segments.Add(segment2);

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

            // Destination page
            heading2.Top = TOC_Doc.Pages[pageNum].Rect.Height;

            // Destination coordinate
            segment2.Text = pdfOutline.Title;

            // Add heading to page containing TOC
            tocPage.Paragraphs.Add(heading2);
            
        }
        TOC_Doc.Save(@"F:\TOC_Test.pdf");

    }

@BrianMiller

Thank you for contacting support.

You may modify the space from where you want to initiate table of contents, by setting top margin of the page.

        ...

        // Create object to represent TOC information
        TocInfo tocInfo = new TocInfo();

        PdfFileInfo info = new PdfFileInfo(TOC_Doc);
        tocPage.PageInfo.Margin.Top = info.GetPageHeight(tocPage.Number)/2;
        
        Aspose.Pdf.Text.TextFragment title = new Aspose.Pdf.Text.TextFragment("Table Of Contents");

        ...

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

That works well to place the TOC. Thank you.

I am finding that my TOC wants to be bigger than a single page. How do i get it to span pages?

so it looks like if i add a page it will span correctly but using an existing page it is not. I can handle that.

@BrianMiller

Thank you for your kind feedback.

We are glad to know suggested solution satisfies your requirements. Please feel free to contact us if you need any further assistance.