最近有个需求,富文本框输入内容生成模板,然后填充内容生成pdf,网上的也看了真是坑爹,大都是连用都不能用的小demo。花了两天时间研究了一下itext5的api,踩了不少坑,为了后来人能少走点弯路,特献上源码,废话不多说,上图!
成品图啦~~
当然这是源码啦~~
/**
* @author kkw
* @date 2019/9/28
*/
public class PDFBuilder extends PdfPageEventHelper {
/**
* 页眉
*/
public String header = "";
/**
* 文档字体大小,页脚页眉最好和文本大小一致
*/
public int presentFontSize = 12;
/**
* 文档页面大小,最好前面传入,否则默认为A4纸张
*/
public Rectangle pageSize = PageSize.A4;
// 模板
public PdfTemplate total;
public BaseFont bf = null;
/**
* 利用基础字体生成的字体对象,一般用于生成中文文字
*/
public Font fontDetail = null;
//logo
public static final String IMAGEPATH = "我是图片.png";
///-------------------------------我是分界线---------------------------------------//
/**
* leftHeader
*/
private Phrase leftHeader;
private Phrase rigntHeader;
static final int marginX = 90;
static final int marginY = 140;
public static PdfTemplate tpl;
private static BaseFont baseFont;
/**
* 生成下划线空白占位符
*/
private static String Blank;
/**
* 页眉字体
*/
public static Font font;
/**
* 下划线字体
*/
private static Phrase blankPhrase;
public PDFBuilder(String[] header) {
this.leftHeader = new Phrase(header[0], PDFBuilder.font);
this.rigntHeader = new Phrase(header[1], PDFBuilder.font);
}
static {
try {