Aspose.Pdf 19.4.0.0 Bookmark doesn't work for first bookmark .NET

Hello,

I added a few bookmark to a pdf and it does work if I add with GoToAction but it doesn’t when I use XYZExplicitDestination (I want to use this to avoid zoom changes).

So this piece it works for all pages:

        string dataDir = "C:\\Whatever";

        // Open document
        Document pdfDocument = new Document(dataDir + "filename.pdf");

        // Create a parent bookmark object
        OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
        pdfOutline.Action = new GoToAction(pdfDocument.Pages[1]);
        pdfOutline.Title = "Parent Bookmark - Page 1";

        // Create a child bookmark object
        OutlineItemCollection pdfChildOutline = new OutlineItemCollection(pdfDocument.Outlines);
        pdfChildOutline.Title = "Child Outline - Page 1";
        pdfChildOutline.Action = new GoToAction(pdfDocument.Pages[1]);

        OutlineItemCollection pdfChildOutline3 = new OutlineItemCollection(pdfDocument.Outlines);
        pdfChildOutline3.Title = "Child Outline page 3";
        pdfChildOutline3.Action = new GoToAction(pdfDocument.Pages[3]);

        // Add child bookmark in parent bookmark's collection
        pdfOutline.Add(pdfChildOutline);
        pdfOutline.Add(pdfChildOutline3);

        // Add parent bookmark in the document's outline collection.
        pdfDocument.Outlines.Add(pdfOutline);

        dataDir = dataDir + "AddChildBookmark_out.pdf";
        // Save output
        pdfDocument.Save(dataDir);

But this one does’t work for the first bookmarks:

        string dataDir = "C:\\Whatever";

        // Open document
        Document pdfDocument = new Document(dataDir + "Filename.pdf");

        // Create a parent bookmark object
        OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
        pdfOutline.Destination = new XYZExplicitDestination(0, 0, 0, 1); //Sets the inherit zoom for the bookmarks for contents
        pdfOutline.Title = "Parent Bookmark - Page 1";

        // Create a child bookmark object
        OutlineItemCollection pdfChildOutline = new OutlineItemCollection(pdfDocument.Outlines);
        pdfChildOutline.Title = "Child Outline - Page 1";
        pdfChildOutline.Destination = new XYZExplicitDestination(0, 0, 0, 1); //Sets the inherit zoom for the bookmarks for contents

        OutlineItemCollection pdfChildOutline3 = new OutlineItemCollection(pdfDocument.Outlines);
        pdfChildOutline3.Title = "Child Outline page 3";
        pdfChildOutline3.Destination = new XYZExplicitDestination(2, 0, 0, 1); //Sets the inherit zoom for the bookmarks for contents


        // Add child bookmark in parent bookmark's collection
        pdfOutline.Add(pdfChildOutline);
        pdfOutline.Add(pdfChildOutline3);

        // Add parent bookmark in the document's outline collection.
        pdfDocument.Outlines.Add(pdfOutline);

        dataDir = dataDir + "AddChildBookmark_out.pdf";
        // Save output
        pdfDocument.Save(dataDir);

Any idea why? It’s just the first parent and first child , the ones that don’t work.

@cesypozo

It seems like you are using an existing PDF document in your code snippet. Could you please share that with us as well? We will test the scenario in our environment and address it accordingly.

1 Like

Filename.pdf (61.6 KB)

@asad.ali
The pdf could be any. I just want to create a bookmark and set the actions to a page, but didn’t work propertly.

Here are the outputs as well @asad.ali .

  1. Using XYZExplicitDestination.CreateDestination to set the zoom:
    Output_Bookmark_Wrong_But_zoom_OK.pdf (96.4 KB)

  2. Using GoToAction:
    Output_Bookmark_OK_But_zoom_Wrong.pdf (96.5 KB)

The problem regarding XYZExplicitDestination.CreateDestination is also happening for latest version 21.3.0.

Here there is a gif of another test I did. As you can see, the 1st bookmark (set to 1st page) always go to the 2nd page.

Also, the page number change quickly for the other bookmarks the 1st time we click on them
firstBookMarkWrong.gif (350.6 KB)

@cesypozo

Please try to set the XYZExplicitDestination as following and let us know if you still face any issue:

