网站首页 > java教程 正文
[翻译]与其他文件一样,PDF文档也具有文档属性。这些属性是以键值对的形式存储的。每个属性提供有关文档的特定信息。
[原文]Like other files, a PDF document also has document properties. These properties are key-value pairs. Each property gives particular information about the document.
Properties /'prɑ.p.tiz/ 属性
Key-value /ki 'vael.ju/ 键值对
Particular /p'tk.j.l/ 特定的
Information /n.f'me.n/ 信息
Document /'dɑ.kj.mnt/ 文档
[翻译]以下是PDF文档的属性 -
- 文件:此属性存储文件的名称。
- 标题:使用此属性,您可以为文档设置标题。
- 作者:使用此属性,您可以为文档设置作者姓名。
- 主题:使用此属性,您可以指定PDF文档的主题。
- 关键字:使用此属性,您可以列出用于搜索文档的关键字。
- 创建日期:使用此属性,您可以设置文档的创建日期。
- 修改日期:使用此属性,您可以设置文档的修改日期。 1.应用程序:使用此属性,您可以设置文档的应用程序。
[原文]Following are the properties of a PDF document -
- File This property holds the name of the file. |
- Title Using this property, you can set the title for the document. |
- Author Using this property, you can set the name of the author for the document. |
- Subject Using this property, you can specify the subject of the PDF document. |
- Keywords Using this property, you can list the keywords with which we can search the document. |
- Created Using this property, you can set the date created for the document. |
- Modified Using this property, you can set the date modified for the document. |
- Application Using this property, you can set the Application of the document. |
Property /'prɑ.p.ti/ 属性
File /fal/ 文件
Title /'ta.tl/ 标题
Author /'ɑ.θ/ 作者
Subject /'sb.dekt/ 主题
Keywords /'ki.wdz/ 关键字
Created /kri'e.td/ 创建的
Modified /'mɑ.d.fad/ 修改的
Application /aep.l'ke.n/ 应用程序
[翻译]以下是PDF文档属性的截图。
[原文]Following is a screenshot of the document properties table of a PDF document.
Screenshot /'skrin.ɑt/ 截图
Properties /'prɑ.p.tiz/ 属性
Document /'dɑ.kj.mnt/ 文档
Setting the Document Properties 设置文档属性
[翻译]PDFBox提供了一个名为PDDocumentInformation的类。此类包含一系列设置和获取方法。设置方法用于为文档的各种属性设置值,获取方法用于检索这些值。
[原文]PDFBox provides you a class named PDDocumentInformation. This class has a set of setter and getter methods. The setter methods of this class are used to set values to various properties of a document and getter methods which are used to retrieve these values.
Provides /pr'vadz/ 提供
Class /klaes/ 类
Setter /'set./ 设置方法
Getter /'ɡet./ 获取方法
Properties /'prɑ.p.tiz/ 属性
Retrieve /r'triv/ 检索
Values /'vael.juz/ 值
[翻译]以下是PDDocumentInformation类的设置方法:
- setAuthor(String author):此方法用于为PDF文档的作者属性设置值。
- setTitle(String title):此方法用于为PDF文档的标题属性设置值。
- setCreator(String creator):此方法用于为PDF文档的创建者属性设置值。
- setSubject(String subject):此方法用于为PDF文档的主题属性设置值。
- setCreationDate(Calendar date):此方法用于为PDF文档的创建日期属性设置值。
- setModificationDate(Calendar date):此方法用于为PDF文档的修改日期属性设置值。
- setKeywords(String keywords list):此方法用于为PDF文档的关键字属性设置值。
[原文]Following are the setter methods of the PDDocumentInformation class.
- setAuthor(String author) This method is used to set the value for the property of the PDF document named Author. |
- setTitle(String title) This method is used to set the value for the property of the PDF document named Title. |
- setCreator(String creator) This method is used to set the value for the property of the PDF document named Creator. |
- setSubject(String subject) This method is used to set the value for the property of the PDF document named Subject. |
- setCreationDate(Calendar date) This method is used to set the value for the property of the PDF document named CreationDate. |
- setModificationDate(Calendar date) This method is used to set the value for the property of the PDF document named ModificationDate. |
- setKeywords(String keywords list) This method is used to set the value for the property of the PDF document named Keywords. |
Setter /'set./ 设置方法
Method /'meθ.d/ 方法
Author /'ɑ.θ/ 作者
Title /'ta.tl/ 标题
Creator /kri'e.t/ 创建者
Subject /'sb.dekt/ 主题
CreationDate /kri'e.n det/ 创建日期
ModificationDate /mɑ.d.f'ke.n det/ 修改日期
Keywords /'ki.wdz/ 关键字
Example 示例
[翻译]PDFBox提供了一个名为PDDocumentInformation的类,此类提供多种方法。这些方法可以为文档设置各种属性并检索它们。此示例演示如何为PDF文档添加作者、标题、日期和主题等属性。在这里,我们将创建一个名为doc_attributes.pdf的PDF文档,为其添加各种属性,并将其保存在路径C:/PdfBox_Examples/中。将此代码保存在名为AddingAttributes.java的文件中。
[原文]PDFBox provides a class called PDDocumentInformation and this class provides various methods. These methods can set various properties to the document and retrieve them. This example demonstrates how to add properties such as Author, Title, Date, and Subject to a PDF document. Here, we will create a PDF document named doc_attributes.pdf, add various attributes to it, and save it in the path C:/PdfBox_Examples/. Save this code in a file with name 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_attributesnodejs
System.out.println("Properties added successfully ");
//Closing the document
document.close();
}
}
Provides /pr'vadz/ 提供
Class /klaes/ 类
Methods /'meθ.dz/ 方法
Properties /'prɑ.p.tiz/ 属性
Retrieve /r'triv/ 检索
Author /'ɑ.θ/ 作者
Title /'ta.tl/ 标题
Date /det/ 日期
Subject /'sb.dekt/ 主题
[翻译]使用以下命令从命令提示符编译并执行保存的Java文件。
[原文]Compile and execute the saved Java file from the command prompt using the following commands.
javac AddingAttributes.java
java AddingAttributes
Compile /km'pal/ 编译
Execute /'ek.s.kjut/ 执行
Command /k'maend/ 命令
Prompt /prɑmpt/ 提示符
[翻译]执行上述程序后,将为文档添加所有指定的属性,并显示以下消息。
[原文]Upon execution, the above program adds all the specified attributes to the document displaying the following message.
Properties added successfully
Attributes /'aet.r.bjuts/ 属性
Specified /'spes..fad/ 指定的
Displaying /d'sple./ 显示
Message /'mes.d/ 消息
[翻译]现在,如果您访问指定路径,可以找到创建的PDF文档。右键单击文档并选择文档属性选项,如下所示。
[原文]Now, if you visit the given path you can find the PDF created in it. Right click on the document and select the document properties option as shown below.
Path /paeθ/ 路径
Created /kri'e.td/ 创建的
Right click /rat klk/ 右键单击
Properties /'prɑ.p.tiz/ 属性
Option /'ɑp.n/ 选项
[翻译]这将显示文档属性窗口,在这里您可以观察到文档的所有属性均被设置为指定的值。
[原文]This will give you the document properties window and here you can observe all the properties of the document were set to specified values.
Window /'wn.do/ 窗口
Observe /b'zv/ 观察
Properties /'prɑ.p.tiz/ 属性
Specified /'spes..fad/ 指定的
Values /'vael.juz/ 值
Retrieving the Document Properties 检索文档属性
[翻译]您可以使用PDDocumentInformation类提供的获取方法来检索文档的属性。以下是PDDocumentInformation类的获取方法:
- getAuthor()
此方法用于检索PDF文档的作者属性的值。 - getTitle()
此方法用于检索PDF文档的标题属性的值。 - getCreator()
此方法用于检索PDF文档的创建者属性的值。 - getSubject()
此方法用于检索PDF文档的主题属性的值。 - getCreationDate()
此方法用于检索PDF文档的创建日期属性的值。 - getModificationDate()
此方法用于检索PDF文档的修改日期属性的值。 - getKeywords()
此方法用于检索PDF文档的关键字属性的值。
[原文]You can retrieve the properties of a document using the getter methods provided by the PDDocumentInformation class. Following are the getter methods of the PDDocumentInformation class.
S.No. | Method & Description |
1 | getAuthor()This method is used to retrieve the value for the property of the PDF document named Author. |
2 | getTitle()This method is used to retrieve the value for the property of the PDF document named Title. |
3 | getCreator()This method is used to retrieve the value for the property of the PDF document named Creator. |
4 | getSubject()This method is used to retrieve the value for the property of the PDF document named Subject. |
5 | getCreationDate()This method is used to retrieve the value for the property of the PDF document named CreationDate. |
6 | getModificationDate()This method is used to retrieve the value for the property of the PDF document named ModificationDate. |
7 | getKeywords()This method is used to retrieve the value for the property of the PDF document named Keywords. |
Retrieve /r'triv/ 检索
Getter /'ɡet./ 获取方法
Properties /'prɑ.p.tiz/ 属性
Author /'ɑ.θ/ 作者
Title /'ta.tl/ 标题
Creator /kri'e.t/ 创建者
Subject /'sb.dekt/ 主题
CreationDate /kri'e.n det/ 创建日期
ModificationDate /mɑ.d.f'ke.n det/ 修改日期
Keywords /'ki.wdz/ 关键字
Example 示例
[翻译]此示例演示如何检索现有PDF文档的属性。在这里,我们将创建Java程序,加载位于路径C:/PdfBox_Examples/中的名为doc_attributes.pdf的PDF文档,并检索其属性。将此代码保存在名为
RetrivingDocumentAttributes.java的文件中。
[原文]This example demonstrates how to retrieve the properties of an existing PDF document. Here, we will create a Java program and load the PDF document named doc_attributes.pdf, which is saved in the path C:/PdfBox_Examples/, and retrieve its properties. Save this code in a file with name
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();
}
}
Demonstrates /'dem.n.strets/ 演示
Retrieve /r'triv/ 检索
Existing /ɡ'zs.t/ 现有的
Document /'dɑ.kj.mnt/ 文档
Path /paeθ/ 路径
Properties /'prɑ.p.tiz/ 属性
[翻译]使用以下命令从命令提示符编译并执行保存的Java文件。
[原文]Compile and execute the saved Java file from the command prompt using the following commands.
javac RetrivingDocumentAttributes.java
java RetrivingDocumentAttributes
Compile /km'pal/ 编译
Execute /'ek.s.kjut/ 执行
Command /k'maend/ 命令
Prompt /prɑmpt/ 提示符
[翻译]执行上述程序后,将检索文档的所有属性并显示如下。
[原文]Upon execution, the above program retrieves all the attributes of the document and displays them as shown below.
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
Execution /ek.s'kju.n/ 执行
Retrieves /r'trivz/ 检索
Attributes /'aet.r.bjuts/ 属性
Displays /d'splez/ 显示
下面是一个设置属性的例子:
package com.virhuiai.pdfbox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.PDPage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* 用于创建新PDF文档,添加空白页面,设置文档属性并保存到指定路径。
* 该类使用Apache PDFBox库操作PDF文档。
*/
public class E8 {
// 定义输出PDF文件的路径常量
public static void main(String[] args) {
String OUTPUT_PATH = "/Volumes/RamDisk/E8.pdf";
// 检查输出路径是否有效
File outputFile = new File(OUTPUT_PATH);
validateOutputPath(outputFile);
// 使用try-with-resources确保PDDocument资源在使用完毕后自动关闭
try (PDDocument document = new PDDocument()) {
// 添加空白页面到文档
addBlankPage(document);
// 设置文档属性
setDocumentProperties(document);
// 保存PDF文档到指定路径
document.save(outputFile);
// 打印成功保存的信息,包含文件的绝对路径
System.out.println("PDF文档已成功保存至: " + outputFile.getAbsolutePath());
} catch (IOException e) {
// 捕获并处理IO异常,例如文件路径错误、权限问题或磁盘空间不足
throw new RuntimeException("无法保存PDF文档: " + e.getMessage(), e);
} catch (IllegalArgumentException e) {
// 捕获并处理无效参数异常,例如日期格式错误
System.err.println("参数错误: " + e.getMessage());
throw e;
}
}
/**
* 检查输出文件路径是否有效,确保父目录存在且可写。
*
* @param outputFile 输出文件对象
* @throws IllegalArgumentException 如果路径无效或不可写
*/
private static void validateOutputPath(File outputFile) {
File parentDir = outputFile.getParentFile();
if (parentDir != null && !parentDir.exists()) {
throw new IllegalArgumentException("输出路径的父目录不存在: " + parentDir.getAbsolutePath());
}
if (parentDir != null && !parentDir.canWrite()) {
throw new IllegalArgumentException("输出路径的父目录不可写: " + parentDir.getAbsolutePath());
}
}
/**
* 向PDF文档添加空白页面。
*
* @param document PDF文档对象
*/
private static void addBlankPage(PDDocument document) {
// 创建一个新的空白页面
PDPage blankPage = new PDPage();
// 将空白页面添加到文档
document.addPage(blankPage);
}
/**
* 设置PDF文档的属性,包括作者、标题、创建者、主题、创建日期、修改日期和关键字。
*
* @param document PDF文档对象
*/
private static void setDocumentProperties(PDDocument document) {
// 获取文档的属性对象
PDDocumentInformation pdd = document.getDocumentInformation();
// 设置文档作者
pdd.setAuthor("Tutorialspoint");
// 设置文档标题
pdd.setTitle("Sample document");
// 设置文档创建者
pdd.setCreator("PDF Examples");
// 设置文档主题
pdd.setSubject("Example document");
// 设置文档创建日期
Calendar creationDate = new GregorianCalendar();
creationDate.set(2015, Calendar.NOVEMBER, 5); // 使用Calendar常量提高可读性
pdd.setCreationDate(creationDate);
// 设置文档修改日期
Calendar modificationDate = new GregorianCalendar();
modificationDate.set(2016, Calendar.JUNE, 5); // 使用Calendar常量提高可读性
pdd.setModificationDate(modificationDate);
// 设置文档关键字,多个关键字以逗号分隔
pdd.setKeywords("sample, first example, my pdf");
}
}
E8相对于RetrivingDocumentAttributes的优化点
以下是 E8 代码中体现的优化点,结合
RetrivingDocumentAttributes 的结构进行类比:
- 代码结构模块化:
- 优化点:E8 将功能拆分为独立的方法(validateOutputPath、addBlankPage、setDocumentProperties),提高了代码的可读性和可维护性。
- 对比:RetrivingDocumentAttributes 的逻辑全部集中在 main 方法中,代码较为紧凑但缺乏模块化。如果将其与创建文档的原始版本对比,E8 的方法抽取显著提升了代码组织性。
- 示例:setDocumentProperties 方法将所有属性设置逻辑封装,方便复用和修改。
- 路径验证逻辑:
- 优化点:E8 增加了 validateOutputPath 方法,检查输出路径的父目录是否存在且可写,提前捕获潜在的 IO 异常。
- 对比:RetrivingDocumentAttributes 直接加载文件路径(C:/PdfBox_Examples/doc_attributes.pdf),未对路径进行验证,可能在文件或目录不存在时抛出未经处理的异常。
- 示例:E8 的路径验证逻辑避免了因路径错误导致的运行时异常。
- 使用 try-with-resources:
- 优化点:E8 使用 try-with-resources 管理 PDDocument 资源,确保文档对象在使用后自动关闭,减少资源泄漏风险。
- 对比:RetrivingDocumentAttributes 手动调用 document.close(),虽然功能正确,但如果在关闭前发生异常,可能导致资源未释放。try-with-resources 是更现代和安全的做法。
- 示例:try (PDDocument document = new PDDocument()) 确保资源自动管理。
- 异常处理改进:
- 优化点:E8 捕获了 IOException 和 IllegalArgumentException,并提供详细的错误信息(如“无法保存PDF文档”),同时将异常包装为 RuntimeException 或通过 System.err 输出,便于调试。
- 对比:RetrivingDocumentAttributes 的异常处理较为简单,仅声明 throws IOException,未提供自定义错误信息,可能不便于定位问题。
- 示例:E8 的 throw new RuntimeException("无法保存PDF文档: " + e.getMessage(), e) 提供更清晰的错误上下文。
- 日期设置逻辑优化:
- 优化点:E8 使用 Calendar.NOVEMBER 和 Calendar.JUNE 等常量设置日期,提高了代码的可读性。同时,为创建日期和修改日期分别创建独立的 Calendar 实例,避免复用对象可能导致的逻辑错误。
- 对比:RetrivingDocumentAttributes 不涉及日期设置,因此无法直接对比。但如果原始版本类似 E8 的属性设置逻辑,E8 的日期处理更规范。
- 示例:creationDate.set(2015, Calendar.NOVEMBER, 5) 比硬编码月份更直观。
- 常量定义:
- 优化点:E8 将输出路径定义为常量 OUTPUT_PATH,便于维护和修改。
- 对比:RetrivingDocumentAttributes 直接硬编码文件路径(C:/PdfBox_Examples/doc_attributes.pdf),如果需要更改路径,必须修改代码本身。
- 示例:private static final String OUTPUT_PATH = "/Volumes/RamDisk/E8.pdf" 提高了代码灵活性。
- 添加文档注释:
- 优化点:E8 为类和方法添加了详细的 JavaDoc 注释,说明功能、参数和异常,增强了代码的可读性和文档化程度。
- 对比:RetrivingDocumentAttributes 仅包含少量行内注释(如“Loading an existing document”),缺乏系统性的文档说明。
- 示例:E8 的 /** 用于创建新PDF文档... */ 提供了清晰的功能描述。
- 包结构规范:
- 优化点:E8 定义了包名 com.virhuiai.pdfbox,符合 Java 包命名规范,便于项目组织和模块管理。
- 对比:RetrivingDocumentAttributes 未定义包名,可能导致在大型项目中难以管理。
- 示例:package com.virhuiai.pdfbox 提供了命名空间。
- 输出信息优化:
- 优化点:E8 的成功消息(PDF文档已成功保存至: ...)更具体,包含文件的绝对路径,便于用户确认文件位置。
- 对比:RetrivingDocumentAttributes 的输出信息(如 Author of the document is :...)仅打印属性值,格式较为简单,且拼接字符串时未使用空格,可能影响可读性(例如 is :)。
- 示例:E8 的 System.out.println("PDF文档已成功保存至: " + outputFile.getAbsolutePath()) 更用户友好。
- 上一篇: 23种设计模式娓娓道来,助你优雅的编写出漂亮代码
- 下一篇:已经是最后一篇了
猜你喜欢
- 2025-07-01 8.Java读取和修改PDF文件属性(PDFBOX)
- 2025-07-01 23种设计模式娓娓道来,助你优雅的编写出漂亮代码
- 2025-07-01 MySQL选择合适的数据类型(char与varchar|text与blob)
- 2025-07-01 freemark学习(二):freemark的数据类型(1)
- 2025-07-01 Java 编程入门:变量与数据类型(java变量数据类型有哪些)
- 2025-07-01 Java基础数据类型与核心概念(java基本数据类型有哪八种)
- 2025-07-01 【Java 编程入门】基本数据类型(java基本数据类型详解)
- 2025-07-01 Chrome 插件怎么安装与下载?超详细
- 2025-07-01 Chrome如何查看保存的网站密码,如此简单!
- 2025-07-01 来看看阿里面试的一面都面了些什么笔试+机试(java岗)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)