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

Free Support Forum - aspose.com

How to copy bookmark and its data to new PDF

Hello,


I am getting started using Aspose.Pdf and was wondering if you could help me by providing a code snippet. I’ve opened an existing PDF, and would like to copy a specific Bookmark and all of it’s data into a brand new PDF. How can I accomplish this?

Thank you!

Hi Eric,


Thanks for contacting support.

Please visit the following link for required information on Import and Export Bookmarks. The code snippet in shared link shows the steps to import the bookmark in existing PDF file and in case you need to import it into new PDF document, you can create a document from scratch using Document instance, add page object to pages collection of PDF file, pass this object to PdfBookmarkEditor class and then import the bookmarks inside it.

[C#]

//
create Document instance
<o:p></o:p>

Document doc = new Document();<o:p></o:p>

//
add page to PDF file
<o:p></o:p>

doc.Pages.Add();<o:p></o:p>

<o:p> </o:p>

//
Create PdfBookmarkEditor class
<o:p></o:p>

Aspose.Pdf.Facades.PdfBookmarkEditor
bookmarkEditor =
new Aspose.Pdf.Facades.PdfBookmarkEditor();<o:p></o:p>

//
Open PDF file
<o:p></o:p>

bookmarkEditor.BindPdf(doc);<o:p></o:p>

//
Import bookmarks
<o:p></o:p>

bookmarkEditor.ImportBookmarksWithXML(dataDir

  • “bookmarks.xml”);<o:p></o:p>

    //
    Save updated PDF file
    <o:p></o:p>

bookmarkEditor.Save(dataDir + "ImportFromXML_out.pdf");

Thank you Nayyer!


Is there a way that I can use bookmarkEditor.ImportBookmarksWithXML() to only import a specific Bookmark? Can I export a single Bookmark somehow with ExportBookmarksToXML() ?

And perhaps I should explain the scope of my project, maybe it will help.


I’d like to open up a PDF file, search for a few different Bookmark Title’s, and copy just those titles along with the data within the bookmarks, into a new PDF file.

I’ve gotten a loop working to find the bookmark’s I want, I just don’t know how to get them into the new PDF.

I am all set on this, I was able to get my code working. The piece I was missing was while looping through the Aspose.Pdf.Facades.Bookmark collection, I had to use the PageNumber and Destination to access the Aspose.Pdf.Page to add it to the new Aspose.Pdf.Document.

Hi Eric,


Thanks for sharing the details.

Apart from earlier shared approach of exporting Bookmarks to XML format, you can traverse through the bookmarks collection and get information related to specific bookmark. The Bookmarks are fetched in OutlineItemCollection and you can access specific object. For more information, please visit Get Bookmarks from a PDF Document.

Once you have the information, you may follow the instructions specified over following link on how to Add a Bookmark to a PDF Document.

Should you have any further query, please feel free to contact.
eforden:
I am all set on this, I was able to get my code working. The piece I was missing was while looping through the Aspose.Pdf.Facades.Bookmark collection, I had to use the PageNumber and Destination to access the Aspose.Pdf.Page to add it to the new Aspose.Pdf.Document.
Hi Eric,

Thanks for sharing the details.

You may either consider using Aspose.Pdf.Facades or use the newest approach of Aspose.Pdf.Document shared in my earlier post.