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.

Mon Mar 20, 2023 5:40 am

Hello,

I have a pdf document generated by Spire Office version 8.2.2. This document is generated with a hand built Table of Contents that generates annotations to other pages within the document using the following code:

Code: Select all
private void CreatePdfAnnotations(PdfDocument tocPdf, Dictionary<PdfLabelValueElement, KeyValuePair<PdfPageBase, float>> pageNumLocations, int startingPageNumberIndex)
        {
            int pageNumberIndex = startingPageNumberIndex;
            for (int i = 0; i < pageNumLocations.Count; i++)
            {
                // Get both our current TOC item and next TOC item information
                var currentPageNumItem = pageNumLocations.ElementAtOrDefault(i);
                var nextPageNumItem = pageNumLocations.ElementAtOrDefault((i + 1));

                if (currentPageNumItem.Key == null)
                    continue;

                // Declare variables based on keyvalupair values to make the next block more readable
                // Page num needs to converted from string to int
                int.TryParse(currentPageNumItem.Key.Value.Text, out int currentPageNum);

                var page = currentPageNumItem.Value.Key;
                float yOffset = currentPageNumItem.Value.Value;

                // Create go to action for hyperlinks
                var itemBounds = new RectangleF(new PointF(0f, yOffset), new SizeF(page.Canvas.ClientSize.Width, currentPageNumItem.Key.Label.Height));

                var dest = new PdfDestination(pageNumberIndex + tocPdf.Pages.Count - 1, new PointF(page.Canvas.ClientSize.Width / 2, page.Canvas.Size.Height), 0);
                var action = new PdfActionAnnotation(itemBounds, new PdfGoToAction(dest));
                action.Border = new PdfAnnotationBorder(0);
                (page as PdfNewPage).Annotations.Add(action);

                // Determine the next TOC items page number and re-calculate the page number index by retrieving the difference between the next item's page number and the current item page number
                if (nextPageNumItem.Key != null) {
                    int.TryParse(nextPageNumItem.Key.Value.Text, out int nextPageNum);
                    pageNumberIndex += (nextPageNum - currentPageNum);
                }

            }
        }


When I download the resulting PDF everything works as expected. However when I view the PDF in Adobe Reader and perform a Save As function, to save it to a new location or change the file name, the annotations built from the code block above in the document appear to be removed in the resulting document.

I'm assuming this is an issue with Spire as bookmarks/annotations created via Adobe Reader appear to be maintained after performing a Save As function.

Please refer to the attached PDF's as a demonstration of the change in metadata after having saved via Adobe.

Please let me know as soon as you can what the solution to this may be.

Thanks,
Zac

ZCliff92
 
Posts: 30
Joined: Thu Jan 20, 2022 1:58 am

Tue Mar 21, 2023 11:13 am

Hi Zac,

Thanks for your feedback.
After testing, we were unable to reproduce the issue you experienced. Our demo successfully added a table of contents using our code, saved the file, opened it in Adobe Reader, and then saved it again without any issues with the table of contents links being lost.
Could you please provide us with your executable project to help us reproduce your issue? you can send it to us via email (support@e-iceblue.com) or attach them here. Thanks for your assistance.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Jun 20, 2023 12:29 am

Hi Triste,

Apologies for taking so long to get back to you about this issue.

I have a test application attached to this message that successfully reproduces the issue I started this post with.

Please open the solution an run the program and you should see a pdf document get created in the same folder that the code lives in called final.pdf.

The test here is that when opening that document, if you then Save As using Adobe Reader to the same location under a different name (for example finalv2.pdf), the links/annotations in the table of contents no longer work compared to the original version.

Feel free to investigate the code in the solution and let me know if the way we are building the Table of Contents is causing this issue or if this is something else going on.

Thank you,
Zac

ZCliff92
 
Posts: 30
Joined: Thu Jan 20, 2022 1:58 am

Tue Jun 20, 2023 4:58 am

Hi,

Thanks for your feedback.
After testing your project, I have reproduced this issue, the links are lost. I have logged this issue into our issue tracking system with the ticket number SPIREPDF-6081. Our developers will investigate and fix it. Sorry for the inconvenience caused. Once the issue is fixed, I will inform you asap.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Sun Jun 25, 2023 9:55 am

Hi,

Thanks for your patience.
Our dev team has determined that this is because the "NeedAppearances" entry in the form dictionary of your document's form is set to "true." When you close the document in Adobe, it prompts you to save modifications which regenerates the appearance stream and causes the Link annotations on the first page to lose their actions, resulting in broken links.

We suggest setting "doc.Form.NeedAppearances = false;" which will prevent Adobe from prompting you to save modifications when you close the document. In this way, the generated document will not need to regenerate the appearance stream, and the links will remain valid.

I’m writing to ask you about whether it is necessary to save the document again after closing without being prompted. Please be aware that Adobe's "Save As" function has its own document operations, which we may not be able to control beyond adjusting the source document settings.

Thank you for bringing this issue to our attention, and please let us know if you have any further questions or concerns.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Jun 26, 2023 5:21 am

Hi Triste,

We tried the "doc.Form.NeedAppearanes = false" solution you mentioned and unfortunately this did not solve our issue, the links were still broken. Something we noticed, however, when going through your example code for generating a Table of Contents is that we can reliably reproduce the same issue with your example code. I'll explain:

We created a small test application using your example code specified here (https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Document-Operation/How-to-create-table-of-contents-on-PDF-in-C.html.).

When generating the PDFDestination object in the example code, it uses a PdfPage reference as the destination.
workingTOC.png


If you change this to use the 'i' variable from the for loop representing the page number instead, save the document, then re-open that document in Adobe and perform the Save As function with a new name, the TOC links no longer work.
brokenTOC.png


This means the issue here seems to lie within the PDFDestination object and how it identifies or defines its destination based on either a page reference or a page number. Unfortunately the way we are generating our PDF documents using Spire, we do not have a page reference at the time of creating the Table of Contents and we need the page number for our reference instead.

Please investigate this for us and let me know if you arrive at the same conclusion. I am interested to see the outcome.

ZCliff92
 
Posts: 30
Joined: Thu Jan 20, 2022 1:58 am

Mon Jun 26, 2023 11:51 am

Hi Zac,

I did notice this issue you mentioned, I will report this issue and the former issue to our developers. I have given the two issues the highest priority and urge our dev team to solve them asap. Please rest assured that our developers will try their best to solve them asap. Apologize for any inconvenience caused and thank you for your valuable feedback.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Jun 26, 2023 10:48 pm

Hi Triste,

I appreciate the urgency on this matter. Please inform me as soon as possible when you have an update on when we can expect this issue to be resolved.

Thank you,
Zac

ZCliff92
 
Posts: 30
Joined: Thu Jan 20, 2022 1:58 am

Wed Jun 28, 2023 6:15 am

Hi Zac,

Thanks for your patience.
After reviewing the concerns raised, our development team advises against using the page index when creating a PdfDestination, as this method may have some issues and will be deprecated in future updates. Instead, we suggest using the linked page object (PdfPageBase) as a parameter when creating the PdfDestination.
Thank you for your understanding and cooperation. If you have any further questions or concerns, just feel free to contact us.

Best regards,
Trite
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF