Itext笔记之pdf页眉页脚
程序员文章站
2022-05-31 20:50:13
...
效果展示:
源码:
public class App
{
public static void main( String[] args ) throws Exception {
Document document = new Document(PageSize.A4,50,50,50,50);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("/home/hzq/a.pdf"));
BaseFont baseFont = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font font = new Font(baseFont,12);
Rectangle rect = new Rectangle(36, 54, 559, 788);
rect.setBorderColor(BaseColor.BLACK);
writer.setBoxSize("art", rect);
HeaderFoot headerFoot = new HeaderFoot();
writer.setPageEvent(headerFoot);
document.open();
Paragraph paragraph = new Paragraph("This is First Page 第一页",font);
document.add(paragraph);
document.newPage();
document.add(new Paragraph("This is Second Page 第二页",font));
document.newPage();
document.add(new Paragraph("This is Second Page 第三页",font));
document.close();
}
}
HeaderFoot类:
public class HeaderFoot extends PdfPageEventHelper {
private String header;
private PdfTemplate template;
public void setHeader(String header){
this.header = header;
}
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
template = writer.getDirectContent().createTemplate(30,16);
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
BaseFont baseFont = null;
try {
baseFont = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Font font = new Font(baseFont,12);
Rectangle rect = writer.getBoxSize("art");
//添加页眉
switch(writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, new Phrase("偶数页页眉",font),
rect.getLeft(230), rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, new Phrase("奇数页页眉",font),
rect.getLeft(230), rect.getTop(), 0);
break;
}
//添加页码
try {
PdfPTable table = new PdfPTable(3);
//设置表格的宽度有两种方法,分别如下
table.setTotalWidth(new float[]{80,12,40});
//将宽度锁定
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(12);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
PdfPCell cell = new PdfPCell( new Paragraph("第 "+writer.getPageNumber()+" 页/共 ",font));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
PdfPCell cell1 = new PdfPCell(Image.getInstance(template));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("页",font));
cell2.setBorder(Rectangle.NO_BORDER);
table.addCell(cell2);
table.writeSelectedRows(0,-1,235,50,writer.getDirectContent());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(template,Element.PARAGRAPH,new Phrase(String.valueOf(writer.getPageNumber()-1)),2,2,0);
}
}
jar包:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.4.5</version>
</dependency>
项目下载路径:http://download.csdn.net/detail/mmd0308/9886919