C# Itext7 Table of Content link to chapter does't work

15 Views Asked by At

I'm trying to create a simple pdf with a table of content that navigates to the chapters. Here is my code to generate the pdf and table of content.

   static void GeneratePDFCommandExecuted(object obj)
        {
            string filePath = @"C:\Work\output2.pdf";

            using (PdfWriter writer = new PdfWriter(filePath))
            {
                using (PdfDocument pdf = new PdfDocument(writer))
                {
                    Document document = new Document(pdf);

                    // Add chapters to the document
                    AddChapter(document, "Chapter 1", "Content of Chapter 1");
                    AddChapter(document, "Chapter 2", "Content of Chapter 2");
                    AddChapter(document, "Chapter 3", "Content of Chapter 3");

                    // Add the table of contents
                    AddTableOfContents(document, pdf);

                    Console.WriteLine($"PDF created successfully at {filePath}");
                }
            }

        }

        static void AddChapter(Document document, string chapterTitle, string chapterContent)
        {
            // Add chapter title
            document.Add(new Paragraph(chapterTitle).SetFont(PdfFontFactory.CreateFont()));

            // Add chapter content
            document.Add(new Paragraph(chapterContent));

            // Add a new page for the next chapter
            document.Add(new AreaBreak());
        }

        static void AddTableOfContents(Document document, PdfDocument pdf)
        {
            // Create the table of contents page
            pdf.AddNewPage();

            // Add title for the table of contents
            document.Add(new Paragraph("Table of Contents").SetFont(PdfFontFactory.CreateFont()));

            // Add links to chapters
            for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
            {

                PdfPage a = pdf.GetPage(i);
                // Generate a unique name for the destination
                string destinationName = $"chapter_{i}";

                // Add a named destination for each chapter
               //pdf.AddNamedDestination(destinationName, a.GetPdfObject());
                pdf.AddNamedDestination(destinationName, a.GetPdfObject());


                // Add a link to the named destination
                document.Add(CreateChapterLink($"Chapter {i}", destinationName));
            }
        }

        static Paragraph CreateChapterLink(string chapterTitle, string destinationName)
        {
            // Create a clickable link to the specified named destination
        Paragraph link = new Paragraph(chapterTitle)
            .SetFontColor(DeviceRgb.BLUE)
            .SetAction(PdfAction.CreateGoTo(destinationName));
            return link;
        }

Everithing was correct generated and also the link but i cannot manage to put the link really working and moving the pdf to the correct page.

I already tried on the AddTableOfContents() method using the PdfExplicitDestination.CreateFitH() but without success

Many thanks

0

There are 0 best solutions below