/
Listing_08_07_TextFields.java
176 lines (147 loc) · 7.01 KB
/
Listing_08_07_TextFields.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.itextpdf.samples.book.part2.chapter08;
import com.itextpdf.forms.fields.PdfFormAnnotation;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.TextFormFieldBuilder;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.IRenderer;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class Listing_08_07_TextFields {
public static final String[] RESULT = {
"./target/book/part2/chapter08/Listing_08_07_TextFields.pdf",
"./target/book/part2/chapter08/Listing_08_07_TextFields_filled.pdf"
};
public static final String DEST = RESULT[0];
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new Listing_08_07_TextFields().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
createPdf(DEST);
fillPdf(DEST, RESULT[1]);
}
public void createPdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Cell cell;
Table table = new Table(UnitValue.createPercentArray(new float[]{1, 2}));
table.setWidth(UnitValue.createPercentValue(80));
table.addCell(new Cell().add(new Paragraph("Name:")));
cell = new Cell();
cell.setNextRenderer(new TextFilledCellRenderer(cell, 1));
table.addCell(cell);
table.addCell(new Cell().add(new Paragraph("Loginname:")));
cell = new Cell();
cell.setNextRenderer(new TextFilledCellRenderer(cell, 2));
table.addCell(cell);
table.addCell(new Cell().add(new Paragraph("Password:")));
cell = new Cell();
cell.setNextRenderer(new TextFilledCellRenderer(cell, 3));
table.addCell(cell);
table.addCell(new Cell().add(new Paragraph("Reason:")));
cell = new Cell();
cell.setNextRenderer(new TextFilledCellRenderer(cell, 4));
cell.setHeight(60);
table.addCell(cell);
doc.add(table);
doc.close();
}
public void fillPdf(String src, String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);
Map<String, PdfFormField> fields = form.getAllFormFields();
fields.get("text_1").setValue("Bruno Lowagie");
fields.get("text_2").setFieldFlags(0);
fields.get("text_2").getFirstFormAnnotation().setBorderColor(ColorConstants.RED);
fields.get("text_2").setValue("bruno");
fields.get("text_3").setFieldFlag(PdfFormField.FF_PASSWORD, false);
fields.get("text_3").getWidgets().get(0).setFlag(PdfAnnotation.PRINT);
form.getField("text_3").setValue("12345678", "********");
((PdfTextFormField) form.getField("text_4")).setMaxLen(12);
form.getField("text_4").regenerateField();
pdfDoc.close();
}
protected class TextFilledCellRenderer extends CellRenderer {
/**
* The text field index of a TextField that needs to be added to a cell.
*/
protected int tf = 0;
public TextFilledCellRenderer(Cell modelElement, int tf) {
super(modelElement);
this.tf = tf;
}
// If a renderer overflows on the next area, iText uses #getNextRenderer() method to create a new renderer for the overflow part.
// If #getNextRenderer() isn't overridden, the default method will be used and thus the default rather than the custom
// renderer will be created
@Override
public IRenderer getNextRenderer() {
return new TextFilledCellRenderer((Cell) modelElement, tf);
}
@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
PdfTextFormField text = new TextFormFieldBuilder(drawContext.getDocument(), String.format("text_%s", tf))
.setWidgetRectangle(getOccupiedAreaBBox()).createText();
text.setValue("Enter your name here...");
text.getFirstFormAnnotation().setBackgroundColor(new DeviceGray(0.75f));
PdfDictionary borderStyleDict = new PdfDictionary();
switch (tf) {
case 1:
borderStyleDict.put(PdfName.S, PdfName.B);
text.getWidgets().get(0).setBorderStyle(borderStyleDict);
text.setFontSize(10);
text.setValue("Enter your name here...");
text.setRequired(true);
text.setJustification(TextAlignment.CENTER);
break;
case 2:
borderStyleDict.put(PdfName.S, PdfName.S);
text.getWidgets().get(0).setBorderStyle(borderStyleDict);
text.setValue("Name...");
text.setMaxLen(8);
text.setComb(true);
text.getFirstFormAnnotation().setBorderWidth(2);
break;
case 3:
borderStyleDict.put(PdfName.S, PdfName.I);
text.getWidgets().get(0).setBorderStyle(borderStyleDict);
text.setPassword(true);
text.setValue("Choose a password");
text.getFirstFormAnnotation().setVisibility(PdfFormAnnotation.VISIBLE_BUT_DOES_NOT_PRINT);
break;
case 4:
borderStyleDict.put(PdfName.S, PdfName.D);
text.getWidgets().get(0).setBorderStyle(borderStyleDict);
text.getFirstFormAnnotation().setBorderColor(ColorConstants.RED);
text.setFontSize(8);
text.setValue("Enter the reason why you want to win a free " +
"accreditation for the Foobar Film Festival");
text.getFirstFormAnnotation().setBorderWidth(2);
text.setMultiline(true);
text.setRequired(true);
break;
}
PdfFormCreator.getAcroForm(drawContext.getDocument(), true).addField(text);
}
}
}