Reputation: 151
Can any one please tell how to develop an application PDF form contains text box, checkbox and radio button. We have requirement in that pdf having different form fields to develop using iText java API?
Upvotes: 0
Views: 1809
Reputation: 96029
Can any one please tell how to develop an application PDF form contains text box,
Cf. the samples by the original author of iText...
Keyword TEXT FIELDS e.g. TextFields.java
TextField text = new TextField(writer, rectangle,
String.format("text_%s", tf));
text.setBackgroundColor(new GrayColor(0.75f));
switch(tf) {
case 1:
text.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
text.setText("Enter your name here...");
text.setFontSize(0);
text.setAlignment(Element.ALIGN_CENTER);
text.setOptions(TextField.REQUIRED);
break;
case ...:
...
}
try {
PdfFormField field = text.getTextField();
...
writer.addAnnotation(field);
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
catch(DocumentException de) {
throw new ExceptionConverter(de);
}
checkbox and
Keyword BUTTON FIELDS > CHECKBOX, e.g. Buttons.java
RadioCheckField checkbox;
for (int i = 0; i < LANGUAGES.length; i++) {
rect = new Rectangle(180, 806 - i * 40, 200, 788 - i * 40);
checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "Yes");
field = checkbox.getCheckField();
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", onOff[1]);
writer.addAnnotation(field);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
new Phrase(LANGUAGES[i], font), 210, 790 - i * 40, 0);
}
radio button.
Keyword BUTTON FIELDS > RADIO FIELD, e.g. RadioButtons.java
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
// create a radio field spanning different pages
PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
radiogroup.setFieldName("language");
Rectangle rect = new Rectangle(40, 806, 60, 788);
RadioCheckField radio;
PdfFormField radiofield;
for (int page = 0; page < LANGUAGES.length; ) {
radio = new RadioCheckField(writer, rect, null, LANGUAGES[page]);
radio.setBackgroundColor(new GrayColor(0.8f));
radiofield = radio.getRadioField();
radiofield.setPlaceInPage(++page);
radiogroup.addKid(radiofield);
}
writer.addAnnotation(radiogroup);
Of course, please have a look at the complete source of the sample to put it into context. If possible, read the book ITEXT IN ACTION — SECOND EDITION to understand what you are doing.
Upvotes: 3