[翻译] 在前一章中,我们已经了解了如何从现有 PDF 文档中提取文本。在本章中,我们将讨论如何向 PDF 文档中插入图片。
[原文] In the previous chapter, we have seen how to extract text from an existing PDF document. In this chapter, we will discuss how to insert image to a PDF document.
previous /'privis/ 之前的 extract /k'straekt/ 提取 insert /n'srt/ 插入 image /'md/ 图片
Inserting Image to a PDF Document 插入图片到 PDF 文档
[翻译] 您可以使用 PDImageXObject 和 PDPageContentStream 类的 createFromFile() 和 drawImage() 方法向 PDF 文档中插入图片。
[原文] You can insert an image into a PDF document using the createFromFile() and drawImage() methods of the classes PDImageXObject and PDPageContentStream respectively.
insert /n'srt/ 插入 image /'md/ 图片 respectively /r'spektvli/ 分别地
Step 1: Loading an Existing PDF Document 第一步:加载现有 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.
load /lod/ 加载 static /'staetk/ 静态的 invoke /n'vok/ 调用 parameter /p'raemtr/ 参数
File file = new File("path of the document")
PDDocument doc = PDDocument.load(file);
Step 2: Retrieving a Page 第二步:获取页面
[翻译] 在 PDF 文档中选择一个页面,并使用 getPage() 方法获取其页面对象,如下所示。
[原文] Select a page in the PDF document and retrieve its page object using the getPage() method as shown below.
retrieve /r'triv/ 检索 page /ped/ 页面
PDPage page = doc.getPage(0);
Step 3: Creating PDImageXObject object 第三步:创建 PDImageXObject 对象
[翻译] PDFBox 库中的 PDImageXObject 类表示一个图片。它提供了与图片相关的所有必要方法,例如插入图片、设置高度、设置宽度等。
我们可以使用 createFromFile() 方法创建该类的对象。在此方法中,我们需要传递要添加的图片路径(以字符串形式)以及需要添加图片的文档对象。
[原文] The class PDImageXObject in PDFBox library represents an image. It provides all the required methods to perform operations related to an image, such as, inserting an image, setting its height, setting its width etc.
We can create an object of this class using the method createFromFile(). To this method, we need to pass the path of the image which we want to add in the form of a string and the document object to which the image needs to be added.
represent /repr'zent/ 表示 provide /pr'vad/ 提供 perform /pr'frm/ 执行 operation /ɑp'ren/ 操作
PDImageXObject pdImage = PDImageXObject.createFromFile("C:/logo.png", doc);
Step 4: Preparing the Content Stream 第四步:准备内容流
[翻译] 您可以使用 PDPageContentStream 类的对象插入各种数据元素。您需要将文档对象和页面对象传递给该类的构造函数,因此,通过传递前几步中创建的这两个对象来实例化该类,如下所示。
[原文] You can insert various kinds of data elements using the object of the class named PDPageContentStream. You need to pass the document object and the page object to the constructor of this class therefore, instantiate this class by passing these two objects created in the previous steps as shown below.
various /'vers/ 各种 element /'elmnt/ 元素 constructor /kn'strktr/ 构造函数 instantiate /n'staeniet/ 实例化
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
Step 5: Drawing the Image in the PDF Document 第五步:在 PDF 文档中绘制图片
[翻译] 您可以使用 drawImage() 方法在 PDF 文档中插入图片。在此方法中,您需要添加在上一步中创建的图片对象以及图片所需的尺寸(宽度和高度),如下所示。
[原文] You can insert an image in the PDF document using the drawImage() method. To this method, you need to add the image object created in the above step and the required dimensions of the image (width and height) as shown below.
draw /dr/ 绘制 dimension /d'menn/ 尺寸
contentstream.drawImage(pdImage, 70, 250);
Step 6: Closing the PDPageContentStream 第六步:关闭 PDPageContentStream
[翻译] 使用 close() 方法关闭 PDPageContentStream 对象,如下所示。
[原文] Close the PDPageContentStream object using the close() method as shown below.
close /kloz/ 关闭
contentstream.close();
Step 7: Saving the Document 第七步:保存文档
[翻译] 添加所需内容后,使用 PDDocument 类的 save() 方法保存 PDF 文档,如下面的代码块所示。
[原文] After adding the required content, save the PDF document using the save() method of the PDDocument class as shown in the following code block.
save /sev/ 保存 content /'kɑntent/ 内容
doc.save("Path");
Step 8: Closing the Document 第八步:关闭文档
[翻译] 最后,使用 PDDocument 类的 close() 方法关闭文档,如下所示。
[原文] Finally, close the document using the close() method of the PDDocument class as shown below.
finally /'fanli/ 最后 close /kloz/ 关闭
doc.close();
Example 示例
[翻译] 假设我们有一个名为 sample.pdf 的 PDF 文档,位于 C:/PdfBox_Examples/ 路径中,包含空白页面,如下所示。
[原文] Suppose we have a PDF document named sample.pdf, in the path C:/PdfBox_Examples/ with empty pages as shown below.
suppose /s'poz/ 假设 empty /'empti/ 空的
[翻译] 本示例演示如何向上述 PDF 文档的空白页面添加图片。在此,我们将加载名为 sample.pdf 的 PDF 文档并向其添加图片。将此代码保存在名为 InsertingImage.java 的文件中。
[原文] This example demonstrates how to add image to a blank page of the above mentioned PDF document. Here, we will load the PDF document named sample.pdf and add image to it. Save this code in a file with name InsertingImage.java.
demonstrate /'demnstret/ 演示 blank /blaek/ 空白的
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
public class InsertingImage {
public static void main(String args[]) throws Exception {
//Loading an existing document
File file = new File("C:/PdfBox_Examples/sample.pdf");
PDDocument doc = PDDocument.load(file);
//Retrieving the page
PDPage page = doc.getPage(0);
//Creating PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile("C:/PdfBox_Examples/logo.png",doc);
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(doc, page);
//Drawing the image in the PDF document
contents.drawImage(pdImage, 70, 250);
System.out.println("Image inserted");
//Closing the PDPageContentStream object
contents.close();
//Saving the document
doc.save("C:/PdfBox_Examples/sample.pdf");
//Closing the document
doc.close();
}
}
[翻译] 从命令提示符编译并执行保存的 Java 文件,使用以下命令。
[原文] Compile and execute the saved Java file from the command prompt using the following commands.
compile /km'pal/ 编译 execute /'ekskjut/ 执行 command /k'maend/ 命令 prompt /prɑmpt/ 提示符
javac InsertingImage.java
java InsertingImage
[翻译] 执行后,上述程序会将图片插入到指定 PDF 文档的指定页面,并显示以下消息。
[原文] Upon execution, the above program inserts an image into the specified page of the given PDF document displaying the following message.
upon /'pɑn/ 在……时 display /d'sple/ 显示
Image inserted
[翻译] 如果您验证文档 sample.pdf,可以观察到其中插入了一张图片,如下所示。
[原文] If you verify the document sample.pdf, you can observe that an image is inserted in it as shown below.
verify /'verfa/ 验证 observe /b'zrv/ 观察
列表形式替换 get 格式
以下是将涉及 getPage() 的部分改为列表形式的翻译:
第二步:检索页面
- 在 PDF 文档中选择一个页面。
- 使用 getPage() 方法检索其页面对象,如下所示:
PDPage page = doc.getPage(0);