// Create a parent bookmark object
OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfOutline.Destination = new XYZExplicitDestination(1, 0, pdfDocument.Pages[1].MediaBox.Height, 1); //Sets the inherit zoom for the bookmarks for contents
pdfOutline.Title = "Parent Bookmark - Page 1";

// Create a child bookmark object
OutlineItemCollection pdfChildOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfChildOutline.Title = "Child Outline - Page 1";
pdfChildOutline.Destination = new XYZExplicitDestination(1, 0, pdfDocument.Pages[1].MediaBox.Height, 1); //Sets the inherit zoom for the bookmarks for contents

OutlineItemCollection pdfChildOutline3 = new OutlineItemCollection(pdfDocument.Outlines);
pdfChildOutline3.Title = "Child Outline page 3";
pdfChildOutline3.Destination = new XYZExplicitDestination(3, 0, pdfDocument.Pages[3].MediaBox.Height, 1); //Sets the inherit zoom for the bookmarks for contents
2 Likes

Thanks a million @asad.ali ! It solved the problem

1 Like

@cesypozo

We are pleased to know that your issue has been resolved. Please keep using our API and feel free to create a new topic in case you face any issue or need any assistance.

Hello @asad.ali

I don’t know why but I took this again because we are having some issues. I just used the same code you wrote but always the first bookmark (parent and child) it goes to page 2 for a specific document.

The code is:


Document pdfDocument = new Document(dataDir + "FileToTest.pdf");



            // Create a parent bookmark object
            OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
            pdfOutline.Destination = new XYZExplicitDestination(1, 0, pdfDocument.Pages[1].MediaBox.Height, 1); //Sets the inherit zoom for the bookmarks for contents
            pdfOutline.Title = "Parent Bookmark - Page 1";

            // Create a child bookmark object
            OutlineItemCollection pdfChildOutline = new OutlineItemCollection(pdfDocument.Outlines);
            pdfChildOutline.Title = "Child Outline - Page 1";
            pdfChildOutline.Destination = new XYZExplicitDestination(1, 0, pdfDocument.Pages[1].MediaBox.Height, 1); //Sets the inherit zoom for the bookmarks for contents

            OutlineItemCollection pdfChildOutline3 = new OutlineItemCollection(pdfDocument.Outlines);
            pdfChildOutline3.Title = "Child Outline page 2";
            pdfChildOutline3.Destination = new XYZExplicitDestination(2, 0, pdfDocument.Pages[2].MediaBox.Height, 1); //Sets the inherit zoom for the bookmarks for contents


            // Add child bookmark in parent bookmark's collection
            pdfOutline.Add(pdfChildOutline);
            pdfOutline.Add(pdfChildOutline3);

            // Add parent bookmark in the document's outline collection.
            pdfDocument.Outlines.Add(pdfOutline);

            dataDir = dataDir + "AddChildBookmark_out.pdf";
            // Save output
            pdfDocument.Save(dataDir);

The input is FileToTest.pdf (attached) and the output is AddChildBookmark_out.pdf(attached).

Any idea why is this happening for that pdf? THanks.

@cesypozo

The first page is rotated on270 angle which means that the top side of the page is actually at the bottom that is why the bookmark is taking you there. You can check the rotation and use below code to make it work correctly.

// Open document
Document pdfDocument = new Document(dataDir + "FileToTest.pdf");
var rotation = pdfDocument.Pages[1].Rotate;
// Create a parent bookmark object
OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfOutline.Destination = new XYZExplicitDestination(1, pdfDocument.Pages[1].MediaBox.Width, 0, 1); //Sets the inherit zoom for the bookmarks for contents
pdfOutline.Title = "Parent Bookmark - Page 1";

Thanks a million! It got solved :slight_smile:

What should be the way to call XYZExplicitDestination to fix it when the page is rotated on180 and on90 ?

Thanks

@cesypozo

It is quite hard to determine the correct and generic way in case pages are rotated. For example, in your PDF the page rotation was effecting Media Height. In some PDFs, the rotation is implemented on page level whereas, in some it is specified in transformation matrix. We have logged an investigation task as PDFNET-51772 in our issue management system to further check and analyze this case. We will look into its details and let you know as soon as the task is resolved. Please be patient and spare us some time.

We apologize for the inconvenience.

Perfect, thanks!