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.zaixian;

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("xuhuhu.com");

      // 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.zaixian;

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 :xuhuhu.com
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添加文檔