C#/VB.NET Generate table of contents bookmarks when converting Word to PDF
When we convert Word document to PDF format, we want to keep the title of Word document as PDF bookmark, so what should we do? Then this article will take C# and VB.NET code as an example to introduce how to generate directory bookmarks when converting Word to PDF. The following are the specific methods and steps, I hope to help you.
Programming Envirinment
IIn this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:
Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.
Method 2: Install via NuGet. It can be installed by the following 2 methods:
(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.
(2) Copy the following content to the PM console installation.
Install-Package FreeSpire.Doc -Version 10.2.0
Specific Steps:
- CLoad the document from disk
- Set CreateWordBookmarks to true
- Create bookmarks using Headings
- Create bookmarks using word bookmarks
- Save to PDF
[C#]
using System;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace ToPDFAndCreateBookmarks
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string inputFile = "../../../../../../../Data/BookmarkTemplate.docx";
string outFile = "ToPDFAndCreateBookmarks_out.pdf";
Document document = new Document();
//Load the document from disk
document.LoadFromFile(inputFile);
ToPdfParameterList parames = new ToPdfParameterList();
//Set CreateWordBookmarks to true
parames.CreateWordBookmarks = true;
////Create bookmarks using Headings
//parames.CreateWordBookmarksUsingHeadings = true;
//Create bookmarks using word bookmarks
parames.CreateWordBookmarksUsingHeadings = false;
//Save to PDF
document.SaveToFile(outFile, parames);
WordDocViewer(outFile);
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
}
}[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace ToPDFAndCreateBookmarks
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim inputFile As String = "../../../../../../../Data/BookmarkTemplate.docx"
Dim outFile As String = "ToPDFAndCreateBookmarks_out.pdf"
Dim document As New Document()
'Load the document from disk
document.LoadFromFile(inputFile)
Dim parames As New ToPdfParameterList()
'Set CreateWordBookmarks to true
parames.CreateWordBookmarks = True
'//Create bookmarks using Headings
'parames.CreateWordBookmarksUsingHeadings = true;
'Create bookmarks using word bookmarks
parames.CreateWordBookmarksUsingHeadings = False
'Save to PDF
document.SaveToFile(outFile, parames)
WordDocViewer(outFile)
End Sub
Private Sub WordDocViewer(ByVal fileName As String)
Try
Process.Start(fileName)
Catch
End Try
End Sub
End Class
End NamespaceEffective Shot
Attebtion:The code path is for reference only, and you can customize the path as you want.

Comments
Post a Comment