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

Free Support Forum - aspose.com

Location of LinkAnnotation for custom TOC

Hi,

I am trying to create custom TOC with Named Destination links. But the position of just created TextFragment doesn’t correspond to real position when document is created. In the attached code LinkAnnotation takes location from TextFragment and it appears in the bottom of the page. Where can I get correct location for LinkAnnotation?

Thank you

Here the sample code

    public static void Run()
    {
        string dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

        Document doc = new Document();

        var page = doc.Pages.Add();

        var tocTable = new Table
        {
            ColumnWidths = "90% 10%",
            DefaultCellPadding = new MarginInfo(0, 2f, 0, 2f)
        };

        // Article 1
        Row row1 = tocTable.Rows.Add();
        Cell titleCell1 = row1.Cells.Add();
        TextFragment article1 = new TextFragment("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
        titleCell1.Paragraphs.Add(article1);

        LinkAnnotation link1 = new LinkAnnotation(page, article1.Rectangle);
        link1.Color = Color.Green;
        link1.Action = new GoToAction(doc, "page1");
        page.Annotations.Add(link1);

        // Article 2
        Row row2 = tocTable.Rows.Add();
        Cell titleCell2 = row2.Cells.Add();
        TextFragment article2 =
            new TextFragment(
                "Vestibulum tincidunt sem non aliquam aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit");
        titleCell2.Paragraphs.Add(article2);

        LinkAnnotation link2 = new LinkAnnotation(page, article2.Rectangle);
        link2.Color = Color.Green;
        link2.Action = new GoToAction(doc, "page3");
        page.Annotations.Add(link2);

        page.Paragraphs.Add(tocTable);

        for (int i = 1; i <= 3; i++)
        {
            var page1 = doc.Pages.Add();
            page1.Paragraphs.Add(new TextFragment("Page " + i));
        }

        doc.NamedDestinations.Add("page1", new FitExplicitDestination(doc.Pages[2]));
        doc.NamedDestinations.Add("page3", new FitExplicitDestination(doc.Pages[3]));

        dataDir = dataDir + "CustomToC.pdf";
        doc.Save(dataDir);
    }

@gmatv

We suggest you please read the following article.
Working with Actions in PDF

If you still face problem, please share your expected output PDF file here for our reference. We will then provide you more information about your query.