java利用itext的API操作pdf
网上的资料比较多,自己整理了一下
添加依赖
<!-- itextpdf 依赖包start -->
<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>
预览
package com.mmt.common.util;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
public class PdfUtils {
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("/project/generatePdf.pdf");
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
document.setPageSize(PageSize.A4);
Rectangle rectangle = new Rectangle(45, 45, 559, 788);
rectangle.setBorderColor(BaseColor.BLUE);
writer.setBoxSize("rectangle", rectangle);
TableFooter footer = new TableFooter();
footer.setHeader("https://isu-jing.github.io/");
writer.setPageEvent(footer);
document.open();
//标题
document.add(getFirstTile("我是大标题"));
Paragraph elements = new Paragraph("下面这是一个图片", getPdfChineseFont());
document.add(elements);
//加入图片
Image imageLogo = Image.getInstance("/project/timg.jpeg");
imageLogo.scaleToFit(170,170);
document.add(imageLogo);
addCjFirstTitle(document,"我是层级标题");
document.add(new Phrase("\n"));
PdfPTable table = new PdfPTable(new float[]{250,250});
table.setTotalWidth(310f);
table.setLockedWidth(true);
table.setSpacingBefore(10f);
//绿色背景
PdfPCell cellByBackgroudColor = getCellByBackgroudColor(new BaseColor(113, 204, 94));
cellByBackgroudColor.setPhrase(getColorFont(Font.BOLD,"绿色背景",new BaseColor(32,42,47),11));
table.addCell(cellByBackgroudColor);
//黑色背景
PdfPCell cellByBackgroudColor1 = getCellByBackgroudColor(new BaseColor(39, 40, 42));
cellByBackgroudColor1.setPhrase(getColorFont(Font.NORMAL,"黑色背景",new BaseColor(235,235,235),13));
table.addCell(cellByBackgroudColor1);
//红色字体
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setPhrase(getColorFont(Font.NORMAL,"红色字体",new BaseColor(219,81,73),13));
pdfPCell.setMinimumHeight(30f);
table.addCell(pdfPCell);
//蓝色字体
PdfPCell pdfPCell1 = new PdfPCell();
pdfPCell1.setPhrase(getColorFont(Font.ITALIC,"蓝色字体",new BaseColor(33,129,239),15));
pdfPCell1.setMinimumHeight(30f);
table.addCell(pdfPCell1);
//水平垂直居中(这里设置了垂直居中不知道为什么不生效,所以设置了一个顶边距。。。)
PdfPCell pdfPCell2 = new PdfPCell();
pdfPCell2.setPhrase(getColorFont(Font.ITALIC,"垂直水平居中",new BaseColor(33,129,239),15));
pdfPCell2.setMinimumHeight(50f);
pdfPCell2.setPaddingTop(17f);
pdfPCell2.setVerticalAlignment(Element.ALIGN_CENTER);
pdfPCell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(pdfPCell2);
//水平中
PdfPCell pdfPCell3 = new PdfPCell();
pdfPCell3.setPhrase(getColorFont(Font.ITALIC,"水平居中",new BaseColor(32,42,47),10));
pdfPCell3.setMinimumHeight(50f);
pdfPCell3.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(pdfPCell3);
document.add(table);
document.add(new Phrase("\n"));
//彩虹刻度尺效果
addRainbow(document);
document.add(new Phrase("\n"));
PdfPTable table1 = new PdfPTable(new float[]{250,250});
table1.setTotalWidth(410f);
table1.setLockedWidth(true);
table1.setSpacingBefore(10f);
//上边框隐藏
PdfPCell cell = getCellByBackgroudColor(new BaseColor(255, 255, 255));
cell.setPhrase(getColorFont(Font.BOLD,"上边框隐藏",new BaseColor(32,42,47),11));
cell.disableBorderSide(1);
cell.setMinimumHeight(30);
//下边框隐层
PdfPCell cell1 = getCellByBackgroudColor(new BaseColor(255, 255, 255));
cell1.setPhrase(getColorFont(Font.BOLD,"下边框隐层",new BaseColor(32,42,47),11));
cell1.disableBorderSide(2);
cell1.setMinimumHeight(30);
//下边框隐层
PdfPCell cell2 = getCellByBackgroudColor(new BaseColor(255, 255, 255));
cell2.setPhrase(getColorFont(Font.BOLD,"都隐层",new BaseColor(32,42,47),11));
cell2.disableBorderSide(15);
cell2.setMinimumHeight(30);
table1.addCell(cell);
table1.addCell(cell);
table1.addCell(cell1);
table1.addCell(cell1);
table1.addCell(cell2);
table1.addCell(cell2);
document.add(table1);
document.close();
}
/**
* @Description: 获取带颜色和大小的字体,
* @Param: [font, content, baseColor, fontSize]
* @Return: com.itextpdf.text.Phrase
* @Author: Jiangsy
* @Date: 2019/9/18
**/
private static Phrase getColorFont(int fontStyle,String content,BaseColor baseColor,float fontSize) throws Exception{
FontSelector selector = new FontSelector();
BaseFont bfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese,fontSize,fontStyle);
font.setColor(baseColor);
selector.addFont(font);
Phrase ph = selector.process(content);
Paragraph p = new Paragraph(ph);
return p;
}
/**
* @Description: 获取带背景色的单元格
* @Param: [baseColor]
* @Return: com.itextpdf.text.pdf.PdfPCell
* @Author: Jiangsy
* @Date: 2019/9/20
**/
private static PdfPCell getCellByBackgroudColor(BaseColor baseColor){
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setBackgroundColor(baseColor);
return pdfPCell;
}
/**
* @MethodName: 一级大标题
* @Param: [title]
* @Return: com.itextpdf.text.Paragraph
* @Author: Jiangsy
* @Date: 2019/8/30
**/
private static Paragraph getFirstTile(String title) throws Exception{
//添加中文字体
BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font firsetTitleFont = new Font(bfChinese,22,Font.NORMAL); //一级标题
Paragraph p1 = new Paragraph(title,firsetTitleFont);
p1.setAlignment(Element.ALIGN_CENTER);
return p1;
}
/**
* @MethodName: 层级列表主标题
* @Param: [document]
* @Return: void
* @Author: Jiangsy
* @Date: 2019/8/30
**/
private static void addCjFirstTitle(Document document,String title) throws Exception{
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(510f);
table.setLockedWidth(true);
PdfPCell cell = new PdfPCell();
cell.setBackgroundColor(new BaseColor(251,114,66));
cell.setMinimumHeight(25); // 设置单元格高度
cell.addElement(getBlueBg(title));
cell.disableBorderSide(15);
cell.setPaddingBottom(10f);
cell.setPaddingLeft(5f);
table.addCell(cell);
table.setHorizontalAlignment(1);
document.add(table);
}
/**
* @MethodName: 白色字体
* @Param: [warn]
* @Return: com.itextpdf.text.Paragraph
* @Author: Jiangsy
* @Date: 2019/8/30
**/
private static Paragraph getBlueBg( String warn) throws Exception {
FontSelector selector = new FontSelector();
BaseFont bfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fff = new Font(bfChinese,14,Font.NORMAL);
selector.addFont(fff);
fff.setColor(BaseColor.WHITE);
Phrase ph = selector.process(warn);
Paragraph p = new Paragraph(ph);
return p;
}
/**
* @MethodName: 获取中文字体
* @Param: []
* @Return: com.itextpdf.text.Font
* @Author: Jiangsy
* @Date: 2019/7/25
**/
public static Font getPdfChineseFont() throws Exception {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 10, Font.NORMAL);
return fontChinese;
}
private static void addRainbow(Document document) throws Exception{
PdfPTable table0 = new PdfPTable(new float[]{80,430});
table0.setTotalWidth(510f);
table0.setLockedWidth(true);
table0.setSpacingBefore(10f);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(510f);
table.setLockedWidth(true);
PdfPTable table2 = new PdfPTable(new float[]{30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30});
table2.addCell(getPdfBgColorCell(new BaseColor(103,204,104)));
table2.addCell(getPdfBgColorCell(new BaseColor(113,204,94)));
table2.addCell(getPdfBgColorCell(new BaseColor(129,204,84)));
table2.addCell(getPdfBgColorCell(new BaseColor(146,204,74)));
table2.addCell(getPdfBgColorCell(new BaseColor(172,204,64)));
table2.addCell(getPdfBgColorCell(new BaseColor(188,204,45)));
table2.addCell(getPdfBgColorCell(new BaseColor(206,204,32)));
table2.addCell(getPdfBgColorCell(new BaseColor(229,203,17)));
table2.addCell(getPdfBgColorCell(new BaseColor(243,205,12)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,220,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,210,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,190,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,170,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,150,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,130,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,110,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,90,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,70,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,60,0)));
table2.addCell(getPdfBgColorCell(new BaseColor(255,40,0)));
table.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell pdfCell1 = new PdfPCell();
pdfCell1.setPhrase(getColorFont(Font.BOLD,"等级测试",new BaseColor(12,91,175),11f));
pdfCell1.setPaddingLeft(10f);
pdfCell1.setMinimumHeight(25f);
pdfCell1.setPaddingTop(6f);
pdfCell1.disableBorderSide(8);
PdfPCell pdfCell2 = new PdfPCell();
pdfCell2.setPhrase(getColorFont(Font.BOLD,"J ava 菜鸡",new BaseColor(218,10,37),11f));
pdfCell2.setPaddingLeft(10f);
pdfCell2.setMinimumHeight(25f);
pdfCell2.setPaddingTop(6f);
pdfCell2.disableBorderSide(4);
table0.addCell(pdfCell1);
table0.addCell(pdfCell2);
PdfPCell pdfPCell2 = new PdfPCell();
pdfPCell2.setPhrase(getColorFont(Font.NORMAL,"水平",new BaseColor(12,91,175),10f));
pdfPCell2.disableBorderSide(2);
pdfPCell2.setPaddingLeft(10f);
PdfPCell pdfPCell3 = new PdfPCell();
int a = 5;
if(a == 1){
pdfPCell3.setPhrase(getColorFont(Font.NORMAL,"▼ J AVA菜鸡",new BaseColor(12,91,175),10f));
pdfPCell3.disableBorderSide(3);
pdfPCell3.setPaddingLeft(50);
}else if(a == 2){
pdfPCell3.setPhrase(getColorFont(Font.NORMAL,"▼ J AVA入门",new BaseColor(12,91,175),10f));
pdfPCell3.disableBorderSide(3);
pdfPCell3.setPaddingLeft(70);
}else if(a == 3){
pdfPCell3.setPhrase(getColorFont(Font.NORMAL,"▼ J AVA中级",new BaseColor(12,91,175),10f));
pdfPCell3.disableBorderSide(3);
pdfPCell3.setPaddingLeft(80);
}else if(a == 4){
pdfPCell3.setPhrase(getColorFont(Font.NORMAL,"▼ J AVA高级",new BaseColor(12,91,175),10f));
pdfPCell3.disableBorderSide(3);
pdfPCell3.setPaddingLeft(110);
}else if(a == 5){
pdfPCell3.setPhrase(getColorFont(Font.NORMAL,"▼ 技术专家",new BaseColor(12,91,175),10f));
pdfPCell3.disableBorderSide(3);
pdfPCell3.setPaddingLeft(220);
}else if(a == 6){
pdfPCell3.setPhrase(getColorFont(Font.NORMAL,"▼ 架构师",new BaseColor(12,91,175),10f));
pdfPCell3.disableBorderSide(3);
pdfPCell3.setPaddingLeft(410);
}
PdfPCell pdfPCell4 = new PdfPCell();
pdfPCell4.addElement(table2);
pdfPCell4.disableBorderSide(3);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(pdfPCell2);
table.addCell(pdfPCell3);
table.addCell(pdfPCell4);
PdfPCell pdfPCell5 = new PdfPCell(new Paragraph("初级"+" "+"架构"));
pdfPCell5.disableBorderSide(1);
pdfPCell5.setPaddingLeft(55);
table.addCell(pdfPCell5);
document.add(table0);
document.add(table);
}
/**
* @Description: 获取带颜色的单元格
* @Param: [baseColor]
* @Return: com.itextpdf.text.pdf.PdfPCell
* @Author: Jiangsy
* @Date: 2019/9/18
**/
private static PdfPCell getPdfBgColorCell(BaseColor baseColor){
PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setBackgroundColor(baseColor);
pdfPCell.disableBorderSide(15);
pdfPCell.setMinimumHeight(20);
return pdfPCell;
}
}
页脚代码
package com.mmt.common.util;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
/**
* @MethodName: pdf页码
* @Param:
* @Return:
* @Author: Jiangsy
* @Date: 2019/9/3
**/
public class TableFooter extends PdfPageEventHelper{
String header;
PdfTemplate total;
public void setHeader(String header) {
this.header = header;
}
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(30, 16);
}
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table = new PdfPTable(3);
try {
table.setWidths(new int[] { 24, 24, 2 });
table.setTotalWidth(505);
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.TOP);
table.getDefaultCell().setBorderWidth(2);
table.getDefaultCell().setBorderColor(BaseColor.BLACK);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(header);
table.addCell(String.format("%d/", writer.getPageNumber()));
PdfPCell cell = new PdfPCell(Image.getInstance(total));
cell.setBorder(Rectangle.TOP);
cell.setBorderWidth(2);
cell.setBorderColor(BaseColor.BLACK);
table.addCell(cell);
table.writeSelectedRows(0, -1, 45, 45, writer.getDirectContent());
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(total, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber())), 0, 2, 0);
}
}