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

Free Support Forum - aspose.com

Updating Bookmark Destination/Action

I build a Word document with Aspose.Words and then create a PDF version from this by saving in PDF format from Aspose.Words. In the Word document I add bookmarks and placeholder pages for PDF format “attachments” that will later be merged into the PDF version - so my Word document and PDF have blank pages where the attachments will later be merged in. Later I use Aspose.PDF to open the PDF and merge in the attachments. I do this by finding the bookmark, deleting the placeholder pages and then inserting the pages from the attachment. I’ve shown the code that I use to do this below. My problem is that I want to update the bookmarks that I use to locate the insertion point for the attachments so that they will continue to work and take the user to the start of the inserted attachment. Unfortunately this does not work - clicking on the attachment bookmark does not navigate anywhere.

            var bookmarkText = GetBookmarkText(attachment, isForSnapshot);
            var bookmark = GetBookmark(pdf, bookmarkText);

            ...

                var filePath = Path.Combine(organisationFolder, attachment.FilePath);
                var attachmentPdfDocument = new Document(filePath);
                var attachmentPageCount = attachmentPdfDocument.Pages.Count;
                var bookmarkPageNo =
                    ((XYZExplicitDestination)(bookmark.Destination)).PageNumber;
                
                // Delete the bookmark before deleting the pages
                //bookmark.Delete(); // this does not remove the bookmark from the outlines collection
                //pdf.Outlines.Delete(bookmarkText); // this gives an error, refer to https://www.aspose.com/community/forums/798011/remove-bookmarks/showthread.aspx#798011

                // Remove the bookmark placeholder pages
                for (var i = 1; i <= attachmentPageCount; i++)
                {
                    pdf.Pages.Delete(bookmarkPageNo);
                }

                pdf.Pages.Insert(bookmarkPageNo, attachmentPdfDocument.Pages);

                // update the bookmark destination page (neither of these work - the bookmark still does not work)
                //bookmark.Action = new GoToAction(pdf.Pages[bookmarkPageNo]);
                //bookmark.Destination = new GoToAction(pdf.Pages[bookmarkPageNo]);
                
                ...

                pdf.Save(pdfPath);

You can see my attempts to update the Action and Destination which do not work. Is this because I delete the pages that relate to the bookmark? You will also see my commented out code where I originally wanted to delete the bookmarks… however that does not work as the bookmark does not get removed from the PDF Outlines. This is why I want to update the bookmark. If I cannot delete it from the Outlines collection then I want it to at least work :slight_smile: How can I do this?

@daveywc,

Thanks for contacting support.

In order to accomplish your requirements, you may simply convert Word file to PDF format, insert pages to PDF file and save the reference of indexes where pages are inserted and then create Bookmarks inside resultant PDF targeting to indexes where pages were added. Please follow the steps specified over Add a Bookmark to a PDF Document.

In case you still face any issue, please share the input PDF file which you are using, so that we can test the scenario in our environment.

Yes I could do what you have suggested, however the reason that I want to update the existing bookmarks is to work around the problem referred to in this forum post. My customers complain because we have bookmarks left in the document that do not work as Aspose has a bug that prevents me from removing them from the outlines collection. Can you please advise how I can update the existing bookmarks - or provide a fix to the bug that prevents me from removing the bookmarks from the outline collection?

@daveywc

Thanks for contacting support.

The issue which have been logged earlier in the forum post, which you have mentioned, is not yet resolved but I would like to inform you that the priority level of the issue has been escalated and its investigation is in progress. Depending upon their development schedule, product team will definitely provide a fix against the issue.

I have associated the issue ID with this forum thread as well, so that you will also receive notification here once the issue is resolved.

Furthermore, I have tried to update an existing bookmark from a sample document, by following code snippet and got success in updating the GoToAction of a bookmark. For your reference, I have also attached input and output file(s) used in the below code snippet.

Document docWithBM = new Document(dataDir + "BookmarksLikeTOC_out.pdf");
docWithBM.Outlines.First.Destination = new GoToAction(docWithBM.Pages[2]);
docWithBM.Save(dataDir + "Updated Bookmarks.pdf");

Updated Bookmarks.pdf (2.9 KB)
BookmarksLikeTOC_out.pdf (2.8 KB)

You may try this approach by modifying the workaround as per your requirement to get desired functionality. As far as the logged issue is concerned, we will definitely let you know once it is resolved. Please be patient and give us little time.

We are sorry for the inconvenience.


Best Regards,
Asad Ali

Hi Asad,

