How to Move and Reorder PDF Bookmarks in C# .NET Core?
Syncfusion Essential® PDF is a comprehensive, high‑performance .NET PDF library designed for creating, reading, and editing PDF documents. It also provides APIs to move and reorder bookmarks programmatically, allowing improved document structure and more efficient navigation.
Steps to move and reorder bookmark in a PDF programmatically
- Create a new project: Start a new Console application in .NET for your PDF bookmark manipulation.
- Install required packages: Add the Syncfusion.Pdf.Net.Core NuGet package as a reference in the console application from Nuget.org.
- Set up the environment: In the
Program.csfile, include the following namespaces.
using Syncfusion.Drawing;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
- Implement main logic: Use the provided code sample in
Program.csto manage and reorder bookmarks within a PDF document.
// Load the PDF document
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Data/Input.pdf"))
{
PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
// Reorder: move bookmark from index
if (bookmarks.Count > 2)
MoveBookmark(bookmarks, 2, 0, loadedDocument);
// Remove bookmark by title.
RemoveBookmarkByTitle(bookmarks, "Bookmark To Remove");
loadedDocument.Save("Output.pdf");
}
- Utilize bookmark movement helper: Employ a helper method designed to accurately move a bookmark from one position to another.
// Moves a bookmark from one index to another within its parent collection.
static void MoveBookmark(PdfBookmarkBase parentCollection, int fromIndex, int toIndex, PdfLoadedDocument document)
{
if (fromIndex == toIndex || fromIndex < 0 || fromIndex >= parentCollection.Count)
return;
PdfLoadedBookmark sourceBookmark = parentCollection[fromIndex] as PdfLoadedBookmark;
if (sourceBookmark == null)
return;
// Store bookmark details.
string title = sourceBookmark.Title;
PdfTextStyle textStyle = sourceBookmark.TextStyle;
PdfColor color = sourceBookmark.Color;
PdfDestination destination = sourceBookmark.Destination ?? new PdfDestination(document.Pages[0]);
List<PdfBookmark> children = new List<PdfBookmark>();
foreach (PdfBookmark child in sourceBookmark)
children.Add(child);
// Remove from original position.
parentCollection.RemoveAt(fromIndex);
// Adjust target index.
int adjustedIndex = toIndex > fromIndex ? toIndex - 1 : toIndex;
adjustedIndex = Math.Max(0, Math.Min(adjustedIndex, parentCollection.Count));
// Insert at new position.
var movedBookmark = parentCollection.Insert(adjustedIndex, title);
movedBookmark.TextStyle = textStyle;
movedBookmark.Color = color;
movedBookmark.Destination = destination;
// Re-add children.
foreach (var child in children)
AddBookmark(movedBookmark, child, document);
}
- Utilize bookmark cloning helper: Implement a helper method to effectively clone a bookmark, including all its nested descendants, and attach it to a specified parent.
// Clones an existing bookmark (including all descendants) and adds it to a parent.
static void AddBookmark(PdfBookmark parent, PdfBookmark sourceBookmark, PdfLoadedDocument document)
{
if (parent == null || sourceBookmark == null)
return;
PdfBookmark newBookmark = parent.Insert(parent.Count, sourceBookmark.Title);
newBookmark.TextStyle = sourceBookmark.TextStyle;
newBookmark.Color = sourceBookmark.Color;
newBookmark.Destination = sourceBookmark.Destination ?? new PdfDestination(document.Pages[0]);
foreach (PdfBookmark child in sourceBookmark)
AddBookmark(newBookmark, child, document);
}
- Utilize bookmark removal helper: Use a helper method to recursively locate and remove the first occurrence of a bookmark by its title from any parent collection.
// Removes the first occurrence of a bookmark with the specified title from a parent (searches recursively).
static void RemoveBookmarkByTitle(PdfBookmarkBase parent, string title)
{
if (parent == null || string.IsNullOrWhiteSpace(title))
return;
for (int i = parent.Count - 1; i >= 0; i--)
{
PdfBookmark bookmark = parent[i] as PdfBookmark;
if (bookmark != null)
{
RemoveBookmarkByTitle(bookmark, title);
if (bookmark.Title == title)
parent.RemoveAt(i);
}
}
}
A complete working sample is available for download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation to learn how to add, modify and remove bookmark in a PDF document.
Conclusion
I hope you enjoyed learning how to move and reorder bookmarks in a PDF document.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!