[翻译] 本章将教您如何在PDF文档的页面中创建彩色矩形框。
[原文] This chapter teaches you how to create color boxes in a page of a PDF document.
Creating Boxes in a PDF Document 在PDF文档中创建矩形框
[翻译]
您可以使用PDPageContentStream类的addRect()方法在PDF页面中添加矩形框。
[原文]
You can add rectangular boxes in a PDF page using the addRect() method of the PDPageContentStream class.
- rectangular /rk'taeɡjlr/ 矩形的
- box /bɑks/ 框
- method /'mθd/ 方法
[翻译]
以下是在PDF文档页面中创建矩形形状的步骤。
[原文]
Following are the steps to create rectangular shapes in a page of a PDF document.
- following /'fɑlo/ 以下的
- step /stp/ 步骤
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 document = PDDocument.load(file);
- load /lod/ 加载
- static /'staetk/ 静态的
- parameter /p'raemtr/ 参数
- invoke /n'vok/ 调用
Step 2: Getting the Page Object 步骤2:获取页面对象
[翻译]
您需要使用PDDocument类的以下方法获取所需页面的PDPage对象:
- 获取指定页面索引的页面对象
[原文]
You need to retrieve the PDPage object of the required page where you want to add rectangles using the getPage() method of the PDDocument class. To this method you need to pass the index of the page where you want to add rectangles.
PDPage page = document.getPage(0);
- retrieve /r'triv/ 检索
- page /ped/ 页面
- index /'ndks/ 索引
Step 3: Preparing the Content Stream 步骤3:准备内容流
[翻译]
您可以使用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.
PDPageContentStream contentStream = new PDPageContentStream(document, page);
- insert /n'srt/ 插入
- constructor /kn'strktr/ 构造函数
- instantiate /n'staeniet/ 实例化
- previous /'privis/ 之前的
Step 4: Setting the Non-stroking Color 步骤4:设置非描边颜色
[翻译]
您可以使用PDPageContentStream类的以下方法为矩形设置非描边颜色:
- 设置非描边颜色
[原文]
You can set the non-stroking color to the rectangle using the setNonStrokingColor() method of the class PDPageContentStream. To this method, you need to pass the required color as a parameter as shown below.
contentStream.setNonStrokingColor(Color.DARK_GRAY);
- non-stroking /nn'strok/ 非描边的
- color /'klr/ 颜色
- parameter /p'raemtr/ 参数
Step 5: Drawing the rectangle 步骤5:绘制矩形
[翻译]
使用addRect()方法绘制具有所需尺寸的矩形。向该方法传递要添加的矩形的尺寸,如下所示。
[原文]
Draw the rectangle with required dimensions using the addRect() method. To this method, you need to pass the dimensions of the rectangle that is to be added as shown below.
contentStream.addRect(200, 650, 100, 100);
- draw /dr/ 绘制
- dimension /d'mnn/ 尺寸
- rectangle /'rktaeɡl/ 矩形
Step 6: Filling the Rectangle 步骤6:填充矩形
[翻译]
PDPageContentStream类的fill()方法使用指定的颜色填充指定尺寸之间的路径,如下所示。
[原文]
The fill() method of the PDPageContentStream class fills the path between the specified dimensions with the required color as shown below.
contentStream.fill();
- fill /fl/ 填充
- path /paeθ/ 路径
- specified /'spsfad/ 指定的
Step 7: Closing the Document 步骤7:关闭文档
[翻译]
最后,使用PDDocument类的close()方法关闭文档,如下所示。
[原文]
Finally close the document using close() method of the PDDocument class as shown below.
document.close();
- finally /'fanli/ 最后
- close /kloz/ 关闭
Example 示例
[翻译]
假设我们有一个名为blankpage.pdf的PDF文档,位于*C:\PdfBox_Examples*路径中,该文档包含一个空白页面,如下图所示。
[原文]
Suppose we have a PDF document named blankpage.pdf in the path *C:\PdfBox_Examples* and this contains a single blank page as shown below.
- suppose /s'poz/ 假设
- blank /blaek/ 空白的
[翻译]
本示例演示如何在PDF文档中创建/插入矩形。在此,我们将在一个空白PDF中创建一个矩形框。将此代码保存为AddRectangles.java。
[原文]
This example demonstrates how to create/insert rectangles in a PDF document. Here, we will create a box in a Blank PDF. Save this code as AddRectangles.java.
import java.awt.Color;
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
public class ShowColorBoxes {
public static void main(String args[]) throws Exception {
//Loading an existing document
File file = new File("C:/PdfBox_Examples/BlankPage.pdf");
PDDocument document = PDDocument.load(file);
//Retrieving a page of the PDF Document
PDPage page = document.getPage(0);
//Instantiating the PDPageContentStream class
PDPageContentStream contentStream = new PDPageContentStream(document, page);
//Setting the non stroking color
contentStream.setNonStrokingColor(Color.DARK_GRAY);
//Drawing a rectangle
contentStream.addRect(200, 650, 100, 100);
//Drawing a rectangle
contentStream.fill();
System.out.println("rectangle added");
//Closing the ContentStream object
contentStream.close();
//Saving the document
File file1 = new File("C:/PdfBox_Examples/colorbox.pdf");
document.save(file1);
//Closing the document
document.close();
}
}
- demonstrate /'dmnstret/ 演示
- insert /n'srt/ 插入
[翻译]
使用以下命令从命令提示符编译并执行保存的Java文件。
[原文]
Compile and execute the saved Java file from the command prompt using the following commands.
javac AddRectangles.java
java AddRectangles
- compile /km'pal/ 编译
- execute /'kskjut/ 执行
- command /k'maend/ 命令
- prompt /prɑmpt/ 提示符
[翻译]
执行后,上述程序会在PDF文档中创建一个矩形,并显示以下消息。
[原文]
Upon execution, the above program creates a rectangle in a PDF document displaying the following image.
Rectangle created
- upon /'pɑn/ 在……时
- create /kri'et/ 创建
[翻译]
如果您检查指定路径并打开保存的文档colorbox.pdf,可以观察到其中插入了一个矩形框,如下图所示。
[原文]
If you verify the given path and open the saved document colorbox.pdf, you can observe that a box is inserted in it as shown below.
- verify /'vrfa/ 验证
- observe /b'zrv/ 观察