📅  最后修改于: 2021-01-07 07:41:04             🧑  作者: Mango
PDF文档具有许多属性。这些属性提供与PDF文档有关的元数据信息。由于某些字段是可选的,因此不能保证所有PDF文件都具有我们需要的所有元数据。
PDF文档包含以下属性-
File Name | It holds the name of the File. |
Title | It is used to set the Title of the PDF document. |
Author | It is used to set the Author name of the PDF document. |
Subject | It is used to specify the Subject of the document. |
Application | It is used to set the Application of the document. |
Keyword | It is used to create the list of the Keywords from which we can search the document. |
Created | It is used to set the Date for the creation of document. |
Modified | It is used to set the Date of the modification of the document. |
Producer | It is used to set the producer name of the document. |
PDFBox提供了PDDocumentInformation类,用于设置文档属性。此类具有一组setter和getter方法。 Setter方法用于设置文档属性的值,而getter方法用于检索该值。
PDDocumentInformation类的重要Setter方法如下:
本示例说明了如何向PDF文档中添加诸如作者,标题,日期,主题等属性。
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 DocumentProperties{
public static void main(String[] args)throws IOException {
//Creating PDF document object
PDDocument doc = new PDDocument();
//Creating a blank page
PDPage blankPage = new PDPage();
//Adding the blank page to the document
doc.addPage( blankPage );
//Creating the PDDocumentInformation object
PDDocumentInformation pdd = doc.getDocumentInformation();
//Setting the author of the document
pdd.setAuthor("JavaTpoint");
// Setting the title of the document
pdd.setTitle("My Document");
//Setting the creator of the document
pdd.setCreator("SSSIT");
//Setting the subject of the document
pdd.setSubject("PDF Example");
//Setting the created date of the document
Calendar date = new GregorianCalendar();
date.set(2018, 5, 7);
pdd.setCreationDate(date);
//Setting the modified date of the document
date.set(2018, 6, 5);
pdd.setModificationDate(date);
//Setting keywords for the document
pdd.setKeywords("Java, example, my pdf");
//Setting Producer for the document
pdd.setProducer("JavaTpoint.com");
//Saving the document
doc.save("/eclipse-workspace/blank.pdf");
System.out.println("Properties added successfully to a PDF document.");
//Closing the document
doc.close();
}
}
输出:
成功执行上述程序后,它将从PDF文档中检索文本,如以下输出所示。
PDDocumentInformation类的重要getter方法如下-
本示例说明了如何向PDF文档中添加诸如作者,标题,日期,主题等属性。
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
public class DocumentProperties {
public static void main(String[] args)throws IOException {
//Loading an existing document
File file = new File("/eclipse-workspace/blanck.pdf");
PDDocument doc = PDDocument.load(file);
//Getting the PDDocumentInformation object
PDDocumentInformation pdd = doc.getDocumentInformation();
//Retrieving the info of a PDF document
System.out.println("Author of the PDF document is :"+ pdd.getAuthor());
System.out.println("Title of the PDF document is :"+ pdd.getTitle());
System.out.println("Subject of the document is :"+ pdd.getSubject());
System.out.println("Creator of the PDF document is :"+ pdd.getCreator());
System.out.println("Keywords of the PDF document are :"+ pdd.getKeywords());
System.out.println("Creation date of the PDF document is :"+ pdd.getCreationDate());
System.out.println("Modification date of the PDF document is :"+ pdd.getModificationDate());
//Closing the document
doc.close();
}
}
输出:
成功执行上述程序后,它将检索PDF文档的所有属性,这些属性可以在以下输出中显示。