PDF管理控件Aspose.PDF for .Net使用教程(二十九):添加和删除PDF中的书签

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

>>Aspose.PDF for .NET更新至最新版v20.2,欢迎下载体验。


将书签添加到PDF文档

书签保存在Document对象的OutlineItemCollection集合中,而对象本身也在OutlineCollection集合中。要将书签添加到PDF:

  1. 使用Document对象打开PDF文档。
  2. 创建一个书签并定义其属性。
  3. 将OutlineItemCollection集合添加到Outlines集合中。

以下代码段显示了如何在PDF文档中添加书签。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();// Open documentDocument pdfDocument = new Document(dataDir + "AddBookmark.pdf");// Create a bookmark objectOutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);pdfOutline.Title = "Test Outline";pdfOutline.Italic = true;pdfOutline.Bold = true;// Set the destination page numberpdfOutline.Action = new GoToAction(pdfDocument.Pages[1]);// Add bookmark in the document's outline collection.pdfDocument.Outlines.Add(pdfOutline);dataDir = dataDir + "AddBookmark_out.pdf";// Save outputpdfDocument.Save(dataDir);

将子书签添加到PDF文档

  1. 打开一个文档。
  2. 将书签添加到OutlineItemCollection,以定义其属性。
  3. 将添加OutlineItemCollection到Document对象的OutlineCollection集合。

就像上面解释的父书签一样,创建了子书签,但是将其添加到了父书签的Outlines集合中。

以下代码段显示了如何将子书签添加到PDF文档。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();// Open documentDocument pdfDocument = new Document(dataDir + "AddChildBookmark.pdf");// Create a parent bookmark objectOutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);pdfOutline.Title = "Parent Outline";pdfOutline.Italic = true;pdfOutline.Bold = true;   // Create a child bookmark objectOutlineItemCollection pdfChildOutline = new OutlineItemCollection(pdfDocument.Outlines);pdfChildOutline.Title = "Child Outline";pdfChildOutline.Italic = true;pdfChildOutline.Bold = true;// Add child bookmark in parent bookmark's collectionpdfOutline.Add(pdfChildOutline);// Add parent bookmark in the document's outline collection.pdfDocument.Outlines.Add(pdfOutline);    dataDir = dataDir + "AddChildBookmark_out.pdf";// Save outputpdfDocument.Save(dataDir);

删除PDF文档中的所有书签

  1. 调用OutlineCollection集合的Delete方法。
  2. 使用Document对象的Save方法保存修改后的文件。

以下代码段显示了如何从PDF文档中删除所有书签。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();// Open documentDocument pdfDocument = new Document(dataDir + "DeleteAllBookmarks.pdf");// Delete all bookmarkspdfDocument.Outlines.Delete();dataDir = dataDir + "DeleteAllBookmarks_out.pdf";// Save updated filepdfDocument.Save(dataDir);

从PDF文档中删除特定的书签

从PDF文档删除所有附件显示了如何从PDF文件删除所有附件。也可以仅删除特定的附件。要从PDF文件中删除特定的书签:

  1. 将书签的标题作为参数传递给OutlineCollection集合的Delete方法。
  2. 使用Document对象Save方法保存更新的文件。

该Document班提供的OutlineCollection集合。该Delete}方法删除的标题传递给方法的任何书签。

以下代码段显示了如何从PDF文档中删除特定的书签。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks();// Open documentDocument pdfDocument = new Document(dataDir + "DeleteParticularBookmark.pdf");// Delete particular outline by TitlepdfDocument.Outlines.Delete("Child Outline");dataDir = dataDir + "DeleteParticularBookmark_out.pdf";// Save updated filepdfDocument.Save(dataDir);

还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年1月6日
下一篇 2020年1月6日

相关推荐

发表回复

登录后才能评论