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

使用itext-core生成PDF

1、添加引用依赖包

        <dependency><groupId>com.itextpdf</groupId><artifactId>itext-core</artifactId><version>8.0.3</version><type>pom</type></dependency>

2、上代码

package com.student.demo.pdf;import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.HorizontalAlignment;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;import java.io.File;
import java.io.IOException;/*** @Author: ylj* @Date: 2024/3/20*/
public class PdfCreator1 {//windows 系统字体路径private static final String FONT_PATH = "C:\\Windows\\Fonts\\simsun.ttc,0";public static void main(String[] args) throws IOException {//文件生成地址String pdfFilePath = "D:\\pdf\\accRecFinVou1.pdf";File file = new File(pdfFilePath);//写pdf文件PdfWriter writer = new PdfWriter(file);//pdf文档对象PdfDocument pdf = new PdfDocument(writer);//文档内容Document document = new Document(pdf, PageSize.A4);// 加载宋体字体//PdfFont songFont = PdfFontFactory.createFont("path/to/simsong.ttc,1", PdfEncodings.IDENTITY_H, true);//PdfFont songFont = PdfFontFactory.createFont("simsong.ttc,1");//HeiseiMin-W3 HeiseiKakuGo-W5 KozMinPro-Regular STSong-Light STSongStd-Light MHei-Medium MSung-Light MSungStd-Light HYGoThic-Medium HYSMyeongJo-Medium HYSMyeongJoStd-Medium//PdfFont songFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);PdfFont songFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);// 文件头Paragraph title = new Paragraph("应收账款融资凭证").setFont(songFont).setFontSize(18).setTextAlignment(TextAlignment.CENTER)//.setFixedPosition(pdf.getFirstPage().getPageSize().getWidth() / 2, pdf.getFirstPage().getPageSize().getHeight() - 100).setBold();document.add(title);// 融资日期Paragraph date = new Paragraph("融资日期:       年   月   日").setFont(songFont).setFontSize(10.5f) // 五号字体,近似值.setTextAlignment(TextAlignment.CENTER);//.setFixedPosition(pdf.getFirstPage().getPageSize().getWidth() / 2, pdf.getFirstPage().getPageSize().getHeight() - 130);document.add(date);// 创建表格 11列float[] columnWidths = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; // 定义每列的宽度比例Table table = new Table(columnWidths);table.setWidth(530);table.setHorizontalAlignment(HorizontalAlignment.CENTER);// 第1行new Cell(所占行数, 所占列数)table.addCell(new Cell(1, 1).add(new Paragraph("第1行").setHeight(40).setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));table.addCell(new Cell(1, 1).add(new Paragraph("XXXXXXXXXXXXXXXXXXX公司").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));table.addCell(new Cell(1, 1).add(new Paragraph("保理商名称").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));table.addCell(new Cell(1, 8).add(new Paragraph("XXXXXXXXXXXXXXXXXXXX公司").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));// 第2行table.addCell(new Cell(2, 1).add(new Paragraph("第2行").setHeight(40).setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));table.addCell(new Cell(2, 2).add(new Paragraph("    仟   佰   拾   万   仟   佰   拾   元整").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));table.addCell(new Cell(1, 1).add(new Paragraph("千").setFont(songFont).setFontSize(10.5f)));table.addCell(new Cell(1, 1).add(new Paragraph("百").setFont(songFont).setFontSize(10.5f)));table.addCell(new Cell(1, 1).add(new Paragraph("十").setFont(songFont).setFontSize(10.5f)));table.addCell(new Cell(1, 1).add(new Paragraph("万").setFont(songFont).setFontSize(10.5f)));table.addCell(new Cell(1, 1).add(new Paragraph("千").setFont(songFont).setFontSize(10.5f)));table.addCell(new Cell(1, 1).add(new Paragraph("百").setFont(songFont).setFontSize(10.5f)));table.addCell(new Cell(1, 1).add(new Paragraph("十").setFont(songFont).setFontSize(10.5f)));table.addCell(new Cell(1, 1).add(new Paragraph("元").setFont(songFont).setFontSize(10.5f)));//第3行for (int i = 1; i<=8; i++){table.addCell(new Cell(1, 1));}// 第4行table.addCell(new Cell(1, 1).add(new Paragraph("第4行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));table.addCell(new Cell(3, 1).add(new Paragraph("表格").setFont(songFont)).setVerticalAlignment(VerticalAlignment.MIDDLE));table.addCell(new Cell(3, 8).setFont(songFont));// 第5行table.addCell(new Cell(1, 1).add(new Paragraph("第5行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));// 第6行table.addCell(new Cell(1, 1).add(new Paragraph("第6行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));// 第7行table.addCell(new Cell(1, 1).add(new Paragraph("第7行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));table.addCell(new Cell(3, 1).add(new Paragraph("编号").setFont(songFont)).setVerticalAlignment(VerticalAlignment.MIDDLE));table.addCell(new Cell(3, 8).setFont(songFont));// 第8行table.addCell(new Cell(1, 1).add(new Paragraph("第8行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));// 第9行table.addCell(new Cell(1, 1).add(new Paragraph("第9行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));// 第10行table.addCell(new Cell(1, 1).add(new Paragraph("第10行").setHeight(80).setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));table.addCell(new Cell(1, 1).setTextAlignment(TextAlignment.CENTER));table.addCell(new Cell(1, 1).add(new Paragraph("签章").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));table.addCell(new Cell(1, 8).setTextAlignment(TextAlignment.CENTER));//添加表格到文档document.add(table);// 关闭文档document.close();System.out.println("PDF生成结束======>");}
}

