PDFBox - 文档属性

与其他文件一样,PDF文档也具有文档属性.这些属性是键值对.每个属性都提供有关该文档的特定信息.

以下是PDF文档的属性 :

S.No.Property&描述
1

文件

此属性包含文件名.

2

标题

使用此属性,您可以设置文档的标题.

3

作者

使用此属性,您可以设置文档作者的姓名.

4

主题

使用此属性,您可以指定PDF文档的主题.

5

关键字

使用此属性,您可以列出关键字我们可以搜索文件.

6

创建

使用此属性,您可以设置为文档创建的日期.

7

修改

使用此属性,您可以设置为文件修改的日期.

8

应用程序

使用此属性,您可以设置申请文件.

以下是文件属性表的截图PDF文档.

PDF properties

设置文档属性

PDFBox为您提供了一个名为 PDDocumentInformation 的类.这个类有一组setter和getter方法.

setter方法此类用于设置文档的各种属性的值和用于检索这些值的getter方法.

以下是 PDDocumentInformation 的setter方法class.

S.No.方法和描述
1

setAuthor(String author)

使用此方法设置名为作者的PDF文档属性的值.

2

setTitle(String title)

使用此方法设置名为标题的PDF文档属性的值.

3

setCreator(String creator)

使用此方法设置名为 Creator 的PDF文档属性的值.

4

setSubject(String subject)

使用此方法设置名为主题的PDF文档属性的值.

5

setCreationDate(Calendar date)

使用此方法设置名为 CreationDate 的PDF文档属性的值.

6

setModificationDate(Calendar date)

使用此方法设置名为 ModificationDate 的PDF文档属性的值.

7

setKeywords(String keywords list)

此方法是用于设置名为关键字的PDF文档属性的值.

示例

PDFBox提供了一个名为 PDDocumentInformation 的类,这个类提供了各种方法.这些方法可以为文档设置各种属性并检索

此示例演示如何向PDF文档中添加作者,标题,日期和主题等属性.在这里,我们将创建一个PDF文档命名为 doc_attributes.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.右键单击文档并选择文档属性选项,如下所示.

文档属性

这将为您提供文档属性窗口,您可以在此处观察文档的所有属性都设置为指定值.

属性菜单

检索文档属性

您可以使用 getter

以下是 PDDocumentInformation 类的getter方法.

S.No.方法&描述
1

getAuthor()

此方法用于检索名为作者

2

getTitle()

此方法用于检索名为标题的PDF文档的属性.

3

getCreator()

此方法用于检索名为 Creator 的PDF文档属性的值.

4

getSubject()

此方法用于检索名为主题的PDF文档属性的值.

5

getCreationDate()

此方法用于检索名为 CreationDate 的PDF文档属性的值.

6

getModificationDate()

此方法用于检索名为 ModificationDate 的PDF文档的属性值.

7

getKeywords()

此方法用于检索名为关键字的PDF文档属性的值.

示例

此示例演示如何检索现有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