Bookmarks getting directed to the wrong page

Hi,

I have an issue where a generated PDF document contains a rotated document. The bookmarks are directed to the page after the rotated document in Chrome but directed to the correct page when opened in Adobe Acrobat.

I have added a sample PDF below.

I’m wondering if you have come across this before and have a fix?

BundlewithBrokenBookmark.pdf (192.5 KB)

Thanks,

@cian.hartigan,

can you share the code snippet how are you generating this PDF?

@cian.hartigan,

I tested it with Opera, Edge, and Firefox; it goes to the middle of the page. So that’s why I want to know how you generate the document.

It seems like an odd behavior from Chrome, as it works differently than three other major browsers.

Hi Carlos,

Thanks for your reply.
Here is a snippet to recreate issue in Chrome with a rotated doc. I have attached
the example docs I Used also:
Docs.zip (108.1 KB)

            var firstPdf = new Document(@"Doc 1.pdf");
            var secondPdf = new Document(@"Doc 2.pdf");
            var rotatedPdf = new Document(@"RotatedDoc.pdf");
            var thirdPdf = new Document(@"Preformatted textDoc 3.pdf");
            var docsToAdd = new Document[] { firstPdf, secondPdf, rotatedPdf, thirdPdf };

            for (int i = 1; i <= docsToAdd.Length; i++)
            {
                Aspose.Pdf.OutlineItemCollection bm = new Aspose.Pdf.OutlineItemCollection(brief.Outlines);
                brief.Pages.Add(docsToAdd[i - 1].Pages[1]);

                if (brief.Pages[i].Rotate == Rotation.None)
                    bm.Destination = new XYZExplicitDestination(i, 0, brief.Pages[i].MediaBox.Height, 0.0); 
                else if (brief.Pages[i].Rotate == Rotation.on270)
                    bm.Destination = new XYZExplicitDestination(i, brief.Pages[i].MediaBox.Width, 0, 0.0); 
                else
                {
                    bm.Destination = new GoToAction(brief.Pages[i]);
                }

                bm.Title = docsToAdd[i - 1].FileName;

                brief.Outlines.Add(bm);
            }

            brief.Save("Brief.pdf");

@cian.hartigan,

I made the following code with your files:

private void Logic()
{
    Document doc = new Document();

    Document doc1 = new Document($"{PartialPath}_input_1.pdf");
    Document doc2 = new Document($"{PartialPath}_input_2.pdf");            
    Document doc3 = new Document($"{PartialPath}_input_rotated.pdf");
    Document doc4 = new Document($"{PartialPath}_input_3.pdf");

    var docList = new List<Document> { doc1, doc2, doc3, doc4 };

    Page tocPage = doc.Pages.Add();
    tocPage.TocInfo = new TocInfo();

    int countDoc = 0;
    int countPage = 0;
    foreach (var docToMerge in docList)
    {
        countDoc++;
        foreach (var pageToAdd in docToMerge.Pages)
        {
            countPage++;

            var page = doc.Pages.Add(pageToAdd);
            if (countDoc == 3)
            {                        
                page.Rotate = Rotation.on180;
            }                   
            
            page.AddStamp(new PageNumberStamp());                    

            tocPage.Paragraphs.Add(new Heading(1)
            {
                TocPage = tocPage,
                DestinationPage = page,
                Text = $"Doc: {countDoc} - page: {countPage}",
                Margin = new MarginInfo(0, 20, 0, 20)
            });
        }
    }

    doc.Save($"{PartialPath}_output.pdf");            
}

The output:
BookmarkGoesToWrongPageInChrome_output.pdf (111.2 KB)

I tested it in Chrome and it works fine. Can you try modifying your code a bit? Let me know if it works.

Hi Thanks for that.

Unfortunately it is a requirement to get the bookmarks themselves working(Screenshot below). We have functionality in place that deals with the Table of content hyperlinks already.

I have had a look through the forum and also found this issue which seems to be related to mine:

image.png (13.3 KB)

@cian.hartigan,

You are right, while coding the solution I got sidetracked and thought it was about a TOC.

Here is the same code but I added the bookmarks. Keep in mind I tested the output in the Google Chrome and the bookmarks work fine.

