为了在 PDF 文件中添加书签,我们将使用 Aspose.PDF for .NET API,这是一款适用于 net 平台的功能丰富、功能强大且易于使用的文档操作 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以在软件包管理器控制台中使用以下命令。
PM > Install-Package Aspose.PDF
通过 C# 使用书签的步骤
你需要 Aspose.PDF for .NET 才能在你的环境中试用代码。
1.使用文档对象打开 PDF 文档。
1.创建书签并定义其属性。
1.将 OutlineItemCollection 集合添加到 OutlineC
1.再次保存该文件
<% bookmarks.code-block.text %>
在 PDF 文档中添加书签-C#。
Document pdfDocument = new Document(dataDir + "AddBookmark.pdf");
// Create a bookmark object
OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfOutline.Title = "Test Outline";
pdfOutline.Italic = true;
pdfOutline.Bold = true;
// Set the destination page number
pdfOutline.Action = new GoToAction(pdfDocument.Pages[1]);
// Add bookmark in the document's outline collection.
pdfDocument.Outlines.Add(pdfOutline);
dataDir = dataDir + "AddBookmark_out.pdf";
// Save output
pdfDocument.Save(dataDir);