PDF 文档的属性是 PDF 文档不展示在页面中的一部分内容,通常包含文档的标题、作者、主题、关键词、创建日期以及创建者等信息。PDF 文档属性中的某些内容不会自动生成,需要我们自己去添加。本文将给大家介绍使用 Spire.PDF for Java 通过编程的方式来设置或获取 PDF 文档的属性的操作步骤。
安装 Spire.PDF for Java
首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。JAR 文件可以从 此链接 下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>11.10.3</version>
    </dependency>
</dependencies>
设置 PDF 文档的属性
设置 PDF 文档属性的详细操作步骤如下:
- 创建 PdfDocument 类的对象。
- 用 PdfDocument.loadFromFile() 方法从磁盘加载 PDF 文档。
- 用 PdfDocument.getDocumentInformation() 方法返回的 DocumentInformation 对象下的方法设置标题、作者、主题、关键词、创建日期、修改日期、创建者及制作工具等属性。
- 用 PdfDocument.saveToFile() 方法保存 PDF 文档。
- Java
import com.spire.pdf.*;
import java.util.Date;
public class setPDFProperties {
    public static void main(String[] args) {
        //创建 PdfDocument 类的对象
        PdfDocument pdfDocument = new PdfDocument();
        //从磁盘加载 PDF 文档
        pdfDocument.loadFromFile("示例.pdf");
        //设置标题
        pdfDocument.getDocumentInformation().setTitle("PDF(便携式文档格式)");
        //设置作者
        pdfDocument.getDocumentInformation().setAuthor("李林");
        //设置主题
        pdfDocument.getDocumentInformation().setSubject("PDF简介");
        //设置关键词
        pdfDocument.getDocumentInformation().setKeywords("PDF, 文档格式");
        //设置创建日期
        pdfDocument.getDocumentInformation().setCreationDate(new Date());
        //设置创建者
        pdfDocument.getDocumentInformation().setCreator("李林");
        //设置修改日期
        pdfDocument.getDocumentInformation().setModificationDate(new Date());
        //设置制作工具
        pdfDocument.getDocumentInformation().setProducer("Spire.PDF");
        //保存文档
        pdfDocument.saveToFile("设置PDF文档的属性.pdf");
    }
}
获取 PDF 文档的属性
获取 PDF 文档属性的详细操作步骤如下:
- 创建 PdfDocument 类的对象。
- 用 PdfDocument.loadFromFile() 方法从磁盘加载 PDF 文档。
- 创建 StringBuilder 类的对象以存放获取的属性数据。
- 用 PdfDocument.getDocumentInformation() 方法返回的 DocumentInformation 对象下的方法获取 PDF 文档的属性并存放于创建的 StringBuilder。
- 用 File.createNewFile() 方法创建一个 TXT 文件。
- 用 BufferedWriter.write() 方法将 StringBuilder 写入 TXT 文件。
- Java
import com.spire.pdf.*;
import java.io.*;
public class getPDFProperties {
    public static void main(String[] args) throws IOException {
        //创建 PdfDocument 类的对象
        PdfDocument pdf = new PdfDocument();
        //从磁盘加载PDF文档
        pdf.loadFromFile("" + "setPDFProperties.pdf");
        //创建 StringBuilder 的对象以储存获取的属性数据
        StringBuilder stringBuilder = new StringBuilder();
        //获取PDF文档的属性数据并储存于创建的 StringBuilder
        stringBuilder.append("标题:" + pdf.getDocumentInformation().getTitle() + "\r\n");
        stringBuilder.append("作者" + pdf.getDocumentInformation().getAuthor() + "\r\n");
        stringBuilder.append("主题:" + pdf.getDocumentInformation().getSubject() + "\r\n");
        stringBuilder.append("关键词:" + pdf.getDocumentInformation().getKeywords() + "\r\n");
        stringBuilder.append("创建者:" + pdf.getDocumentInformation().getCreator() + "\r\n");
        stringBuilder.append("创建时间:" + pdf.getDocumentInformation().getCreationDate() + "\r\n");
        stringBuilder.append("制作工具:" + pdf.getDocumentInformation().getProducer() + "\r\n");
        //创建一个TXT文件
        File file = new File("getPDFProperties.txt");
        file.createNewFile();
        //将 StringBuilder 写入TXT文件
        FileWriter fileWriter = new FileWriter(file, true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(stringBuilder.toString());
        bufferedWriter.flush();
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



