📅  最后修改于: 2020-11-12 05:15:14             🧑  作者: Mango
与其他文件一样,PDF文档也具有文档属性。这些属性是键值对。每个属性都提供有关文档的特定信息。
以下是PDF文档的属性-
S.No. | Property & Description |
---|---|
1 |
File This property holds the name of the file. |
2 |
Title Using this property, you can set the title for the document. |
3 |
Author Using this property, you can set the name of the author for the document. |
4 |
Subject Using this property, you can specify the subject of the PDF document. |
5 |
Keywords Using this property, you can list the keywords with which we can search the document. |
6 |
Created Using this property, you can set the date created for the document. |
7 |
Modified Using this property, you can set the date modified for the document. |
8 |
Application Using this property, you can set the Application of the document. |
以下是PDF文档的文档属性表的屏幕截图。
PDFBox为您提供了一个名为PDDocumentInformation的类。此类具有一组setter和getter方法。
此类的setter方法用于将值设置为文档的各种属性,而getter方法用于获取这些值。
以下是PDDocumentInformation类的setter方法。
S.No. | Method & Description |
---|---|
1 |
setAuthor(String author) This method is used to set the value for the property of the PDF document named Author. |
2 |
setTitle(String title) This method is used to set the value for the property of the PDF document named Title. |
3 |
setCreator(String creator) This method is used to set the value for the property of the PDF document named Creator. |
4 |
setSubject(String subject) This method is used to set the value for the property of the PDF document named Subject. |
5 |
setCreationDate(Calendar date) This method is used to set the value for the property of the PDF document named CreationDate. |
6 |
setModificationDate(Calendar date) This method is used to set the value for the property of the PDF document named ModificationDate. |
7 |
setKeywords(String keywords list) This method is used to set the value for the property of the PDF document named Keywords. |
PDFBox提供了一个称为PDDocumentInformation的类,该类提供了各种方法。这些方法可以为文档设置各种属性并检索它们。
本示例演示如何向PDF文档添加诸如Author,Title,Date和Subject之类的属性。在这里,我们将创建一个名为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("Tutorialspoint");
// 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类的getter方法。
S.No. | Method & Description |
---|---|
1 |
getAuthor() This method is used to retrieve the value for the property of the PDF document named Author. |
2 |
getTitle() This method is used to retrieve the value for the property of the PDF document named Title. |
3 |
getCreator() This method is used to retrieve the value for the property of the PDF document named Creator. |
4 |
getSubject() This method is used to retrieve the value for the property of the PDF document named Subject. |
5 |
getCreationDate() This method is used to retrieve the value for the property of the PDF document named CreationDate. |
6 |
getModificationDate() This method is used to retrieve the value for the property of the PDF document named ModificationDate. |
7 |
getKeywords() This method is used to retrieve the value for the property of the PDF document named Keywords. |
本示例演示了如何检索现有PDF文档的属性。在这里,我们将创建一个Java程序并加载名为doc_attributes.pdf的PDF文档,该文档保存在路径C:/ PdfBox_Examples /中,并检索其属性。将此代码保存在名为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 :Tutorialspoint
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