首先使用的到的jar包:itextpdf-5.5.11.jar;itext-asian-5.2.0.jar;itext-xtra-5.5.11.jar;itext-2.1.7.jar
原理介绍:
需要写一个分页事件继承PdfPageEventHelper ,重写以下方法
public void onOpenDocument(PdfWriter writer, Document document);
public void onStartPage(PdfWriter writer, Document document);
public void onEndPage(PdfWriter writer, Document document);
public void onCloseDocument(PdfWriter writer, Document document);
分页事件主要在onEndPage方法中实现。
特别注意的是:给 PdfWriter 对象添加事件时要在open方法之前,例:
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
writer.setPageEvent(new HeaderAndFooter());
document.open();
...
document.close();
以下是本人整理的工具类,直接上代码
public class PdfUtils {
// 不保存待定的更改。
public static final int wdDoNotSaveChanges = 0;
// PDF 格式
public static final int wdFormatPDF = 17;
// 基本字体和样式
public static BaseFont bfChinese;
public static Font fontChinese;
// 设置字体大小
public static Font headFont;
public static Font keyFont;
public static Font infoFont;
public static Font textFont;
public static Font spaceFont;
static {
try {
// 使用iTextAsian.jar中的字体
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 基础字体
fontChinese = new Font(bfChinese, 14, Font.NORMAL);
// 设置字体大小
headFont = new Font(bfChinese, 22, Font.BOLD);
keyFont = new Font(bfChinese, 16, Font.BOLD);
infoFont = new Font(bfChinese, 14, Font.BOLD);
textFont = new Font(bfChinese, 10, Font.NORMAL);
spaceFont = new Font(bfChinese, 3, Font.NORMAL);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 无参构造方法
*/
public PdfUtils() {
}
/**
* 添加标题
*
* @param document
* @param title
* @param font
* @throws DocumentException
*/
public static void addTitle(Document document, String title, Font font) throws DocumentException {
Paragraph t = new Paragraph(title, font);
// 居中显示
t.setAlignment(1);
document.add(t);
}
/**
* 添加小标题
*
* @param document
* @param title
* @param font
* @throws DocumentException
*/
public static void addSerTitle(Document document, String title, Font font) throws DocumentException {
document.add(new Paragraph(title, font));
}
/**
* 添加内容
*
* @param document
* @param content
* @param font
* @throws DocumentException
*/
public static void addDocument(Document document, String content, Font font) throws DocumentException {
document.add(new Paragraph(content, font));
}
/**
* 添加段落
*
* @param document
* @param content
* @param font
*/
public static void addParagraph(Document document, String content, Font font) throws DocumentException {
Paragraph paragraph = new Paragraph(content, font);
// 首行缩进
paragraph.setFirstLineIndent(24f);
// 设置段段落居中
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
// 行间距
paragraph.setLeading(20f);
// 设置段落前后间距
paragraph.setSpacingBefore(5f);
paragraph.setSpacingAfter(10f);
// 设置段落连续
paragraph.setKeepTogether(true);
document.add(paragraph);
}
private static int totalWidth = 330;
/**
* 创建PDF表格 (一)
*
* @param colNumber
* @return
*/
public static PdfPTable createTable(int colNumber) {
PdfPTable table = new PdfPTable(colNumber);
try {
table.setTotalWidth(totalWidth); // 设置表格的总宽度
table.setLockedWidth(true); // 锁定宽度
table.setHorizontalAlignment(Element.ALIGN_CENTER); // 对齐方式,左对齐
table.getDefaultCell().setBorder(1); // 默认单元格的border为1
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
/**
* 创建PDF表格 (二)
*
* @param widths
* @return
*/
public static PdfPTable createTable(float[] widths) {
PdfPTable table = new PdfPTable(widths);
try {
table.setTotalWidth(totalWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
/**
* 创建单元格,可调整对其的格式
*
* @param value
* @param font
* @param align
* @return
*/
public static PdfPCell createCell(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
return cell;
}
/**
* 创建无边框的单元格,可调整对其的格式
*
* @param value
* @param font
* @param align
* @return
*/
public static PdfPCell createNoBorderCell(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
// 设置无边框