How to rename PDF bookmarks in a Word to PDF converted document?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can rename PDF bookmarks in a Word to PDF converted document using C#.
You can achieve this by using the PdfBookmark.Title API in the Essential® PDF library after converting the Word document to PDF.
Steps to rename PDF bookmarks in a Word to PDF converted document:
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIORenderer.NET.Core NuGet package as a reference to your .NET Core application from NuGet.org.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
- Use the following code example to rename PDF bookmarks in a Word to PDF converted document.
C#
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open))
{
//Loads an existing Word document.
using (WordDocument wordDocument = new WordDocument(fileStream, Syncfusion.DocIO.FormatType.Automatic))
{
//Creates an instance of DocIORenderer.
using (DocIORenderer renderer = new DocIORenderer())
{
//Sets ExportBookmarks for preserving Word document headings as PDF bookmarks.
renderer.Settings.ExportBookmarks = ExportBookmarkType.Bookmarks;
//Converts Word document into PDF document
using (PdfDocument pdfDocument = renderer.ConvertToPDF(wordDocument))
{
//Saves the PDF document to MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
pdfDocument.Save(stream);
stream.Position = 0;
//Load the PDF document.
using (PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(stream))
{
//Get each bookmark and change the title of the bookmark.
for (int i = 0; i < pdfLoadedDocument.Bookmarks.Count; i++)
{
pdfLoadedDocument.Bookmarks[i].Title = "PdfBookMark" + (i + 1);
}
//Saves the PDF file to the file system.
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.pdf"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
pdfLoadedDocument.Save(outputStream);
}
}
}
}
}
}
}
You can download a complete working sample to rename PDF bookmarks in a Word to PDF converted document from GitHub.
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to rename PDF bookmarks in a Word to PDF converted document in a .NET Core Word document.
You can refer to our ASP.NET Core DocIO 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 DocIO 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!