6.Java中加载PDF文档(PDFBOX,并对其进行一些操作如加一页)
[翻译]在前面的示例中,您已经了解了如何创建新文档并向其中添加页面。本章将教您如何加载系统中已有的PDF文档,并对其进行一些操作。
[原文]In the previous examples, you have seen how to create a new document and add pages to it. This chapter teaches you how to load a PDF document that already exists in your system, and perform some operations on it.
Previous /'pri.vi.s/ 之前的
Examples /ɡ'zaem.plz/ 示例
Create /kri'et/ 创建
Document /'dɑ.kj.mnt/ 文档
Load /lod/ 加载
Exists /ɡ'zsts/ 存在
System /'ss.tm/ 系统
Operations /ɑ.p're.nz/ 操作
Loading an Existing PDF Document 加载现有PDF文档
[翻译]PDDocument类的load()方法用于加载现有的PDF文档。请按照以下步骤加载现有PDF文档。
[原文]The load() method of the PDDocument class is used to load an existing PDF document. Follow the steps given below to load an existing PDF document.
Load /lod/ 加载
Method /'meθ.d/ 方法
PDDocument /pi.di'di.dɑ.kj.mnt/ (类名称,无直接翻译)
Existing /ɡ'zs.t/ 现有的
Document /'dɑ.kj.mnt/ 文档
Step 1: Loading an Existing PDF Document 步骤 1:加载现有PDF文档
[翻译]使用PDDocument类的静态方法load()加载现有PDF文档。此方法接受一个文件对象作为参数,因为这是一个静态方法,您可以使用类名调用它,如下所示。
[原文]Load an existing PDF document using the static method load() of the PDDocument class. This method accepts a file object as a parameter, since this is a static method you can invoke it using class name as shown below.
File file = new File("path of the document")
PDDocument.load(file);
Load /lod/ 加载
Static /'staet.k/ 静态的
Method /'meθ.d/ 方法
Accepts /k'septs/ 接受
File /fal/ 文件
Object /'ɑb.dekt/ 对象
Parameter /p'raem..t/ 参数
Invoke /n'vok/ 调用
Step 2: Perform the Required Operations 步骤 2:执行所需操作
[翻译]对加载的文档执行所需的操作,例如添加页面、添加文本、添加图像。
[原文]Perform the required operations such as adding pages adding text, adding images to the loaded document.
Perform /p'frm/ 执行
Required /r'kward/ 所需的
Operations /ɑ.p're.nz/ 操作
Adding /'aed./ 添加
Pages /'pe.dz/ 页面
Text /tekst/ 文本
Images /'m.dz/ 图像
Loaded /'lo.dd/ 已加载的
Step 3: Saving the Document 步骤 3:保存文档
[翻译]添加所有页面后,使用PDDocument类的save()方法保存PDF文档,如以下代码块所示。
[原文]After adding all the pages, save the PDF document using the save() method of the PDDocument class as shown in the following code block.
document.save("Path");
Save /sev/ 保存
Document /'dɑ.kj.mnt/ 文档
Method /'meθ.d/ 方法
Step 4: Closing the Document 步骤 4:关闭文档
[翻译]最后,使用PDDocument类的close()方法关闭文档,如下所示。
[原文]Finally close the document using the close() method of the PDDocument class as shown below.
document.close();
Close /kloz/ 关闭
Document /'dɑ.kj.mnt/ 文档
Method /'meθ.d/ 方法
Example 示例
[翻译]假设我们有一个包含单一页面的PDF文档,位于路径C:/PdfBox_Examples/中,如下截图所示。
[原文]Suppose we have a PDF document which contains a single page, in the path, C:/PdfBox_Examples/ as shown in the following screenshot.
Suppose /s'poz/ 假设
Contains /kn'tenz/ 包含
Single /'s.ɡl/ 单一的
Page /ped/ 页面
Path /paeθ/ 路径
Screenshot /'skrin.ɑt/ 截图
[翻译]此示例演示如何加载现有PDF文档。在这里,我们将加载上述显示的PDF文档sample.pdf,向其添加一个页面,并以相同的名称保存在同一路径中。
[原文]This example demonstrates how to load an existing PDF Document. Here, we will load the PDF document sample.pdf shown above, add a page to it, and save it in the same path with the same name.
Demonstrates /'dem.n.strets/ 演示
Load /lod/ 加载
Existing /ɡ'zs.t/ 现有的
Document /'dɑ.kj.mnt/ 文档
Add /aed/ 添加
Page /ped/ 页面
Path /paeθ/ 路径
[翻译]步骤 1 - 将此代码保存在名为
LoadingExistingDocument.java的文件中。
[原文]Step 1 - Save this code in a file with name
LoadingExistingDocument.java.
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class LoadingExistingDocument {
public static void main(String args[]) throws IOException {
//Loading an existing document
File file = new File("C:/PdfBox_Examples/sample.pdf");
PDDocument document = PDDocument.load(file);
System.out.println("PDF loaded");
//Adding a blank page to the document
document.addPage(new PDPage());
//Saving the document
document.save("C:/PdfBox_Examples/sample.pdf");
//Closing the document
document.close();
}
}
Save /sev/ 保存
File /fal/ 文件
Name /nem/ 名称
[翻译]使用以下命令从命令提示符编译并执行保存的Java文件
[原文]Compile and execute the saved Java file from the command prompt using the following commands
javac LoadingExistingDocument.java
java LoadingExistingDocument
Compile /km'pal/ 编译
Execute /'ek.s.kjut/ 执行
Command /k'maend/ 命令
Prompt /prɑmpt/ 提示符
[翻译]执行上述程序后,将加载指定的PDF文档并向其添加一个空白页面,同时显示以下消息。
[原文]Upon execution, the above program loads the specified PDF document and adds a blank page to it displaying the following message.
PDF loaded
Execution /ek.s'kju.n/ 执行
Loads /lodz/ 加载
Specified /'spes..fad/ 指定的
Document /'dɑ.kj.mnt/ 文档
Blank /blaek/ 空白的
Page /ped/ 页面
Displaying /d'sple./ 显示
Message /'mes.d/ 消息
[翻译]如果您验证指定的路径,可以发现指定的PDF文档中添加了一个额外的页面,如下截图所示。
[原文]If you verify the specified path, you can find an additional page added to the specified PDF document as shown below.
Verify /'ver..fa/ 验证
Specified /'spes..fad/ 指定的
Path /paeθ/ 路径
Additional /'d.n.l/ 额外的
Page /ped/ 页面
Document /'dɑ.kj.mnt/ 文档
Screenshot /'skrin.ɑt/ 截图
更好点的写法如下:
package com.virhuiai.pdfbox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import java.io.File;
import java.io.IOException;
/**
* 用于加载一个PDF文档并向其中添加空白页面
*/
public class E6 {
public static void main(String[] args) {
// 定义输入PDF文件的路径,指定要加载的现有PDF文件
File file = new File("/Volumes/RamDisk/E2.pdf");
// 使用try-with-resources确保PDDocument资源在使用完毕后自动关闭
// PDDocument类属于org.apache.pdfbox.pdmodel包,是PDF文档的内存表示形式
try (PDDocument document = PDDocument.load(file)) {
// 创建一个新的空白页面,PDPage类表示PDF文档中的单个页面
PDPage blankPage = new PDPage();
// 将新创建的空白页面添加到PDF文档中
// addPage()方法用于向PDDocument对象添加页面
document.addPage(blankPage);
// 将修改后的PDF文档保存到指定路径
// save()方法接受文件路径作为参数,保存文档到指定位置
document.save("/Volumes/RamDisk/E6.pdf");
// 打印提示信息,确认PDF文档已成功创建
System.out.println("PDF创建成功");
// 无需显式调用close()方法,try-with-resources会自动关闭PDDocument对象
} catch (IOException e) {
// 捕获并处理可能发生的IO异常,例如文件路径错误或权限问题
// 将IO异常包装为RuntimeException抛出,便于调试
throw new RuntimeException("无法加载或保存PDF文档: " + e.getMessage(), e);
}
}
}