引入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
代码示例,直接使用
package com.sboot.dzp.test;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class PdfExportTest {
@Test
public void test2() throws DocumentException, IOException {
String outFilePath = "C:\\Users\\HZZF\\Desktop\\aa.pdf";
File file = new File(outFilePath);
file.createNewFile();
Document document = new Document(PageSize.A4, 25, 25, 20, 38);
//设置输出流
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(outFilePath));
//打开文档
document.open();
//设置字体,解决中文不显示
BaseFont bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
addTitle(document, new Font(bfChinese, 18));
addSubTitle(document, "第(2023)10号", new Font(bfChinese, 12));
Font font = new Font(bfChinese, 14);
PdfPTable pdfPTable1 = new PdfPTable(5);
pdfPTable1.setWidthPercentage(100);
//第1行
pdfPTable1.addCell(createCell("申请人", font));
pdfPTable1.addCell(createCell("ADMIN", font, 4));
//第二行
pdfPTable1.addCell(createCell("手机号码", font));
pdfPTable1.addCell(createCell("13111111111", font, 4));
//第三行
pdfPTable1.addCell(createCell("地址", font));
pdfPTable1.addCell(createCell("XXXXXXXXX", font, 4));
//第4行
pdfPTable1.addCell(createCell("土地面积", font));
pdfPTable1.addCell(createCell("202.33平方米", font));
pdfPTable1.addCell(createCell("房屋面积", font, 1, 2));
pdfPTable1.addCell(createCell("有证", font));
pdfPTable1.addCell(createCell("", font));
//第5行
pdfPTable1.addCell(createCell("权利状况", font));
pdfPTable1.addCell(createCell("国有", font));
pdfPTable1.addCell(createCell("无证", font));
pdfPTable1.addCell(createCell("", font));
//第6行
pdfPTable1.addCell(createCell("原土地用途", font));
pdfPTable1.addCell(createCell("", font));
pdfPTable1.addCell(createCell("规划用途", font));
pdfPTable1.addCell(createCell("住宅", font, 2));
//第7行 content
Paragraph paragraph = new Paragraph("位于长沙国际会展中心的主展馆设立了“六馆一区”,展览面积达10万平方米。据介绍,博览会将集中展示非洲的红酒、咖啡、手工艺品等特色商品,以及中国的工程机械、医疗器械、日用百货、农机设备等。在主展馆的W1馆,布展工作人员正在对中国远洋海运集团有限公司、中国通用技术(集团)控股有限责任公司等中央企业展区进行外观结构施工。", font);
//首行缩进
paragraph.setFirstLineIndent(font.getSize() * 2);
PdfPCell cell = createCell(paragraph, 5, 1);
cell.setFixedHeight(150);
pdfPTable1.addCell(cell);
paragraph = new Paragraph("经办人:", font);
setAbsolute(new int[]{30, 380, 300, 410}, paragraph, pdfWriter);
paragraph = new Paragraph("2023年6月27日", font);
setAbsolute(new int[]{430, 380, 600, 410}, paragraph, pdfWriter);
//第8行
cell = createCell("人事意见", font);
cell.setFixedHeight(100);
pdfPTable1.addCell(cell);
pdfPTable1.addCell(createCell("", font, 4));
//第9行
cell = createCell("分管领导意见", font);
cell.setFixedHeight(100);
pdfPTable1.addCell(cell);
pdfPTable1.addCell(createCell("", font, 4));
//第10行
cell = createCell("BOSS意见", font);
cell.setFixedHeight(100);
pdfPTable1.addCell(cell);
pdfPTable1.addCell(createCell("", font, 4));
document.add(pdfPTable1);
document.close();
}
private void setAbsolute(int[] position, Element e, PdfWriter pdfWriter) throws DocumentException {
Rectangle rectangle = new Rectangle(position[0], position[1], position[2], position[3]);
ColumnText ct = new ColumnText(pdfWriter.getDirectContent());
ct.addElement(e);
ct.setSimpleColumn(rectangle);
ct.go();
}
private PdfPCell createCell(String value, Font fontZH) {
return createCell(value, fontZH, 1, 1);
}
private PdfPCell createCell(String value, Font fontZH, int colspan) {
return createCell(value, fontZH, colspan, 1);
}
private PdfPCell createCell(Paragraph value, int colspan, int rowspan) {
PdfPCell cell = new PdfPCell();
cell.addElement(value);
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setUseAscender(true);
cell.setUseDescender(true);
return cell;
}
private PdfPCell createCell(String value, Font fontZH, int colspan, int rowspan) {
PdfPCell cell = new PdfPCell();
cell.setPhrase(new Phrase(value, fontZH));
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setUseAscender(true);
cell.setFixedHeight(40);
cell.setUseDescender(true);
//水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
return cell;
}
private void addTitle(Document document, Font font) throws DocumentException, IOException {
Paragraph paragraph = new Paragraph("申请表", font);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
}
private void addSubTitle(Document document, String text, Font font) throws DocumentException {
Chunk chunk = new Chunk(text, font);
chunk.setLineHeight(20);
chunk.setTextRise(5);//调整文字上下距离
Paragraph paragraph = new Paragraph(chunk);
paragraph.setAlignment(Element.ALIGN_RIGHT);
document.add(paragraph);
}
}