18.在PDF文档中创建矩形框(JAVA+PDFBOX)
wptr33 2025-08-02 22:16 19 浏览
[翻译] 本章将教您如何在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/ 观察
相关推荐
- oracle数据导入导出_oracle数据导入导出工具
-
关于oracle的数据导入导出,这个功能的使用场景,一般是换服务环境,把原先的oracle数据导入到另外一台oracle数据库,或者导出备份使用。只不过oracle的导入导出命令不好记忆,稍稍有点复杂...
- 继续学习Python中的while true/break语句
-
上次讲到if语句的用法,大家在微信公众号问了小编很多问题,那么小编在这几种解决一下,1.else和elif是子模块,不能单独使用2.一个if语句中可以包括很多个elif语句,但结尾只能有一个...
- python continue和break的区别_python中break语句和continue语句的区别
-
python中循环语句经常会使用continue和break,那么这2者的区别是?continue是跳出本次循环,进行下一次循环;break是跳出整个循环;例如:...
- 简单学Python——关键字6——break和continue
-
Python退出循环,有break语句和continue语句两种实现方式。break语句和continue语句的区别:break语句作用是终止循环。continue语句作用是跳出本轮循环,继续下一次循...
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
-
用for循环或者while循环时,如果要在循环体内直接退出循环,可以使用break语句。比如计算1至100的整数和,我们用while来实现:sum=0x=1whileTrue...
- Python 中 break 和 continue 傻傻分不清
-
大家好啊,我是大田。...
- python中的流程控制语句:continue、break 和 return使用方法
-
Python中,continue、break和return是控制流程的关键语句,用于在循环或函数中提前退出或跳过某些操作。它们的用途和区别如下:1.continue(跳过当前循环的剩余部分,进...
- L017:continue和break - 教程文案
-
continue和break在Python中,continue和break是用于控制循环(如for和while)执行流程的关键字,它们的作用如下:1.continue:跳过当前迭代,...
- 作为前端开发者,你都经历过怎样的面试?
-
已经裸辞1个月了,最近开始投简历找工作,遇到各种各样的面试,今天分享一下。其实在职的时候也做过面试官,面试官时,感觉自己问的问题很难区分候选人的能力,最好的办法就是看看候选人的github上的代码仓库...
- 面试被问 const 是否不可变?这样回答才显功底
-
作为前端开发者,我在学习ES6特性时,总被const的"善变"搞得一头雾水——为什么用const声明的数组还能push元素?为什么基本类型赋值就会报错?直到翻遍MDN文档、对着内存图反...
- 2023金九银十必看前端面试题!2w字精品!
-
导文2023金九银十必看前端面试题!金九银十黄金期来了想要跳槽的小伙伴快来看啊CSS1.请解释CSS的盒模型是什么,并描述其组成部分。...
- 前端面试总结_前端面试题整理
-
记得当时大二的时候,看到实验室的学长学姐忙于各种春招,有些收获了大厂offer,有些还在苦苦面试,其实那时候的心里还蛮忐忑的,不知道自己大三的时候会是什么样的一个水平,所以从19年的寒假放完,大二下学...
- 由浅入深,66条JavaScript面试知识点(七)
-
作者:JakeZhang转发链接:https://juejin.im/post/5ef8377f6fb9a07e693a6061目录...
- 2024前端面试真题之—VUE篇_前端面试题vue2020及答案
-
添加图片注释,不超过140字(可选)...
- 今年最常见的前端面试题,你会做几道?
-
在面试或招聘前端开发人员时,期望、现实和需求之间总是存在着巨大差距。面试其实是一个交流想法的地方,挑战人们的思考方式,并客观地分析给定的问题。可以通过面试了解人们如何做出决策,了解一个人对技术和解决问...
- 一周热门
- 最近发表
-
- oracle数据导入导出_oracle数据导入导出工具
- 继续学习Python中的while true/break语句
- python continue和break的区别_python中break语句和continue语句的区别
- 简单学Python——关键字6——break和continue
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
- Python 中 break 和 continue 傻傻分不清
- python中的流程控制语句:continue、break 和 return使用方法
- L017:continue和break - 教程文案
- 作为前端开发者,你都经历过怎样的面试?
- 面试被问 const 是否不可变?这样回答才显功底
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)
