1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
import java.io.IOException; 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.Rectangle; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfTemplate; import com.itextpdf.text.pdf.PdfWriter; public class PdfReportM1HeaderFooter extends PdfPageEventHelper {
public String header = "";
public int presentFontSize = 12;
public Rectangle pageSize = PageSize.A4; public PdfTemplate total; public BaseFont bf = null; public Font fontDetail = null;
public PdfReportM1HeaderFooter() { }
public PdfReportM1HeaderFooter(String yeMei, int presentFontSize, Rectangle pageSize) { this.header = yeMei; this.presentFontSize = presentFontSize; this.pageSize = pageSize; } public void setHeader(String header) { this.header = header; } public void setPresentFontSize(int presentFontSize) { this.presentFontSize = presentFontSize; }
public void onOpenDocument(PdfWriter writer, Document document) { total = writer.getDirectContent().createTemplate(70, 70); }
public void onEndPage(PdfWriter writer, Document document) { try { if (bf == null) { bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); } if (fontDetail == null) { fontDetail = new Font(bf, presentFontSize, Font.NORMAL); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(header, fontDetail), document.left(), document.top() + 20, 0); int pageS = writer.getPageNumber(); String foot1 = "第 " + pageS + " 页 /共"; Phrase footer = new Phrase(foot1, fontDetail); float len = bf.getWidthPoint(foot1, presentFontSize); PdfContentByte cb = writer.getDirectContent();
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer, (document.rightMargin() + document.right() + document.leftMargin() - document.left() - len) / 2.0F + 20F, document.bottom() - 20, 0); cb.addTemplate(total, (document.rightMargin() + document.right() + document.leftMargin() - document.left()) / 2.0F + 20F, document.bottom() - 20); }
public void onCloseDocument(PdfWriter writer, Document document) { total.beginText(); total.setFontAndSize(bf, presentFontSize); String foot2 = " " + (writer.getPageNumber()) + " 页"; total.showText(foot2); total.endText(); total.closePath(); } }
|