Pdfbox-document-properties
PDFBox-ドキュメントプロパティ
他のファイルと同様に、PDFドキュメントにもドキュメントプロパティがあります。 これらのプロパティはキーと値のペアです。 各プロパティは、ドキュメントに関する特定の情報を提供します。
以下は、PDFドキュメントのプロパティです-
S.No. | Property & Description |
---|---|
1 |
File このプロパティは、ファイルの名前を保持します。 |
2 |
Title このプロパティを使用して、ドキュメントのタイトルを設定できます。 |
3 |
Author このプロパティを使用して、ドキュメントの作成者の名前を設定できます。 |
4 |
Subject このプロパティを使用して、PDFドキュメントの件名を指定できます。 |
5 |
Keywords このプロパティを使用すると、ドキュメントを検索できるキーワードをリストできます。 |
6 |
Created このプロパティを使用して、ドキュメントに作成された日付を設定できます。 |
7 |
Modified このプロパティを使用して、ドキュメントの変更日を設定できます。 |
8 |
Application このプロパティを使用して、ドキュメントのアプリケーションを設定できます。 |
以下は、PDFドキュメントのドキュメントプロパティテーブルのスクリーンショットです。
ドキュメントプロパティの設定
PDFBoxは、 PDDocumentInformation という名前のクラスを提供します。 このクラスには、setterメソッドとgetterメソッドのセットがあります。
このクラスのセッターメソッドは、ドキュメントのさまざまなプロパティに値を設定するために使用され、これらの値を取得するために使用されるゲッターメソッドです。
以下は PDDocumentInformation クラスのセッターメソッドです。
S.No. | Method & Description |
---|---|
1 |
setAuthor(String author) このメソッドは、 Author という名前のPDFドキュメントのプロパティの値を設定するために使用されます。 |
2 |
setTitle(String title) このメソッドは、 Title という名前のPDFドキュメントのプロパティの値を設定するために使用されます。 |
3 |
setCreator(String creator) このメソッドは、 Creator という名前のPDFドキュメントのプロパティの値を設定するために使用されます。 |
4 |
setSubject(String subject) このメソッドは、 Subject という名前のPDFドキュメントのプロパティの値を設定するために使用されます。 |
5 |
setCreationDate(Calendar date) このメソッドは、 CreationDate という名前のPDFドキュメントのプロパティの値を設定するために使用されます。 |
6 |
setModificationDate(Calendar date) このメソッドは、 ModificationDate という名前のPDFドキュメントのプロパティの値を設定するために使用されます。 |
7 |
setKeywords(String keywords list) このメソッドは、 Keywords という名前のPDFドキュメントのプロパティの値を設定するために使用されます。 |
例
PDFBoxは PDDocumentInformation というクラスを提供し、このクラスはさまざまなメソッドを提供します。 これらのメソッドは、ドキュメントにさまざまなプロパティを設定して取得できます。
この例では、 Author、Title、Date、Subject などのプロパティをPDFドキュメントに追加する方法を示します。 ここでは、 doc_attributes.pdf という名前のPDFドキュメントを作成し、さまざまな属性を追加して、パス C:/PdfBox_Examples/ に保存します。 このコードを AddingAttributes.java という名前のファイルに保存します。
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.PDPage;
public class AddingDocumentAttributes {
public static void main(String args[]) throws IOException {
//Creating PDF document object
PDDocument document = new PDDocument();
//Creating a blank page
PDPage blankPage = new PDPage();
//Adding the blank page to the document
document.addPage( blankPage );
//Creating the PDDocumentInformation object
PDDocumentInformation pdd = document.getDocumentInformation();
//Setting the author of the document
pdd.setAuthor("finddevguides");
//Setting the title of the document
pdd.setTitle("Sample document");
//Setting the creator of the document
pdd.setCreator("PDF Examples");
//Setting the subject of the document
pdd.setSubject("Example document");
//Setting the created date of the document
Calendar date = new GregorianCalendar();
date.set(2015, 11, 5);
pdd.setCreationDate(date);
//Setting the modified date of the document
date.set(2016, 6, 5);
pdd.setModificationDate(date);
//Setting keywords for the document
pdd.setKeywords("sample, first example, my pdf");
//Saving the document
document.save("C:/PdfBox_Examples/doc_attributes.pdf");
System.out.println("Properties added successfully ");
//Closing the document
document.close();
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。
javac AddingAttributes.java
java AddingAttributes
上記のプログラムは、実行時に、指定されたすべての属性を次のメッセージを表示するドキュメントに追加します。
Properties added successfully
これで、指定されたパスにアクセスすると、そのパスで作成されたPDFを見つけることができます。 ドキュメントを右クリックして、下に示すようにドキュメントプロパティオプションを選択します。
これにより、ドキュメントプロパティウィンドウが表示され、ここで、ドキュメントのすべてのプロパティが指定された値に設定されていることを確認できます。
ドキュメントプロパティの取得
*PDDocumentInformation* クラスによって提供される *getter* メソッドを使用して、ドキュメントのプロパティを取得できます。
以下は PDDocumentInformation クラスの取得メソッドです。
S.No. | Method & Description |
---|---|
1 |
getAuthor() このメソッドは、 Author という名前のPDFドキュメントのプロパティの値を取得するために使用されます。 |
2 |
getTitle() このメソッドは、 Title という名前のPDFドキュメントのプロパティの値を取得するために使用されます。 |
3 |
getCreator() このメソッドは、 Creator という名前のPDFドキュメントのプロパティの値を取得するために使用されます。 |
4 |
getSubject() このメソッドは、 Subject という名前のPDFドキュメントのプロパティの値を取得するために使用されます。 |
5 |
getCreationDate() このメソッドは、 CreationDate という名前のPDFドキュメントのプロパティの値を取得するために使用されます。 |
6 |
getModificationDate() このメソッドは、 ModificationDate という名前のPDFドキュメントのプロパティの値を取得するために使用されます。 |
7 |
getKeywords() このメソッドは、 Keywords という名前のPDFドキュメントのプロパティの値を取得するために使用されます。 |
例
この例は、既存のPDFドキュメントのプロパティを取得する方法を示しています。 ここでは、Javaプログラムを作成し、パス C:/PdfBox_Examples/ に保存されている doc_attributes.pdf という名前のPDFドキュメントをロードし、そのプロパティを取得します。 このコードを RetrivingDocumentAttributes.java という名前のファイルに保存します。
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
public class RetrivingDocumentAttributes {
public static void main(String args[]) throws IOException {
//Loading an existing document
File file = new File("C:/PdfBox_Examples/doc_attributes.pdf")
PDDocument document = PDDocument.load(file);
//Getting the PDDocumentInformation object
PDDocumentInformation pdd = document.getDocumentInformation();
//Retrieving the info of a PDF document
System.out.println("Author of the document is :"+ pdd.getAuthor());
System.out.println("Title of the document is :"+ pdd.getTitle());
System.out.println("Subject of the document is :"+ pdd.getSubject());
System.out.println("Creator of the document is :"+ pdd.getCreator());
System.out.println("Creation date of the document is :"+ pdd.getCreationDate());
System.out.println("Modification date of the document is :"+
pdd.getModificationDate());
System.out.println("Keywords of the document are :"+ pdd.getKeywords());
//Closing the document
document.close();
}
}
次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。
javac RetrivingDocumentAttributes.java
java RetrivingDocumentAttributes
実行すると、上記のプログラムはドキュメントのすべての属性を取得し、次のように表示します。
Author of the document is :finddevguides
Title of the document is :Sample document
Subject of the document is :Example document
Creator of the document is :PDF Examples
Creation date of the document is :11/5/2015
Modification date of the document is :6/5/2016
Keywords of the document are :sample, first example, my pdf