当前位置: 首页 > news >正文

itextPdf生成pdf简单示例

文章环境

jdk1.8,springboot2.6.13

POM依赖

        <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

示例代码

package com.example.example;import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;import java.io.FileOutputStream;
import java.io.IOException;/*** @author bao* @date 2024/3/25 16:10*/
public class SimpleGenPdf {public static void main(String[] args) throws DocumentException,IOException {// 定义中文字体BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font fontCN = new Font(bfChinese, 12, Font.NORMAL);// 步骤1:创建一个大小为A4的文档Document document = new Document(PageSize.A4);try {// 步骤 2:// 我们为document创建一个监听,并把PDF流写到文件中//获取资源文件路径String resourcePath = "./src/main/resources/";PdfWriter.getInstance(document, new FileOutputStream(resourcePath + "/pdf/simplePdf.pdf"));// 步骤 3:打开文档document.open();// 段落Paragraph paragraph = new Paragraph("证明\r\r", new Font(bfChinese, 13, Font.NORMAL));//设置文字居中paragraph.setAlignment(Element.ALIGN_CENTER);//设置左缩进paragraph.setIndentationLeft(12);//设置右缩进paragraph.setIndentationRight(12);//设置首行缩进paragraph.setFirstLineIndent(24);//行间距paragraph.setLeading(15f);//设置段落上空白paragraph.setSpacingBefore(5f);//设置段落下空白paragraph.setSpacingAfter(5f);document.add(paragraph);//第一个表格document.add(new Paragraph("默认情况下的大小---居中 80%", fontCN));// 创建一个有3列的表格PdfPTable userTable = new PdfPTable(3);// 定义一个表格列头PdfPCell tableColumnHead = new PdfPCell(new Paragraph("header with colspan 3"));// 定义一个表格单元的跨度tableColumnHead.setColspan(3);// 把单元加到表格中userTable.addCell(tableColumnHead);//把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行userTable.addCell("1.1");userTable.addCell("2.1");userTable.addCell("3.1");userTable.addCell("1.2");userTable.addCell("2.2");userTable.addCell("3.2");userTable.addCell("1.3");userTable.addCell("2.3");userTable.addCell("3.3");// 增加到文档中document.add(userTable);//第二个表格document.add(new Paragraph("居中 100%", fontCN));PdfPTable centerTable = userTable;// 设置表格大小为可用空白区域的100%centerTable.setWidthPercentage(100);// 增加到文档中2document.add(centerTable);//第三个表格document.add(new Paragraph("居右 50%", fontCN));PdfPTable rightTable = userTable;// 设置表格大小为可用空白区域的50%rightTable.setWidthPercentage(50);// 设置水平对齐方式为 居右rightTable.setHorizontalAlignment(Element.ALIGN_RIGHT);// 增加到文档中3document.add(rightTable);document.add(new Paragraph("居左 50%", fontCN));PdfPTable leftTable = userTable;// 设置水平对齐方式为 居左leftTable.setHorizontalAlignment(Element.ALIGN_LEFT);document.add(leftTable);} catch (Exception de) {de.printStackTrace();}// 步骤 5:关闭文档document.close();}
}

生成结果

git完整项目代码

bnmjstu / itextpdf-simple-example · GitCode

http://www.lryc.cn/news/329741.html

相关文章:

  • 【Linux系列】tree和find命令
  • AI预测福彩3D第23弹【2024年4月1日预测--第4套算法重新开始计算第9次测试】
  • Java常见限流用法介绍和实现
  • 算法——图论:判断二分图(染色问题)
  • 三步提升IEDA下载速度——修改IDEA中镜像地址
  • CentOS7 RPM升级支持BBR TCP/CC的内核版本
  • 文本向量模型BGE与BGE-M3
  • 【黑马头条】-day04自媒体文章审核-阿里云接口-敏感词分析DFA-图像识别OCR-异步调用MQ
  • 新能源充电桩站场AI视频智能分析烟火检测方案及技术特点分析
  • springboot集成logback-spring.xml文件
  • centos7 安装 nginx
  • 29. UE5 RPG应用GamplayAbility
  • http和https的区别!
  • 使用AOP实现打印日志
  • 2024年新算法-冠豪猪优化算法(CPO),CPO-RF-Adaboost,CPO优化随机森林RF-Adaboost回归预测-附代码
  • 浅谈高阶智能驾驶-NOA领航辅助的技术与发展
  • 大模型 智能体 智能玩具 智能音箱 构建教程 wukong-robot
  • Clickhouse-表引擎探索之MergeTree
  • 网络电视盒子哪个好?小编分享电视盒子品牌排行榜
  • 开源模型应用落地-baichuan2模型小试-入门篇(三)
  • 景联文科技高质量大模型训练数据汇总!
  • 【python】正则表达式
  • 学习vue3第十二节(组件的使用与类型)
  • flume配置文件后不能跟注释!!
  • 【docker】Dockerfile自定义镜像
  • webpack项目打包console git分支、打包时间等信息 exec
  • Linux centos7离线搭建FTP
  • 关于GPT-SoVITS语音合成的效果展示(西游之西天送葬团)
  • 如何安装OceanBase的OBD
  • Unity 读写Excel打包后无法运行可能的解决方案