3、效果图

最后注意几个点:

1、字体问题:itext不支持中文,在显示过程中会碰到中文不显示问题,上面给的代码是支持中文宋体的。

//使用的是系统自带支持的中文宋体
PdfFont songFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
//使用系统路径下的字体(windows和linux系统对应目录下都有,自行查询目录位置)
PdfFont songFont = PdfFontFactory.createFont(FONT_PATH);

2、版本问题,不同版本之间存在有些方法调用的名称和参数发生了变化,这个需要注意。

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

相关文章:

  • 接口自动化框架搭建(四):pytest的使用
  • 蓝桥杯算法基础(34)深度优先搜索DFS(数独游戏)(部分和)(水洼数目)(八皇后问题)(素数环)(困难的串)
  • 蓝桥杯备考随手记: Math 类中常用方法
  • 外包干了4年,技术退步明显。。。。
  • 亚远景科技-Hardware Engineering SPICE课程大纲
  • JDK8的下载安装与环境变量配置教程
  • 深入探讨分布式ID生成方案
  • 花钱的艺术:消费和投资如何分配
  • git 代码库查看方法
  • MySql的下载与安装
  • python学习17:python中的while循环
  • Android中的导航navigation的使用
  • Clip算法解读
  • 使用第三方远程连接工具ssh连接vagrant创建的虚拟机
  • linux查找指定目录下包含指定字符串文件,包含子目录
  • 27. BI - PageRank 的那些相关算法 - PersonRank, TextRank, EdgeRank
  • [数据集][目标检测]公共场所危险物品检测数据集VOC+YOLO格式1431张6类别
  • 创业项目开发(持续更新)
  • 基于SpringBoot的“校园台球厅人员与设备管理系统”的设计与实现(源码+数据库+文档+PPT)
  • 【Java数据结构】关于栈的操作出栈,压栈,中缀表达式,后缀表达式,逆波兰表达式详解
  • wireshark 使用
  • C++纯虚函数的使用
  • 读所罗门的密码笔记06_共生思想(上)
  • QA:ubuntu22.04.4桌面版虚拟机鼠标丢失的解决方法
  • idea从零开发Android 安卓 (超详细)
  • 1.5T数据惨遭Lockbit3.0窃取,亚信安全发布《勒索家族和勒索事件监控报告》
  • 喜讯!聚铭网络荣获《日志分类方法及系统》发明专利
  • 每日一博 - 关于日志记录的最佳实践
  • 针对pycharm打开新项目需要重新下载tensorflow的问题解决
  • <商务世界>《第29课 外贸展会上应注意的事项》