How to get OutlineItemCollection.Action targetPage

Note: I figured this one out and don't need help on it anymore. It seems Facades is the best way to perform actions on the PDF for these sorts of things.

I am leaving the original issue below because I think the documentation does not clearly express the difference between Pdf and Facades.

__________________________________________________________________________.

I have tried a number of ways to get the targetPage out of a bookmark.

This link is your example on how to add a bookmark:

// Open document
Document pdfDocument = new Document("input.pdf");
// Create a bookmark object
OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfOutline.Title = "Test Outline";
pdfOutline.Italic = true;
pdfOutline.Bold = true;
// Set the destination page number
pdfOutline.Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(pdfDocument.Pages[1]);
// Add bookmark in the document's outline collection.
pdfDocument.Outlines.Add(pdfOutline);
// Save output
pdfDocument.Save("output.pdf");

This link is your example on how to get a bookmark:

// Open document
Document pdfDocument = new Document("input.pdf");
// Loop through all the bookmarks
foreach (OutlineItemCollection outlineItem in pdfDocument.Outlines)
{
Console.WriteLine(outlineItem.Title);
Console.WriteLine(outlineItem.Italic);
Console.WriteLine(outlineItem.Bold);
Console.WriteLine(outlineItem.Color);
}

Here is a thread where your support answered how to get the Page of a GoToAction:

Document pdfDocument = new Document(myDir+"test.pdf");

int pageNumber1 = pdfDocument.Destinations.GetPageNumber("UntitledDestination", false);


The problem I am encountering is the pdfDocument.Destinations is null even after I added the bookmark. I am using the same Document in all methods. If I look at the OutlineItemsCollection, the Action is there. I can use the debugger to inspect it and see the page, but I cannot reference the page in my code. The only property of Action is Next.

Also, I am confused between Pdf, Pdf Facades and PdfGenerator. Can you point me to something which explains their differences?

Thanks.


jeffmatt:
Note: I figured this one out and don’t need help on it anymore. It seems Facades is the best way to perform actions on the PDF for these sorts of things.
Hi Jeff,

Thanks for using our API’s. We are glad to hear that your problem. Furthermore, in order to get target page associated with Bookmark, please try using following code snippet based on new Document Object Model (DOM).

[C#]
string strDocDir = @“C:\pdftest”;
string strDocName = “Input_Bookmarked.pdf”;
string strFilePath = strDocDir + strDocName;

//Create PdfBookmarkEditor
PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor();

//Ppen PDF file
bookmarkEditor.BindPdf(strFilePath);

//Extract bookmarks
Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks();
foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks)
{
string strLevelSeprator = string.Empty;
for (int i = 1; i < bookmark.Level; i++)
{
strLevelSeprator += “----”;
}

Console.WriteLine(<span class="code-quote" style="color: rgb(0, 145, 0); background-color: inherit;">"{0}Title: {1}"</span>, strLevelSeprator, bookmark.Title);
Console.WriteLine(<span class="code-quote" style="color: rgb(0, 145, 0); background-color: inherit;">"{0}Page <span class="code-object" style="color: rgb(145, 0, 145); background-color: inherit;">Number</span>: {1}"</span>, strLevelSeprator, bookmark.PageNumber);
Console.WriteLine(<span class="code-quote" style="color: rgb(0, 145, 0); background-color: inherit;">"{0}Page Action: {1}"</span>, strLevelSeprator, bookmark.Action);

}



jeffmatt:
Also, I am confused between Pdf, Pdf Facades and PdfGenerator. Can you point me to something which explains their differences?
Aspose.Pdf.Generator is legacy approach for creating PDF documents from scratch and Aspose.Pdf.Facades also also a legacy approach for manipulating existing PDF files. However Aspose.Pdf is new Document Object Model for PDF file creation as well as manipulation. Should you have any further query, please feel free to contact.
You stated:
Aspose.Pdf.Facades also also a legacy approach for manipulating existing PDF files.

But your example above is:
//Extract bookmarks
Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks();
foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks)
{
.....

}

Aspose.Pdf does not have a Bookmarks collection like Facades does. I tried unsuccessfully to get the Bookmark's pageNumber using the Pdf DOM.

Here was my issue trying the DOM approach:
The problem I am encountering is the pdfDocument.Destinations is null even after I added the bookmark. I am using the same Document in all methods. If I look at the OutlineItemsCollection, the Action is there. I can use the debugger to inspect it and see the page, but I cannot reference the page in my code. The only property of Action is Next.
This brings me full circle. Should I use Facades, or am I supposed to use DOM? If I am supposed to use DOM, how do I use it to get to the bookmark's pageNumber?






Hi Jeff,


Thanks for sharing the details.

Yes you are correct. Currently Aspose.Pdf.Facades is returning correct results when trying to retrieve page number for bookmark and when using Aspose.Pdf namespace for the retrieval of page number, its returning invalid value.

Currently you may consider using Aspose.Pdf.Facades approach and meanwhile we are working on optimizing Aspose.Pdf to cater such scenarios, effectively.

[C#]

// Open document<o:p></o:p>

Document pdfDocument = new Document("c:/pdftest/BookmarkTest.pdf");

// Loop through all the bookmarks

foreach (OutlineItemCollection outlineItem in pdfDocument.Outlines)

{

Console.WriteLine(outlineItem.Title);

Console.WriteLine(pdfDocument.Destinations.GetPageNumber(outlineItem.Title,false));

}

Hi Jeff,


I have also logged an issue PDFNEWNET-40071 in our issue tracking system that when creating Bookmark and retrieving page number associated with that bookmark, an exception is being generated. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

[C#]

// Open document<o:p></o:p>

Document pdfDocument = new Document();

pdfDocument.Pages.Add();

// Create a bookmark object

OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);

pdfOutline.Title = "Test Outline";

pdfOutline.Italic = true;

pdfOutline.Bold = true;

// Set the destination page number

pdfOutline.Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(pdfDocument.Pages[1]);

// Add bookmark in the document's outline collection.

pdfDocument.Outlines.Add(pdfOutline);

MemoryStream ms = new MemoryStream();

// Save output

pdfDocument.Save(ms);

// Open document

Document pdfDocument1 = new Document(ms);

// Loop through all the bookmarks

foreach (OutlineItemCollection outlineItem in pdfDocument1.Outlines)

{

Console.WriteLine(outlineItem.Title);

Console.WriteLine(pdfDocument.Destinations.GetPageNumber(outlineItem.Title,false));

}

ms.Close();

No problem.

Frankly, I like the Facades approach because it is not so generic. I do realize that Facades is being deprecated; however, please consider that Facades' use of the syntax "Bookmark" is a lot more descriptive than "OutlineItem." I would hope that PDF could be just as specific when it is fixed, even if that means that Doc.Bookmarks is the same as Doc.GetOutlineItems(OutlineItemTypes OutlineItemType.Bookmark). It's nice to be able to get a specific type of item with ease.

Maybe it is designed to do that already.

Thank you all for being so helpful. I really appreciate it.

Hi Jeff,


Thanks for sharing the feedback. Yes referring to bookmark items with name Doc.Bookmarks is more understandable as compared to use OutlineItems. But in order to change the name, it requires a significant amount of work and this change might impact many areas in API. However your concerns have been shared with product team and we will internally review this request and in case there are any plans of opting for this approach, we will let you know.

The issues you have found earlier (filed as PDFNET-40071) have been fixed in Aspose.PDF for .NET 22.5.