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
| import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.draw.LineSeparator; import lombok.SneakyThrows;
public class MyHeaderFooter extends PdfPageEventHelper { MyHeaderFooter(){ } public float marginLeft; MyHeaderFooter(float marginLeft){ this.marginLeft = marginLeft; }
public PdfTemplate total;
public BaseFont bf = null;
@Override public void onOpenDocument(PdfWriter writer, Document document) { total = writer.getDirectContent().createTemplate(50, 50); }
@SneakyThrows @Override public void onEndPage(PdfWriter writer, Document document) { this.addPage(writer, document); this.addWatermark(writer); }
public void addPage(PdfWriter writer, Document document) throws Exception { if (bf == null) { bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); }
Font columnTextfont = new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.RED); int pageS = writer.getPageNumber(); if (pageS != 0) { ClassPathResource classPathResource = new ClassPathResource("img/logo.png"); URL url = classPathResource.getURL(); Image image = Image.getInstance(url); Phrase p1 = new Phrase("", columnTextfont); p1.add(new Chunk(image, 0, -10)); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, p1, document.left(), document.top() + 15, 0);
Paragraph p2 = new Paragraph(); LineSeparator lineSeparator = new LineSeparator(); lineSeparator.setLineColor(BaseColor.GRAY); lineSeparator.setLineWidth(1.0f); p2.add(new Chunk(lineSeparator)); ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, p2, document.left() - marginLeft, document.top() + 3, 0);
String foot1 = "第 " + (pageS) + " 页 /共"; Font fontDetail = new Font(bf, Font.STRIKETHRU, Font.NORMAL, BaseColor.BLACK); Phrase footer = new Phrase(foot1, fontDetail); float len = bf.getWidthPoint(foot1, Font.STRIKETHRU);
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 addWatermark(PdfWriter writer) throws Exception { PdfContentByte content = writer.getDirectContent(); content.beginText(); BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); content.setFontAndSize(font, 20); PdfGState gs = new PdfGState(); gs.setFillOpacity(0.1f); content.setGState(gs); for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { content.showTextAligned(Element.ALIGN_CENTER, "这是水印 这是水印", i * 200, j * 200, 45); } } content.setColorFill(BaseColor.GRAY); content.endText(); }
@Override public void onCloseDocument(PdfWriter writer, Document document) { total.beginText(); total.setFontAndSize(bf, Font.STRIKETHRU); String foot2 = " " + (writer.getPageNumber()) + " 页"; total.showText(foot2); total.endText(); total.closePath(); } }
|