PDFBox文档属性

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

以下是PDF文档的属性 -

编号 属性 描述
1 File 该属性保存文件的名称。
2 Title 使用此属性,可以设置文档的标题。
3 Author 使用此属性,可以设置文档的作者姓名。
4 Subject 使用此属性,可以指定PDF文档的主题。
5 Keywords 使用此属性,列出可以搜索文档的关键字。
6 Created 使用此属性,可以设置为文档修改的日期
7 Application 使用此属性,可以设置文档的应用程序。

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

设置文档属性

PDFBox提供了一个名称为PDDocumentInformation的类。 这个类有一组settergetter方法。

该类的setter方法用于设置文档的各种属性的值,getter方法用于检索这些值的。

以下是PDDocumentInformation类的setter方法。

编号 方法 描述
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的类,该类提供了各种方法。 这些方法可以为文档设置各种属性并检索它们。

本示例演示如何将诸如作者,标题,日期和主题等属性添加到PDF文档。 在这里,我们将创建一个名称为doc_attributes.pdf的PDF文档,为此pdf文档添加各种属性,并将其保存在路径为:F:\worksp\pdfbox目录中。 将下面代码保存在名称为AddingAttributes.java的文件中。

package com.zyiz;

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("zyiz.net");

      // Setting the title of the document
      pdd.setTitle("一个简单的文档标题"); 

      //Setting the creator of the document 
      pdd.setCreator("PDF Examples"); 

      //Setting the subject of the document 
      pdd.setSubject("文档标题"); 

      //Setting the created date of the document 
      Calendar date = new GregorianCalendar();
      date.set(2017, 11, 5); 
      pdd.setCreationDate(date);
      //Setting the modified date of the document 
      date.set(2018, 10, 5); 
      pdd.setModificationDate(date); 

      //Setting keywords for the document 
      pdd.setKeywords("pdfbox, first example, my pdf"); 

      //Saving the document 
      document.save("F:/worksp/pdfbox/doc_attributes.pdf");

      System.out.println("Properties added successfully ");

      //Closing the document
      document.close();

   }
}

执行时,上述程序将所有指定的属性添加到显示以下消息的文档中。

Properties added successfully

现在,访问给定的路径找到创建的PDF。 右键单击文档并选择文档属性选项。打开文档属性窗口,在这里可以观察到文档的所有属性都被设置为指定的值。如下所示。

检索文档属性

使用PDDocumentInformation类提供的getter方法来检索文档的属性。

以下是PDDocumentInformation类的getter方法。

编号 方法 描述
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程序并加载保存在路径F:\worksp\pdfbox中的名称为doc_attributes.pdf的PDF文档,并检索其属性。 将此代码保存在名称为RetrivingDocumentAttributes.java的文件中。

package com.zyiz;

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("F:/worksp/pdfbox/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();        
   }  
}

执行后,上述程序将检索文档的所有属性并显示它们,如下所示。

Author of the document is :zyiz.net
Title of the document is :一个简单的文档标题
Subject of the document is :文档标题
Creator of the document is :PDF Examples
Creation date of the document is :java.util.GregorianCalendar[time=1512441059000,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=java.util.SimpleTimeZone[id=GMT+08:00,offset=28800000,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=11,WEEK_OF_YEAR=49,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=339,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=30,SECOND=59,MILLISECOND=0,ZONE_OFFSET=28800000,DST_OFFSET=0]
Modification date of the document is :java.util.GregorianCalendar[time=1541385059000,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=java.util.SimpleTimeZone[id=GMT+08:00,offset=28800000,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=10,WEEK_OF_YEAR=45,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=309,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=30,SECOND=59,MILLISECOND=0,ZONE_OFFSET=28800000,DST_OFFSET=0]
Keywords of the document are :pdfbox, first example, my pdf

上一篇:PDFBox删除页面

下一篇:PDFBox添加文档

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程