PDFBox - 文档属性

PDFBox - 文档属性 首页 / PDFbox入门教程 / PDFBox - 文档属性

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

以下是PDF文档的属性-

S.No.Property & 描述
1

File

此属性保存文件的名称。

2

Title

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

3

Author

使用此属性,可以设置文档的作者名称。

4

Subject

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

5

Keywords

使用此属性,您可以列出可用来搜索文档的关键字。

6

Created

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

7

Modified

使用此属性,可以设置文档的修改日期。

8

Application

使用此属性,可以设置文档的应用程序。

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

PDF properties

设置文件属性

PDFBox为您提供了一个名为 PDDocumentInformation 的类。此类具有一组settergetter方法。

此类的setter方法用于将值设置为文档的各种属性,而getter方法用于获取这些值。

以下是 PDDocumentInformation 类的设置方法。

S.No.Method & 描述
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 keyword)

此方法用于为名为“keyword”的PDF文档的属性设置值。

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 {

      //创建 PDF 文档对象
      PDDocument document = new PDDocument();

      //创建空白页
      PDPage blankPage = new PDPage();
       
      //将空白页添加到文档
      document.addPage( blankPage );

      //创建 PDDocumentInformation 对象
      PDDocumentInformation pdd = document.getDocumentInformation();

      //设置文档的作者
      pdd.setAuthor("Learnfk");
       
      //设置文档的标题
      pdd.setTitle("Sample document"); 
       
      //设置文档的创建者
      pdd.setCreator("PDF Examples"); 
       
      //设置文档的主题
      pdd.setSubject("Example document"); 
       
      //设置文档的创建日期
      Calendar date = new GregorianCalendar();
      date.set(2015, 11, 5); 
      pdd.setCreationDate(date);
      //设置文档的修改日期
      date.set(2016, 6, 5); 
      pdd.setModificationDate(date); 
       
      //为文档设置关键字
      pdd.setKeywords("sample, first example, my pdf"); 
 
      //保存文档
      document.save("C:/PdfBox_Examples/doc_attributes.pdf");

      System.out.println("Properties added successfully ");
       
      //关闭文档
      document.close();

   }
}

使用以下命令从命令提示符处编译并执行保存的Java文件。

javac AddingAttributes.java 
java AddingAttributes 

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

Properties added successfully

现在,如果您访问给定的路径,则可以找到在其中创建的PDF。右键单击文档,然后选择文档属性options,如下所示。

Document properties

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

Properties menu

检索文档属性

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

以下是 PDDocumentInformation 类的getter方法。

S.No.Method & 描述
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程序并加载名为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 {
      
      //加载现有文档
      File file = new File("C:/PdfBox_Examples/doc_attributes.pdf")
      PDDocument document = PDDocument.load(file);
      //获取 PDDocumentInformation 对象
      PDDocumentInformation pdd = document.getDocumentInformation();

      //检索 PDF 文档的信息
      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()); 
       
      //关闭文档
      document.close();        
   }  
}      

使用以下命令从命令提示符处编译并执行保存的Java文件。

javac RetrivingDocumentAttributes.java 
java RetrivingDocumentAttributes

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

Author of the document is :Learnfk 
Title of the document is :Sample document 
Subject of the document is :Examples 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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

软件测试52讲 -〔茹炳晟〕

TensorFlow快速入门与实战 -〔彭靖田〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

编译原理实战课 -〔宫文学〕

小马哥讲Spring AOP编程思想 -〔小马哥〕

Spring编程常见错误50例 -〔傅健〕

Web漏洞挖掘实战 -〔王昊天〕

大型Android系统重构实战 -〔黄俊彬〕

好记忆不如烂笔头。留下您的足迹吧 :)