百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

18.在PDF文档中创建矩形框(JAVA+PDFBOX)

wptr33 2025-08-02 22:16 3 浏览

[翻译] 本章将教您如何在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/ 观察

相关推荐

MySql系列-常用命令

本篇是对...

Record.ToTable 格式转换

本期案例对表格格式进行转换,前后转换效果如下:解题套路1.Record.ToTable解题思路:思路就是构造可以透视的样式。使用Record.ToTable对行记录进行转换,获得包含两列的表,首行可以...

Table.Group 按时期累计计算唯一值

本期案例是根据不同id进行汇总统计:组内,相同日期的为一组,统计“from”、“to”中的非重复个数;连续日期的,统计累计数。前后转换效果如下:解题套路1.Table.Group...

MySQL 9.1正式发布,有哪些值得关注的新特性?

MySQL创新版9.1.0于2024年10月15日正式发布。此外,MySQL8.0.40及8.4.3补丁版本也同时发布。8.4.3是目前MySQL的LTS长期支持版本,该版本中将不会增加新的功能与特性...

SQL基本语句练习(基础版)

最近在学习SQL基本语句的练习,在此分享一下笔者做过的练习以及个人的解决教程:首先是基本练习表格的搭建,具体内容如下表所示:...

SQL 从入门到精通:全面掌握数据库操作

学习SQL(StructuredQueryLanguage)是掌握数据库操作的关键步骤。SQL是一种用于管理和处理关系型数据库的标准语言,广泛应用于数据检索、插入、更新和删除等操作。以下是一些...

ClickHouse学习笔记四ClickHouse基础语法

前言这里我们介绍ClickHouse的基本语法,使用环境是腾讯云的ClickHouse。默认情况下,ClickHouse在进行集群纬度执行建表等DDL操作时需要手动添加ONCLUSTERX...

程序员总结的常用sql语句大全

多年经验程序员总结的我们一般需要使用的sql语句,赶快收藏起来,方便以后使用。以下是一些常用的SQL语句及其用法:一、数据定义语言(DDL)创建库CREATEDATABASE:创建一个新数据库。...

PQ03-分组求和

目标已知:销售清单求:每个销售员的销量合计方法数据准备...

好荐:一款数据库元数据管理平台工具

“元数据”的定义在不同的软件、项目、工程的定义范围都不太一样。本文这里指的是软件项目开发使用的数据库表结构信息。我今天介绍的这个开源项目叫Databasir,它是一个面向团队的关系型数据库模型文档管理...

MySQL 8.0 SQL优化黑科技,面试官都不一定知道!

前言提到SQL优化,大多数人想到的还是那些经典套路:建索引、避免全表扫描、优化JOIN顺序…这些确实是基础,但如果你还停留在MySQL5.7时代的优化思维,那就out了。MySQL8.0已经发布好...

MySQL数据库深度优化指南:从基础到架构层面的20个关键策略

一、核心性能优化原则数据最小化原则...

动物源性食品中兽药残留的检测——喹啉类药物残留

喹啉类药物(quinoxaline)是具有喹啉-N1,N4-二氧化物基本结构的一类化学合成的动物专用药,具有广谱抗菌、提高饲料转化率和促生长作用。1965年德国拜耳公司以邻硝基苯胺为原料合成喹乙醇(o...

适合普通开发者和产品经理的PHP应用模板开发AI的SaaS应用框架

简单到傻!Liang_SaaS适合普通开发者和产品经理的PHP应用模板开发AI的SaaS应用框架,利用Php开发AI的SaaS应用框架,是一个强大的内容管理仪表板模板,基于Bootstrap和...

Power Query 交错合并表格的方法

两张表格合并成一张表格,需要交错排列,表1取一行,表2取一行,这样排列在一起:前提是两张表的行数相同,内容排列顺序相同:我们来看两张表:表1:12列10行表2:11列10行行数相同列数不同,我们在数据...