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