チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.PDF for Java»文書操作»Java:PDF のプロパティの設定・取得方法
2022-09-02

Java:PDF のプロパティの設定・取得方法

PDF プロパティは、PDF ドキュメントの一部ですが、ページには表示されません。それらのプロパティは、タイトル、著者、サブタイトル、キーワード、作成日、アプリケーションなど、ドキュメントに関する情報を含んでいます。プロパティ値の中には、自動的に生成されないものもあり、自分で設定する必要があります。この記事では、Spire.PDF for Java を使用してプログラム的に PDF プロパティを設定または取得する方法を説明します。

Spire.PDF for Java をインストールします

まず、Spire. PDF for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>8.8.3</version>
    </dependency>
</dependencies>

PDF ドキュメントのプロパティを設定する

PDF のプロパティを設定する詳細な手順は以下のとおりです。

  • PdfDocument クラスのオブジェクトを作成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • PdfDocument.getDocumentInformation() メソッドが返す DocumentInformation オブジェクトの下のメソッドを使用して、タイトル、作成者、サブタイトル、キーワード、作成日、更新日、アプリケーション、PDF 変換などのドキュメントプロパティを設定します。
  • PdfDocument.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.pdf.*;
import java.util.Date;

public class setPDFProperties {
    public static void main(String[] args) {

        //PdfDocument クラスのオブジェクトを作成する
        PdfDocument pdfDocument = new PdfDocument();

        //PDFドキュメントを読み込む
        pdfDocument.loadFromFile("C:/例.pdf");

        //タイトルを設定する
        pdfDocument.getDocumentInformation().setTitle("PDF(ポータブルドキュメント形式)");

        //Set the author
        pdfDocument.getDocumentInformation().setAuthor("穂波 たまえ");

        //サブタイトルを設定する
        pdfDocument.getDocumentInformation().setSubject("PDFの紹介");

        //キーワードを設定する
        pdfDocument.getDocumentInformation().setKeywords("PDF、ドキュメント形式");

        //作成日を設定する
        pdfDocument.getDocumentInformation().setCreationDate(new Date());

        //作成者を設定する
        pdfDocument.getDocumentInformation().setCreator("穂波 たまえ");

        //更新日を設定する
        pdfDocument.getDocumentInformation().setModificationDate(new Date());

        //PDF変換を設定する
        pdfDocument.getDocumentInformation().setProducer("Spire.PDF for Java");

        //ドキュメントを保存する
        pdfDocument.saveToFile("PDFのプロパティの設定.pdf");
    }
}

Java:PDF のプロパティの設定・取得方法

PDF ドキュメントのプロパティを取得する

PDF のプロパティを取得する詳細な手順は、以下の通りです。

  • PdfDocument クラスのオブジェクトを作成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • StringBuilder クラスのインスタンスを作成し、ドキュメントのプロパティの値を格納します。
  • PdfDocument.getDocumentInformation() メソッドから返された DocumentInformation オブジェクトのメソッドを使用してプロパティを取得し、それらを StringBuilder に格納します。
  • File.createNewFile() メソッドを使用して新しい TXT ファイルを作成します。
  • BufferedWriter.write() メソッドを使って、StringBuilder を TXT ファイルに書き込みます。
  • Java
import com.spire.pdf.*;
import java.io.*;

public class getPDFProperties {
    public static void main(String[] args) throws IOException {

        //PdfDocument クラスのオブジェクトを作成する
        PdfDocument pdf = new PdfDocument();

        //PDFドキュメントを読み込む
        pdf.loadFromFile("PDFのプロパティの設定.pdf");

        //ドキュメントのプロパティの値を格納するための StringBuilder クラスのインスタンスを作成する
        StringBuilder stringBuilder = new StringBuilder();

        //プロパティの値を取得し、StringBuilder に格納する
        stringBuilder.append("タイトル: " + pdf.getDocumentInformation().getTitle() + "\r\n");
        stringBuilder.append("作成者: " + pdf.getDocumentInformation().getAuthor() + "\r\n");
        stringBuilder.append("サブタイトル: " + pdf.getDocumentInformation().getSubject() + "\r\n");
        stringBuilder.append("キーワード: " + pdf.getDocumentInformation().getKeywords() + "\r\n");
        stringBuilder.append("アプリケーション: " + pdf.getDocumentInformation().getCreator() + "\r\n");
        stringBuilder.append("作成日: " + pdf.getDocumentInformation().getCreationDate() + "\r\n");
        stringBuilder.append("PDF変換: " + pdf.getDocumentInformation().getProducer() + "\r\n");

        //TXTファイルを新規に作成する
        File file = new File("PDFのプロパティの取得.txt");
        file.createNewFile();

        //StringBuilder をTXTファイルに書き込む
        FileWriter fileWriter = new FileWriter(file, true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(stringBuilder.toString());
        bufferedWriter.flush();
    }
}

Java:PDF のプロパティの設定・取得方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 1446 times