-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathTextFooter.java
83 lines (69 loc) · 3.16 KB
/
TextFooter.java
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
package com.itextpdf.samples.sandbox.events;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.pdf.event.AbstractPdfDocumentEventHandler;
import com.itextpdf.kernel.pdf.event.AbstractPdfDocumentEvent;
import com.itextpdf.kernel.pdf.event.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import java.io.File;
import java.io.IOException;
public class TextFooter {
public static final String DEST = "./target/sandbox/events/text_footer.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TextFooter().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));
for (int i = 0; i < 3; i++) {
doc.add(new Paragraph("Test " + (i + 1)));
if (i != 2) {
doc.add(new AreaBreak());
}
}
doc.close();
}
private static class TextFooterEventHandler extends AbstractPdfDocumentEventHandler {
protected Document doc;
public TextFooterEventHandler(Document doc) {
this.doc = doc;
}
@Override
public void onAcceptedEvent(AbstractPdfDocumentEvent currentEvent) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
Rectangle pageSize = docEvent.getPage().getPageSize();
PdfFont font = null;
try {
font = PdfFontFactory.createFont(StandardFonts.HELVETICA_OBLIQUE);
} catch (IOException e) {
// Such an exception isn't expected to occur,
// because helvetica is one of standard fonts
System.err.println(e.getMessage());
}
float coordX = ((pageSize.getLeft() + doc.getLeftMargin())
+ (pageSize.getRight() - doc.getRightMargin())) / 2;
float headerY = pageSize.getTop() - doc.getTopMargin() + 10;
float footerY = doc.getBottomMargin();
Canvas canvas = new Canvas(docEvent.getPage(), pageSize);
canvas
// If the exception has been thrown, the font variable is not initialized.
// Therefore null will be set and iText will use the default font - Helvetica
.setFont(font)
.setFontSize(5)
.showTextAligned("this is a header", coordX, headerY, TextAlignment.CENTER)
.showTextAligned("this is a footer", coordX, footerY, TextAlignment.CENTER)
.close();
}
}
}