通过 Java 处理 PDF 文档中的书签

如何使用 Java 以编程方式操作 PDF 中的书签。

如何使用 Java 库在 PDF 文档中使用书签

为了向 PDF 添加书签,我们将使用 Aspose.PDF for Java API,这是一款适用于 Java 平台的功能丰富、功能强大且易于使用的转换 API。你可以直接从 [Maven](https://repository.aspose.com/webapp/ #/artifacts/browse/Tree/General/repo/com/aspose/aspose-PDF)下载其最新版本,然后通过在 pom.xml 中添加以下配置将其安装在基于 Maven 的项目中。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

通过 Java 使用书签的步骤


你需要 Aspose.PDF for Java 才能在你的环境中试用代码。

1.使用文档对象打开 PDF 文档。 1.创建书签并定义其属性。 1.将 OutlineItemCollection 集合添加到 OutlineC 1.再次保存该文件

<% bookmarks.code-block.text %>

在 PDF 文档中添加书签-Java。

<% bookmarks.code-block.subtitle %>


    Document pdfDocument = new Document(GetDataDir() + "AddBookmark.pdf");

    // Create a bookmark object
    OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.getOutlines());
    pdfOutline.setTitle("Test Outline");
    pdfOutline.setItalic(true);
    pdfOutline.setBold(true);

    // Set the destination page number
    pdfOutline.setAction(new GoToAction(pdfDocument.getPages().get_Item(2)));

    // Add a bookmark in the document's outline collection.
    pdfDocument.getOutlines().add(pdfOutline);

    // Save the update document
    pdfDocument.save(_dataDir + "AddBookmark_out.pdf");