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。Document オブジェクトを使用して PDF ドキュメントを開きます。 1。ブックマークを作成し、そのプロパティを定義します。 1。OutlineItemCollection コレクションをアウトラインコレクションに追加します。 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");