方式一:(生成pdf的时候加)
分享一个 html 转 pdf 生成每页页脚
具体 自行看源码,我页是在网上找的资料
侵权即删
网盘链接地址
提取码:uz84
方式二:(直接在后台加)
直接贴源码了,亲测:
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
/**
* @author 作者 : 小布
* @version 创建时间 : 2019年5月15日 下午4:41:27
* @explain 类说明 :
*/
public class PDFBuilder extends PdfPageEventHelper {
private Phrase leftHeader;
private Phrase rigntHeader;
public static final int marginX = 90;
public static final int marginY = 70;
private static BaseFont baseFont;
// 生成下划线空白占位符
private static String Blank;
// 页眉字体
private static Font font;
// 下划线字体
private static Phrase blankPhrase;
public PDFBuilder() {
//this.leftHeader = new Phrase(header[0], PDFBuilder.font);
//this.rigntHeader = new Phrase(header[1], PDFBuilder.font);
}
static {
try {
// 中文字体依赖itext得itext-asian包
baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 168; i++) {
sb.append("\u00a0");
}
Blank = sb.toString();
font = new Font(PDFBuilder.baseFont, 16, Font.UNDEFINED);
blankPhrase = new Phrase(PDFBuilder.Blank, new Font(PDFBuilder.baseFont, Font.DEFAULTSIZE, Font.UNDERLINE));
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param writer
* @param document
*/
@Override
public void onEndPage(PdfWriter writer, Document document) {
int yMargin = -20;
float top = document.top(yMargin);
// 第一页不生成页眉页脚
//if (document.getPageNumber() == 1) {
// return;
//}
//生成下划线,使用空格占位
/*ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, PDFBuilder.blankPhrase,
document.left(-1), top, 0);
//生成左侧页眉
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, leftHeader,
document.left(), top, 0);
//生成右侧页眉
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, rigntHeader,
document.right(), top, 0);*/
//生成页脚页数
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase("注:对本报告如有疑问,请及时与病理科联系 ", PDFBuilder.font), document.right(), document.bottom(-5), 0);
}
}
然后在方法中 加入:
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
// pdf输出流
OutputStream outputStream = new FileOutputStream(outputFile);
PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
// 添加页眉页脚
pdfWriter.setPageEvent(new PDFBuilder());
document.open();
XMLWorkerHelper.getInstance().parseXHtml( pdfWriter, document, new ByteArrayInputStream( htmlStr .getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
document.close();