Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Oct 25, 2022 2:36 pm

Hi,

I am trying PdfGoToAction in table of contents. I have below table of contents and the pdf document has 9 pages.

Content1..........................1
Content2..........................2
Content3..........................3
Content4..........................5
Content5..........................6

1. When I click 1 and 2, it jumps to the correct pages. But when I click 3, it jumps to page 4.
2. When I click 5, it jumps to the correct page - 5
2. There is no hyperlink when I hover over the text 6.

This is how I bring the table of contents
--------------------------------------------------
1. The pdf report is first generated with 7 pages.
2. I Insert the table of content (2 pages) after page 1 in the document.

This is the code I am using for goto page which is having issue.
-----------------------------------------------------------------------------
Code: Select all
Dim tocPage As PdfNewPage = doc.Pages.Insert(i, page.Size) '' inserting a new page for table of content (2 pages in a loop)
                        tocPage.Canvas.DrawImage(image, 0, y + 17, 10.0F, 10.0F)
                        tocPage.Canvas.DrawString(Text, textFont, New PdfSolidBrush(New PdfRGBColor(67, 103, 155)), x + 20, y + 13)
                        tocPage.Canvas.DrawString(pageNumText, textFont, New PdfSolidBrush(New PdfRGBColor(67, 103, 155)), pageNumlocation, y + 13)

                        For j As Integer = dotLocation To pageNumlocation

                            If (dotLocation >= pageNumlocation) Then
                                Exit For
                            End If

                            tocPage.Canvas.DrawString(".", textFont, PdfBrushes.Black, dotLocation, y + 13)
                            dotLocation += 8
                        Next

                        Dim location = New PointF(0, y)
                        Dim titleBounds = New RectangleF(location, New SizeF(tocPage.Canvas.ClientSize.Width, titleSize.Height))
                        Dim Dest = New PdfDestination(doc.Pages(pp)) ''pp is the pagenumber
                        Dim Action = New PdfActionAnnotation(titleBounds, New PdfGoToAction(Dest))
                        Action.Border = New PdfAnnotationBorder(0)
                        tocPage.Annotations.Add(Action)



I tried doc.AfterOpenAction method which lands on the correct page. But the above code does not seem to work.

Please provide your expertise to solve this issue. Your help is much appreciated.

Thanks
Chitra

chitraotech_123
 
Posts: 3
Joined: Tue Sep 06, 2022 3:43 pm

Wed Oct 26, 2022 10:18 am

Hello,

Thanks for your inquiry.
Now, the latest version of Spire.Pdf is V8.10.5, if you don’t use it, please update to it and test again your issue. If the issue still exist, to help us reproduce your issue and work out a solution for you, please offer the following message, thanks for your assistance in advance.
1) Your Pdf document before adding TOC and the Pdf document after adding TOC, you can attach them here or send them to us vis email ([email protected]).
2) Your full test code that can reproduce your issue.
3) Application type, such as Console App, .NET Framework 4.8.
4) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Wed Oct 26, 2022 3:39 pm

Hi,

Can I upgrade to the latest version V8.10.5 using the existing License?

Thanks
Chitra

chitraotech_123
 
Posts: 3
Joined: Tue Sep 06, 2022 3:43 pm

Thu Oct 27, 2022 2:05 am

Hello,

Thank for your feedback.
According to your purchase information, your license has expired on July 5, 2022. Please note that our license is based on one year subscription. The 1-year subscription enables free priority support and updates for one year. After the subscription expired, you can still use the product and deploy to your applications, only the hot fixes and new releases is no longer available.

The latest Spire.Pdf is released on October 19, 2022, so your license is invalid for it. However, we are willing to provide a temporary license (one month free) to help you evaluate the latest version of our provided. If you need it, please contact our sales team ([email protected]).

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Fri Oct 28, 2022 9:06 am

Hello,

Thanks for your feedback.
I did reproduce your issue using your code to test. After investigation, you need modify your code to achieve the effect you want:
1) Modify the location where the PdfActionAnnotation is drawn to match the loation of “pagetitle”: change “location = New PointF(0, y)” to “location = New PointF(0, y + 13)”.
2) Change “Dim Dest = New PdfDestination(doc.Pages(pp))” to “Dim Dest = New PdfDestination(doc.Pages(pp + 1))”, due to when adding a new page containing TOC, the index of the other pages after the new page is increased by 1.
I put the complete code below for your reference. If you have any issue, just feel free to contact us.

