Creating form fields
These examples were written in answer to questions such as:
createjapanesebutton
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/29016333/pushbuttonfield-with-double-byte-character
*/
package sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PushbuttonField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class CreateJapaneseButton {
public static final String DEST = "results/acroforms/japanese_button.pdf";
public static final String JAPANESE = "\u3042\u304d\u3089";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static void main(String[] args) throws IOException,
DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CreateJapaneseButton().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PushbuttonField button = new PushbuttonField(writer, new Rectangle(36, 780, 144, 806), "japanese");
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
button.setFont(bf);
button.setText(JAPANESE);
writer.addAnnotation(button.getField());
document.close();
}
}
fileselectionexample
JAVA
JAVA
/**
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/24830060/send-file-to-server-through-itext-pdf
*/
package sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class FileSelectionExample {
public static final String DEST = "results/acroforms/file_selection.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FileSelectionExample().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
TextField file = new TextField(writer, new Rectangle(36, 788, 559, 806), "myfile");
file.setOptions(TextField.FILE_SELECTION);
PdfFormField upload = file.getTextField();
upload.setAdditionalActions(PdfName.U,
PdfAction.javaScript(
"this.getField('myfile').browseForFileToSubmit();"
+ "this.getField('mytitle').setFocus();",
writer));
writer.addAnnotation(upload);
TextField title = new TextField(writer, new Rectangle(36, 752, 559, 770), "mytitle");
writer.addAnnotation(title.getTextField());
document.close();
}
}
radiogroupmultipage1
JAVA
JAVA
/*
* This example was written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/30895930/issue-with-itext-radiocheckfield-when-displayed-on-multiple-pages
*/
package sandbox.acroforms;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Bruno Lowagie (iText Software)
*/
public class RadioGroupMultiPage1 {
public static final String DEST = "results/acroforms/multipage_radiobutton1.pdf";
/** Possible values of a Choice field. */
public static final String[] LANGUAGES = { "English", "German", "French", "Spanish", "Dutch" };
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new RadioGroupMultiPage1().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer =
PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte cb = writer.getDirectContent();
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);
// add the content
for (int i = 0; i < LANGUAGES.length; i++) {
cb.beginText();
cb.setFontAndSize(bf, 18);
cb.showTextAligned(Element.ALIGN_LEFT, LANGUAGES[i], 70, 790, 0);
cb.endText();
document.newPage();
}
// step 5
document.close();
}
}
genericfields
JAVA
JAVA
/*
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/33247348/add-pdfpcell-to-paragraph
*/
package sandbox.events;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
/**
* @author Bruno Lowagie (iText Software)
*/
@WrapToTest
public class GenericFields {
public static final String DEST = "results/events/generic_fields.pdf";
public class FieldChunk extends PdfPageEventHelper {
@Override
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
TextField field = new TextField(writer, rect, text);
try {
writer.addAnnotation(field.getTextField());
} catch (IOException ex) {
throw new ExceptionConverter(ex);
} catch (DocumentException ex) {
throw new ExceptionConverter(ex);
}
}
}
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new GenericFields().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
writer.setPageEvent(new FieldChunk());
document.open();
Paragraph p = new Paragraph();
p.add("The Effective Date is ");
Chunk day = new Chunk(" ");
day.setGenericTag("day");
p.add(day);
p.add(" day of ");
Chunk month = new Chunk(" ");
month.setGenericTag("month");
p.add(month);
p.add(", ");
Chunk year = new Chunk(" ");
year.setGenericTag("year");
p.add(year);
p.add(" that this will begin.");
document.add(p);
document.close();
}
}