C# PDF Bookmark Library
How to add, get, update, remove PDF bookmark & outline using c# in wpf, winform, asp.net
Empower Your C# Project with Rapid PDF Internal Navigation Via Bookmark and Outline in .NET Program
In this tutorial, you learn how to manage PDF bookmarks using C#.
- Get PDF bookmarks
- Add new PDF bookmark
- Update PDF bookmark
- Remove bookmarks
- Create PDF from Word with bookmarks
- Split PDF document by bookmarks
How to manage PDF bookmarks using C#
- An advanced PDF SDK able to edit PDF bookmark in Visual C# .NET console application
- Free trial library and components for navigate PDF with bookmark in C# .NET WinForms and ASP.NET program
- Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
- C# demo code for editing PDF bookmarks in Visual Studio .NET class
- Help to add or insert bookmark and outline into PDF file in .NET framework
- Ability to remove and delete bookmark and outline from PDF document
- Merge and split PDF file with bookmark
- Save PDF file with bookmark open
RasterEdge XDoc.PDF SDK package provides PDF file navigation features for your C# project. On this C# tutorial, you will learn how to retrieve, edit and update PDF bookmark and outline.
Read, get PDF bookmarks using C#
Please directly copy and paste C# demo code below to your project to retrieve PDF document bookmarks.
String inputFilePath = Program.RootPath + "\\" + "2.pdf";
PDFDocument doc = new PDFDocument(inputFilePath);
REOutline outline = doc.GetOutline();
foreach (REEntry entry in outline.Entry)
{
Console.WriteLine("Page: " + entry.GetPageIndex());
Console.WriteLine("Level: " + entry.GetLevel());
Console.WriteLine("Position: " + entry.GetLocation());
Console.WriteLine("Text: " + entry.GetText());
}
Add PDF bookmarks using c#
String inputFilePath = Program.RootPath + "\\" + "2.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output_Outline.pdf";
PDFDocument doc = new PDFDocument(inputFilePath);
// initial a new outline object
REOutline outline = new REOutline();
// create 1st entry: Level = 1; Location: page index 0, y = 50
OutLine entryChapter1 = new OutLine("Chapter 1: *******", 1, 0, 50);
// add 1st entry to outline
outline.Entry.Add(entryChapter1);
// create 1st entry's child entries
// create 1st child entry: Level = 2; Location: page index 1, y = 0
OutLine entryChapter11 = new OutLine("###### #### #### #### ###", 2, 1, 0);
// connect this entry to the parent entry
entryChapter11.SetParentREEntry(entryChapter1);
// add 1st child of 1st entry to outline
outline.Entry.Add(entryChapter11);
// create 2nd child entry: Level = 2; Location: page index 2, y = 0
OutLine entryChapter12 = new OutLine("## ## ## ## ## #### ###", 2, 2, 0);
// connect this entry to the parent entry
entryChapter12.SetParentREEntry(entryChapter1);
// add 2nd child of 1st entry to outline
outline.Entry.Add(entryChapter12);
// create 2nd entry: Level = 1; Location: page index 3, y = 100
OutLine entryChapter2 = new OutLine("Chapter 2: ******* ****", 1, 3, 100);
// add 2nd entry to outline
outline.Entry.Add(entryChapter2);
// create 3rd entry: Level = 1; Location: page index 5, y = 200
OutLine entryChapter3 = new OutLine("Chapter 3: **** **** ***", 1, 5, 200);
// add 3rd entry to outline
outline.Entry.Add(entryChapter3);
// create 3rd entry's child entries
// create 1st child entry: Level = 2; Location: page index 9, y = 0
OutLine entryChapter31 = new OutLine("# #### ###", 2, 9, 0);
// connect this entry to the parent entry
entryChapter31.SetParentREEntry(entryChapter3);
// add 1st child of 3rd entry to outline
outline.Entry.Add(entryChapter31);
// create a child entry for the 1st child entry of the 3rd entry
// create entry: Level = 3; Location: page index 9, y = 500
OutLine entryChapter311 = new OutLine("???? ???? ??? ???", 3, 9, 500);
// connect this entry to the parent entry
entryChapter311.SetParentREEntry(entryChapter31);
// add 1st child of 3rd entry to outline
outline.Entry.Add(entryChapter311);
// create 4th entry: Level = 1; Location: page index 10, y = 0
OutLine entryChapter4 = new OutLine("Chapter 4: ******* ** ** **** ***", 1, 10, 0);
// add 4th entry to outline
outline.Entry.Add(entryChapter4);
// create 5th entry: Level = 1; Location: page index 20, y = 0
OutLine entryChapter5 = new OutLine("Chapter 5: * ** **** **** ****", 1, 20, 0);
// add 5th entry to outline
outline.Entry.Add(entryChapter5);
// update the new outline
doc.SetOutline(outline);
doc.Save(outputFilePath);
Remove bookmarks from PDF document in C# application
To remove, delete all bookmarks from a PDF document, you call method SetOutline(null).
String inputFilePath = Program.RootPath + "\\" + "2.pdf";
String outputFilePath = Program.RootPath + "\\" + "Output_Outline.pdf";
PDFDocument doc = new PDFDocument(inputFilePath);
// remove all bookmarks
doc.SetOutline(null);
doc.Save(outputFilePath);
Create PDF from Word document with bookmarks using C# in ASP.NET, Windows app
When you convert Microsoft Word (.docx) to PDF file, you can generate PDF bookmarks from Word document heading content or bookmarks.
There are four options for OutlineMode:
- None: Do not create any outline entry.
- ByOutlineLevel: Create outline entries by paragraph's outline level.
- ByHeading: Create outline entries by paragraph's style IDs (heading styles).
- ByBookmark: Create outline entries by bookmarks.
You can view more features to convert Word to PDF file using C#.
String inputFilePath = @"C:\1.docx");
String outputFilePath = @"C:\output.pdf");
DOCXDocument doc = new DOCXDocument(inputFilePath);
// Set strategy used to create outline entries for the document.
// Default: OutlineMode.ByOutlineLevel
doc.OutlineSetting.Mode = OutlineMode.ByHeading;
// Set the maximum level of entires in the outline.
// Valid range: 1 ~ 9; default value: 3
doc.OutlineSetting.MaxLevel = 2;
// Convert DOCX document to PDF file.
doc.ConvertToDocument(DocumentType.PDF, outputFilePath);
Split PDF file by bookmark using C# in ASP.NET, Windows application
When you separate a PDF document into multiple files, you can split PDF by bookmarks. You will also get more methods to
split PDF document using C# here.
#region split pdf file by top level bookmarks
internal static void splitPdfFileByBookmarks()
{
String inputFilePath = @"C:\2.pdf";
// set split option
SplitOptions options = new SplitOptions(SplitMode.ByBookMark);
// set output option
SplitOutputOptions outputOps = new SplitOutputOptions();
outputOps.OutputFolder = @"C:\output\";
outputOps.Mode = 2;
outputOps.Label = @"Part";
outputOps.Separator = '_';
// split a PDF file with options
PDFDocument.SplitDocument(inputFilePath, options, outputOps);
}
#endregion