private void Logic()
{
    Document doc = new Document();

    Document doc1 = new Document($"{PartialPath}_input_1.pdf");
    Document doc2 = new Document($"{PartialPath}_input_2.pdf");            
    Document doc3 = new Document($"{PartialPath}_input_rotated.pdf");
    Document doc4 = new Document($"{PartialPath}_input_3.pdf");

    var docList = new List<Document> { doc1, doc2, doc3, doc4 };

    Page tocPage = doc.Pages.Add();
    tocPage.TocInfo = new TocInfo();

    int countDoc = 0;
    int countPage = 0;

    doc.Outlines.Clear();    
    
    foreach (var docToMerge in docList)
    {
        countDoc++;
        foreach (var pageToAdd in docToMerge.Pages)
        {
            countPage++;

            var page = doc.Pages.Add(pageToAdd);
            if (countDoc == 3)
            {                        
                page.Rotate = Rotation.on180;
            }                   
            
            page.AddStamp(new PageNumberStamp());

            string textTitle = $"Doc: {countDoc} - page: {countPage}";
            tocPage.Paragraphs.Add(new Heading(1)
            {
                TocPage = tocPage,
                DestinationPage = page,
                Text = textTitle,
                Margin = new MarginInfo(0, 20, 0, 20)
            });

            OutlineItemCollection pdfOutline = new OutlineItemCollection(doc.Outlines);
            pdfOutline.Title = textTitle;
            pdfOutline.Destination = new GoToAction(page);
            doc.Outlines.Add(pdfOutline);
        }
    }            

    doc.Save($"{PartialPath}_output.pdf");            
}

The output file:
BookmarkGoesToWrongPageInChrome_output.pdf (112.1 KB)

If possible we would like to avoid physically rotating documents as they are appended. This would cause issues for our users.

        if (countDoc == 3)
        {                        
            page.Rotate = Rotation.on180;
        }

@cian.hartigan,

That was to simulate a rotated document. You dont need that at all.

Remember. I am only guiding you, I am not implementing a final solution for your clients.

My job is to help you or guide you do your tasks. You can can code the details you need after you get the ideas or suggestions from my code samples.

Apologies I see what you mean. The document I gave you called rotated.pdf should already be rotated on270.

I uploaded a gif of what I see when I open in chrome vs adobe using the doc you generated from above. Are you seeing the same?

ChromeVsAdobe.gif (577.1 KB)

@cian.hartigan,

I can clearly see the problem. EDIT: This is not a bug. See the solution below.

Excuse me, would you tell me what tool are you using to create the gif? Only if you do not mind sharing it.

Thanks Carlos,

I used https://www.screentogif.com/

@cian.hartigan,

I figure it out why was not working on the browsers. I was missing the action.

Please test my latest code and let me know.

private void Logic()
{
    Document doc = new Document();

    Document doc1 = new Document($"{PartialPath}_input_1.pdf");
    Document doc2 = new Document($"{PartialPath}_input_2.pdf");            
    Document doc3 = new Document($"{PartialPath}_input_rotated.pdf");
    Document doc4 = new Document($"{PartialPath}_input_3.pdf");

    var docList = new List<Document> { doc1, doc2, doc3, doc4 };

    Page tocPage = doc.Pages.Add();
    tocPage.TocInfo = new TocInfo();

    int countDoc = 0;
    int countPage = 0;

    doc.Outlines.Clear();

    OutlineItemCollection rootOutline = new OutlineItemCollection(doc.Outlines);            
    rootOutline.Title = "Table of Content";
    rootOutline.Destination = new GoToAction(tocPage);
    rootOutline.Action = new GoToAction(tocPage);
    doc.Outlines.Add(rootOutline);


    foreach (var docToMerge in docList)
    {
        countDoc++;
        foreach (var pageToAdd in docToMerge.Pages)
        {
            countPage++;

            var page = doc.Pages.Add(pageToAdd);                
            
            page.AddStamp(new PageNumberStamp());

            string textTitle = $"Doc: {countDoc} - page: {countPage}";
            tocPage.Paragraphs.Add(new Heading(1)
            {
                TocPage = tocPage,
                DestinationPage = page,
                Text = textTitle,
                Margin = new MarginInfo(0, 20, 0, 20)
            });

            OutlineItemCollection pdfOutline = new OutlineItemCollection(doc.Outlines);
            pdfOutline.Title = textTitle;
            pdfOutline.Destination = new GoToAction(page);
            pdfOutline.Action = new GoToAction(page);
            doc.Outlines.Add(pdfOutline);
        }
    }            

    doc.Save($"{PartialPath}_output.pdf");            
}

The output file:
BookmarkGoesToWrongPageInChrome_output.pdf (112.4 KB)

Hi Carlos,

that looks to have fixed the issue. Thanks