什么是itext呢?
在我们工作实际项目开发中,会有大量的统计报表数据产生,这个时候除了会根据需求生成word,excel文件外,pdf无疑也是最常见的文件创建方式了,目前itext在Github中提供了Java和C#的itext7-dotnet创建使用,有多种历史版本选择,目前最新版本为:iText 7 Core 7.2.3
根据官网中的显示,itext7在进行开展商业活动的时候并不是完全免费的,一旦您开展商业活动,在您的产品中分发 iText 软件或将其部署到网络上,而无需根据 AGPL 许可披露您自己的应用程序的源代码,就必须购买许可。
itext官网地址:https://itextpdf.com/products/itext-7 学习了解更多相关的内容
itext项目依赖
在我们项目中需要通过itext创建并pdf文件时,需要提前导入好itext的maven依赖,下面依赖版本供参考
<!--pdf核心依赖-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<!--用于输出中文,根据需求添加-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--设置pdf文件密码,根据需求添加-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
</dependency>itext
通过创建pdf过程示例
创建一个CreatePdfController控制器
package com.cx.ssm.controller;
import com.cx.ssm.service.CreatePdfService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("interface/CreatePdf")
public class CreatePdfController {
@Resource
CreatePdfService createPdfService;
@RequestMapping("createPdfFile")
public Boolean createPdfFile(){
return createPdfService.createPdfFile();
}
}
创建一个CreatePdfService业务逻辑层
package com.cx.ssm.service;
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 org.springframework.stereotype.Service;
import java.io.FileOutputStream;
import java.io.IOException;
@Service
public class CreatePdfService {
public Boolean createPdfFile() {
Document document = new Document(PageSize.A4);
document.setMargins(-50, -50, 15, 5);
FileOutputStream fileOutputStream = null;
PdfWriter pdfWriter = null;
try {
fileOutputStream = new FileOutputStream("C:/Users/xx/Downloads/个人就业信息登记表模板.pdf");
pdfWriter= PdfWriter.getInstance(document, fileOutputStream);
BaseFont bfChinese = null;
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font titlefont = new Font(bfChinese, 16, Font.BOLD);
Font keyfont = new Font(bfChinese, 10, Font.BOLD);
Font textfont = new Font(bfChinese, 10);
document.open();
PdfPTable usernameInfoPdfPTable = new PdfPTable(1);
PdfPCell usernameInfo = new PdfPCell(new Paragraph("个人就业信息登记表模板", titlefont));
usernameInfo.setFixedHeight(43);
usernameInfo.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
//水平垂直居中
usernameInfo.setVerticalAlignment(Element.ALIGN_TOP);
usernameInfo.setBorder(0);
//设置每列宽度比例
int[] widthUsername= {100};
usernameInfoPdfPTable.setWidths(widthUsername);
usernameInfoPdfPTable.getDefaultCell().setBorder(0);
usernameInfoPdfPTable.addCell(usernameInfo);
document.add(usernameInfoPdfPTable);
//第三列
PdfPTable oneLinePdfPTable = new PdfPTable(6);
//设置每列宽度比例
int[] oneLineWidthPdfPTable = {14, 12, 14, 12, 14, 14};
oneLinePdfPTable.setWidths(oneLineWidthPdfPTable);
oneLinePdfPTable.getDefaultCell().setBorder(0);
PdfPCell name = new PdfPCell(new Paragraph("姓名", keyfont));
PdfPCell username = new PdfPCell(new Paragraph("张三", textfont));
PdfPCell sex = new PdfPCell(new Paragraph("性别", keyfont));
PdfPCell sexValue = new PdfPCell(new Paragraph("男", textfont));
PdfPCell like = new PdfPCell(new Paragraph("爱好", keyfont));
PdfPCell likeValue = new PdfPCell(new Paragraph("打篮球", textfont));
name.setFixedHeight(25);
name.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
username.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
sex.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
sexValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
like.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
likeValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
name.setVerticalAlignment(Element.ALIGN_MIDDLE);
username.setVerticalAlignment(Element.ALIGN_MIDDLE);
sex.setVerticalAlignment(Element.ALIGN_MIDDLE);
sexValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
like.setVerticalAlignment(Element.ALIGN_MIDDLE);
likeValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
oneLinePdfPTable.addCell(name);
oneLinePdfPTable.addCell(username);
oneLinePdfPTable.addCell(sex);
oneLinePdfPTable.addCell(sexValue);
oneLinePdfPTable.addCell(like);
oneLinePdfPTable.addCell(likeValue);
document.add(oneLinePdfPTable);
PdfPTable twoLinePdfPTable = new PdfPTable(6);
int[] twoLineWidthPdfPTable = {14, 12, 14, 12, 14, 14};
twoLinePdfPTable.setWidths(twoLineWidthPdfPTable);
twoLinePdfPTable.getDefaultCell().setBorder(0);
PdfPCell idCard = new PdfPCell(new Paragraph("身份证号码", keyfont));
PdfPCell idCardValue = new PdfPCell(new Paragraph("xxxxxxxx", textfont));
PdfPCell address = new PdfPCell(new Paragraph("家庭地址", keyfont));
PdfPCell addressValue = new PdfPCell(new Paragraph("中国", textfont));
PdfPCell health = new PdfPCell(new Paragraph("健康情况", keyfont));
PdfPCell healthValue = new PdfPCell(new Paragraph("良", textfont));
idCard.setFixedHeight(25);
idCard.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
idCardValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
address.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
addressValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
health.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
healthValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
idCard.setVerticalAlignment(Element.ALIGN_MIDDLE);
idCardValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
address.setVerticalAlignment(Element.ALIGN_MIDDLE);
addressValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
health.setVerticalAlignment(Element.ALIGN_MIDDLE);
healthValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
twoLinePdfPTable.addCell(idCard);
twoLinePdfPTable.addCell(idCardValue);
twoLinePdfPTable.addCell(address);
twoLinePdfPTable.addCell(addressValue);
twoLinePdfPTable.addCell(health);
twoLinePdfPTable.addCell(healthValue);
document.add(twoLinePdfPTable);
PdfPTable threeLinePdfPTable = new PdfPTable(6);
int[] threeLineWidthPdfPTable = {14, 12, 14, 12, 14, 14};
threeLinePdfPTable.setWidths(threeLineWidthPdfPTable);
threeLinePdfPTable.getDefaultCell().setBorder(0);
PdfPCell born = new PdfPCell(new Paragraph("出生日期", keyfont));
PdfPCell bornValue = new PdfPCell(new Paragraph("1999年3月3", textfont));
PdfPCell home = new PdfPCell(new Paragraph("家庭籍贯", keyfont));
PdfPCell homeValue = new PdfPCell(new Paragraph("中国", textfont));
PdfPCell cell66 = new PdfPCell(new Paragraph("", textfont));
PdfPCell cell67 = new PdfPCell(new Paragraph("", textfont));
born.setFixedHeight(25);
homeValue.setColspan(3);
born.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
bornValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
home.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
homeValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
cell66.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
cell67.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
born.setVerticalAlignment(Element.ALIGN_MIDDLE);
bornValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
home.setVerticalAlignment(Element.ALIGN_MIDDLE);
homeValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell66.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell67.setVerticalAlignment(Element.ALIGN_MIDDLE);
threeLinePdfPTable.addCell(born);
threeLinePdfPTable.addCell(bornValue);
threeLinePdfPTable.addCell(home);
threeLinePdfPTable.addCell(homeValue);
threeLinePdfPTable.addCell(cell66);
threeLinePdfPTable.addCell(cell67);
document.add(threeLinePdfPTable);
PdfPTable schoolPdfPTable = new PdfPTable(2);
int[] schoolWidthPdfPTable = {7, 33};
schoolPdfPTable.setWidths(schoolWidthPdfPTable);
schoolPdfPTable.getDefaultCell().setBorder(0);
PdfPCell school = new PdfPCell(new Paragraph("学历信息", keyfont));
PdfPCell schoolValue = new PdfPCell(new Paragraph("xxxxxxxxxxxxxxx", textfont));
school.setFixedHeight(25);
school.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
schoolValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
school.setVerticalAlignment(Element.ALIGN_MIDDLE);
schoolValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
schoolPdfPTable.addCell(school);
schoolPdfPTable.addCell(schoolValue);
document.add(schoolPdfPTable);
PdfPTable internshipPdfPTable = new PdfPTable(2);
int[] internshipWidthPdfPTable = {7, 33};
internshipPdfPTable.setWidths(internshipWidthPdfPTable);
internshipPdfPTable.getDefaultCell().setBorder(0);
PdfPCell internship = new PdfPCell(new Paragraph("实习经历", keyfont));
PdfPCell internshipValue = new PdfPCell(new Paragraph("xxxxxxxxxxxxxxx", textfont));
internship.setFixedHeight(25);
internship.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
internshipValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
internship.setVerticalAlignment(Element.ALIGN_MIDDLE);
internshipValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
internshipPdfPTable.addCell(internship);
internshipPdfPTable.addCell(internshipValue);
document.add(internshipPdfPTable);
PdfPTable jobPdfPTable = new PdfPTable(2);
int[] jobWidthPdfPTable = {7, 33};
jobPdfPTable.setWidths(jobWidthPdfPTable);
jobPdfPTable.getDefaultCell().setBorder(0);
PdfPCell job = new PdfPCell(new Paragraph("工作经历", keyfont));
PdfPCell jobValue = new PdfPCell(new Paragraph("xxxxxxxxxxxxxxx", textfont));
job.setFixedHeight(25);
job.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
jobValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
job.setVerticalAlignment(Element.ALIGN_MIDDLE);
jobValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
jobPdfPTable.addCell(job);
jobPdfPTable.addCell(jobValue);
document.add(jobPdfPTable);
PdfPTable telnetPdfPTable = new PdfPTable(2);
int[] telnetWidthPdfPTable = {7, 33};
telnetPdfPTable.setWidths(telnetWidthPdfPTable);
telnetPdfPTable.getDefaultCell().setBorder(0);
PdfPCell personTelnet = new PdfPCell(new Paragraph("个人技能", keyfont));
PdfPCell personTelnetValue = new PdfPCell(new Paragraph("xxxxxxxxxxxxxxx", textfont));
personTelnet.setFixedHeight(25);
personTelnet.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
personTelnetValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
personTelnet.setVerticalAlignment(Element.ALIGN_MIDDLE);
personTelnetValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
telnetPdfPTable.addCell(personTelnet);
telnetPdfPTable.addCell(personTelnetValue);
document.add(telnetPdfPTable);
PdfPTable hobbyPdfPTable = new PdfPTable(2);
int[] hobbyWidthPdfPTable = {7, 33};
hobbyPdfPTable.setWidths(hobbyWidthPdfPTable);
hobbyPdfPTable.getDefaultCell().setBorder(0);
PdfPCell hobby = new PdfPCell(new Paragraph("性格爱好", keyfont));
PdfPCell hobbyValue = new PdfPCell(new Paragraph("xxxxxxxxxxxxxxx", textfont));
hobby.setFixedHeight(40);
hobby.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
hobbyValue.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
hobby.setVerticalAlignment(Element.ALIGN_MIDDLE);
hobbyValue.setVerticalAlignment(Element.ALIGN_MIDDLE);
hobbyPdfPTable.addCell(hobby);
hobbyPdfPTable.addCell(hobbyValue);
document.add(hobbyPdfPTable);
} catch (Exception e) {
e.printStackTrace();
} finally {
document.close();
pdfWriter.close();
try {
if(fileOutputStream !=null){
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
创建成功模板示例
开源地址:https://github.com/itext/itext7