Java を介して PDF ドキュメントに添付ファイルを追加

Java を使用してプログラムで PDF に添付ファイルを追加する方法

Java ライブラリを使用して添付ファイルを管理する方法

添付ファイルを追加するために、Javaプラットフォーム用の機能が豊富で強力で使いやすい変換APIである Aspose.PDF for Java APIを使用します。最新バージョンを Maven から直接ダウンロードし、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 経由で PDF ファイルに添付ファイルを追加


ご使用の環境でコードを試すには Aspose.PDF for Java が必要です。

1。新しい Java プロジェクトを作成します。 1。Aspose.PDF DLL への参照を追加します。 1。Document オブジェクトを作成します。 1。追加するファイルとファイルの説明を含む FileSpecification オブジェクトを作成します。 1。コレクションの Add メソッドを使用して、FileSpecification オブジェクトを Document オブジェクトの EmbeddedFiles コレクションに追加します。 1。PDF ファイルを保存します。

PDF ドキュメントへの添付ファイルの追加


    // Open a document
    Document pdfDocument = new Document(_dataDir+"input.pdf");
    // Set up a new file to be added as attachment
    FileSpecification fileSpecification = new FileSpecification("sample.txt", "Sample text file");
    // Add an attachment to document's attachment collection
    pdfDocument.getEmbeddedFiles().add(fileSpecification);
    // Save the updated document
    pdfDocument.save("output.pdf");