I believe that I am already doing as you have suggested - and it is not working. You can download a sample project which fails to update the bookmark destinations from here.

Kind regards,

@daveywc

Thanks for sharing sample project.

I have tested the scenario by running your project with latest version Aspose.Pdf for .NET 17.7, and observed that bookmarks were fine in the resultant file. For example the bookmark with the name “Att 15.1.1.1 PIRlxpTjEeaCIphf0z-tiA” was navigating to Page Number 7 which was added from the PDF “Grant Tillett QT 154-16.pdf”. Same was the case with other bookmarks.

For your reference, I have attached an output, generated over our end (Output), and an image to display the working of bookmarks as well Working_Bookmarks.gif (517.3 KB)
. Please check the following line of code which was being used while testing the scenario.

bookmark.Destination = new GoToAction(pdf.Pages[bookmarkPageNo]);

In addition, I have also tried to view the file (Ordinary_Council_Meeting After.pdf) which you have included in the shared ZIP Package and noticed that bookmarks were working in that file as well, which seemed to me quite strange :slight_smile: as you have reported that bookmarks were not working in the resultant file.

Would you please share, which PDF viewer are you using to view the PDF document or confirm if I am missing something in testing the scenario. Your response is awaited.


Best Regards,
Asad Ali

Hello Asad,

Thanks for your reply. My default PDF viewer is Foxit Phantom PDF. I had not thought of trying this in a different viewer. When I tried it in Adobe Acrobat Reader the bookmarks worked fine. I then tried it in the PDF Viewer extension for Chrome - the bookmarks did not work there. I also tried it in Microsoft Edge and those attachment bookmarks did not even show up.

So the bottom line is that the bookmarks do work in Adobe Reader - which is good (and may be good enough in the short term) - but they do not seem to work in other PDF viewers. I wonder why this would be the case?

Kind regards,

David

@daveywc

Thanks for writing back.

I have tried to view the generated PDF in PDF Viewers other than Adobe Reader and observed that the bookmarks which were edited/modified, were not working there. Furthermore, I have also observed that bookmarks were not visible at all when I opened document in Microsoft Edge. Please note that, for Microsoft Edge, the issue was already reported in Microsoft forums and can be checked here.

Furthermore, regarding non responsive bookmarks in Foxit and Chrome, I have logged an investigation ticket as PDFNET-43047 in our issue tracking system. Our product team will further look into the details of the issue and as soon as they share some feedback on this investigation, we will definitely update you within this forum thread. Please be patient and spare us little time.

We are sorry for the inconvenience.


Best Regards,
Asad Ali

Hi Asad,

Further to the issue above, which was resolved by updating the bookmark.Destination using the following line of code:

myBookmark.Destination = new GoToAction(pdf.Pages[bookmarkPageNo]);

I have now found a further problem. If, for example, myBookmark was originally nested like:

bookmark-level1
bookmark-level1
=>=>bookmark-level2
=>=>=>=>myBookmark (at level 3)

After I update the destination the bookmark ends up losing it’s place in the collection hierarchy so that it now looks like this:

bookmark-level1
bookmark-level1
=>=>bookmark-level2
myBookmark (now at level 1)

How can I keep the myBookmark’s place in the collection hierarchy after updating it’s destination?

I’ve included more of my code below to give you some better context:

            var attachmentPdfDocument = new Document(filePath);
            var attachmentPageCount = attachmentPdfDocument.Pages.Count;
            var bookmarkPageNo =
                ((XYZExplicitDestination)(myBookmark.Destination)).PageNumber;

            .......
            
            // Remove the bookmark placeholder pages
            for (var i = 1; i <= attachmentPageCount; i++)
            {
                pdf.Pages.Delete(bookmarkPageNo);
            }

            pdf.Pages.Insert(bookmarkPageNo, attachmentPdfDocument.Pages);
            myBookmark.Destination = new GoToAction(pdf.Pages[bookmarkPageNo]);

@daveywc

Thanks for contacting support.

We have tested the scenario in our environment by updating Destination of a child bookmark (Level 3) and were unable to notice the change in its position. However, would you please share your sample PDF document along with complete sample code snippet, which is able to reproduce the mentioned issue. We will again test the scenario in our environment and address it accordingly.

Hi Asad,

@asad.ali

Thanks for your response. In the process of putting together a sample project I discovered that there was an error elsewhere in my logic that was causing this problem. All working now :slight_smile:

@daveywc

Thanks for your feedback.

It is good to know that your issue has been resolved. Please keep using our API and in case of any further assistance, please feel free to contact us.