Code: Select all
 ''load the BeforeTOC.pdf file
        Dim bb = File.ReadAllBytes("../../data/BeforeTOC.pdf")

        Dim ms As New MemoryStream()
        Dim dtoc As New List(Of DataRow)

        Dim dt As New DataTable()
        dt.Columns.Add("PageTitle")
        dt.Columns.Add("PageNumber")
        dt.Columns.Add("IsParentSectionTitle")

        Dim r1 As DataRow = dt.NewRow
        r1("PageTitle") = "Uniform Crime Reporting Program"
        r1("PageNumber") = "1"
        r1("IsParentSectionTitle") = "1"
        dt.Rows.Add(r1)

        Dim r2 As DataRow = dt.NewRow
        r2("PageTitle") = "Contributing & Non-Contributing Agencies"
        r2("PageNumber") = "2"
        r2("IsParentSectionTitle") = "1"
        dt.Rows.Add(r2)

        Dim r3 As DataRow = dt.NewRow
        r3("PageTitle") = "Law Enforcement Personnel Counts"
        r3("PageNumber") = "3"
        r3("IsParentSectionTitle") = "1"
        dt.Rows.Add(r3)

        Dim r4 As DataRow = dt.NewRow
        r4("PageTitle") = "Law Enforcement Killed in the Line of Duty"
        r4("PageNumber") = "5"
        r4("IsParentSectionTitle") = "1"
        dt.Rows.Add(r4)

        Dim r5 As DataRow = dt.NewRow
        r5("PageTitle") = "Alaska Population Figures"
        r5("PageNumber") = "6"
        r5("IsParentSectionTitle") = "1"
        dt.Rows.Add(r5)

        dtoc = dt.AsEnumerable().ToList()

        Dim tocList As New Dictionary(Of Integer, List(Of DataRow))
        Dim doc As New PdfDocument()
        doc.LoadFromBytes(bb)

        '' split the table of content entries for 2 pages
        tocList.Add(1, dtoc.Take(30).ToList())
        tocList.Add(2, dtoc.Skip(30).Take(dtoc.Count - 30).ToList())

        Dim pageCount = doc.Pages.Count
        Dim page As PdfPageBase = doc.Pages(0)
        Dim image As PdfImage = PdfImage.FromFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../data/OIP-C.jfif"))

        For i As Integer = 1 To tocList.Count '' number of pages to insert for table of contents

            Dim tocPage As PdfNewPage = doc.Pages.Insert(i, page.Size)

            Dim grid As New PdfGrid()
            grid.Style.BackgroundBrush = PdfBrushes.White
            grid.Columns.Add(1)
            grid.Columns(0).Width = Convert.ToSingle("500", System.Globalization.CultureInfo.InvariantCulture)

            Dim title As String = "Contents"
            Dim titleFont = New PdfTrueTypeFont(New Font("FRABK", 16, FontStyle.Bold))
            Dim centerAlignment = New PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle)
            Dim location = New PointF(grid.Columns(0).Width / 2, titleFont.MeasureString(title).Height)
            tocPage.Canvas.DrawString(title, titleFont, PdfBrushes.Black, New PointF(0, titleFont.MeasureString(title).Height - 5), centerAlignment)

            Dim textFont = New PdfTrueTypeFont(New Font("FRABK", 11, FontStyle.Regular))
            location = New PointF(grid.Columns(0).Width / 2, textFont.MeasureString(title).Height)
            Dim y = textFont.MeasureString(title).Height + 7
            Dim x = 0

            For Each row As DataRow In tocList(i)

                Try

                    Dim pp As Integer
                    Dim spage = row("PageNumber").ToString().Split("-")
                    pp = spage(0)

                    Dim Text = row("PageTitle").ToString()
                    Dim titleSize = textFont.MeasureString(Text)
                    Dim navigatedPage As PdfPageBase = doc.Pages(pp)



                    ''Dim pageNumText = (pp + 1).ToString()
                    Dim pageNumText = (pp).ToString()
                    Dim pageNumlocation = 495
                    Dim dotLocation = titleSize.Width + 20 + x

                    If (row("IsParentSectionTitle") = "1") Then

                        If (pageNumText.Length = 1) Then
                            pageNumlocation = 506
                        ElseIf (pageNumText.Length = 2) Then
                            pageNumlocation = 499
                        Else
                            pageNumlocation = 490
                        End If

                        textFont = New PdfTrueTypeFont(New Font("FRABK", 14, FontStyle.Regular))
                        titleSize = textFont.MeasureString(Text)
                        tocPage.Canvas.DrawImage(image, 0, y + 4, 10.0F, 10.0F)
                        tocPage.Canvas.DrawString(Text, textFont, New PdfSolidBrush(New PdfRGBColor(67, 103, 155)), x + 20, y + 13)
                        tocPage.Canvas.DrawString(pageNumText, textFont, New PdfSolidBrush(New PdfRGBColor(67, 103, 155)), pageNumlocation, y + 13)
                        dotLocation = titleSize.Width + 20 + x
                    Else

                        If (pageNumText.Length = 1) Then
                            pageNumlocation = 506
                        ElseIf (pageNumText.Length = 2) Then
                            pageNumlocation = 501
                        End If

                        textFont = New PdfTrueTypeFont(New Font("FRABK", 11, FontStyle.Regular))
                        titleSize = textFont.MeasureString(Text)
                        tocPage.Canvas.DrawString(Text, textFont, PdfBrushes.Black, x + 20, y + 13)
                        tocPage.Canvas.DrawString(pageNumText, textFont, PdfBrushes.Black, pageNumlocation, y + 13)
                    End If

                    For j As Integer = dotLocation To pageNumlocation

                        If (dotLocation >= pageNumlocation) Then
                            Exit For
                        End If

                        tocPage.Canvas.DrawString(".", textFont, PdfBrushes.Black, dotLocation, y + 13)
                        dotLocation += 8
                    Next

                    location = New PointF(0, y + 13)
                    Dim titleBounds = New RectangleF(location, New SizeF(tocPage.Canvas.ClientSize.Width, titleSize.Height))
                    ''Dim Dest = New PdfDestination(navigatedPage, New PointF(-doc.PageSettings.Margins.Top, -doc.PageSettings.Margins.Left))
                    ''Dim Dest = New PdfDestination(pp, New PointF(doc.PageSettings.Margins.Top, doc.PageSettings.Margins.Left), 1)
                    Dim Dest = New PdfDestination(doc.Pages(pp + 1))
                    Dim Action = New PdfActionAnnotation(titleBounds, New PdfGoToAction(Dest))
                    Action.Border = New PdfAnnotationBorder(0)
                    tocPage.Annotations.Add(Action)

                    ''Dim destination = New PdfDestination(doc.Pages(3))
                    ''Dim act = New PdfGoToAction(destination)
                    ''act.Destination.Zoom = 0.8F
                    ''doc.AfterOpenAction = act


                    y += titleSize.Height + 7

                Catch ex As Exception
                    Throw ex
                End Try

            Next

        Next



Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF