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.
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.
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
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.
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.
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";
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.