Creating form fields

Tags: formspushbutton fieldfile selection fieldsubmit formiText 7iText 7 examples
Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/29016333/pushbuttonfield-with-double-byte-character
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.test.annotations.type.SampleTest;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.samples.GenericTest;
 
import java.io.File;
import java.io.FileOutputStream;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class CreateJapaneseButton extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/create_japanese_button.pdf";
    public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
    public static final String JAPANESE = "\u3042\u304d\u3089";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CreateJapaneseButton().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
 
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
 
        PdfButtonFormField pushButton = PdfFormField.createPushButton(
                pdfDoc,
                new Rectangle(36, 780, 108, 26),
                "japanese",
                JAPANESE,
                font,
                PdfFormField.DEFAULT_FONT_SIZE);
 
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        form.addField(pushButton);
 
        pdfDoc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/24830060/send-file-to-server-through-itext-pdf
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.test.annotations.type.SampleTest;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.samples.GenericTest;
 
import java.io.File;
import java.io.FileOutputStream;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class FileSelectionExample extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/file_selection_example.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FileSelectionExample().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
 
        PdfTextFormField field = PdfFormField.createText(pdfDoc,
                new Rectangle(36, 788, 523, 18), "myfile", "");
        field.setFileSelect(true);
        field.setAdditionalAction(PdfName.U, PdfAction.createJavaScript(
                "this.getField('myfile').browseForFileToSubmit();"
                        + "this.getField('mytitle').setFocus();"));
        form.addField(field);
 
        PdfTextFormField title = PdfFormField.createText(pdfDoc,
                new Rectangle(36, 752, 523, 18), "mytitle", "");
        form.addField(title);
 
        pdfDoc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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 com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
import com.itextpdf.test.annotations.type.SampleTest;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.samples.GenericTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class RadioGroupMultiPage1 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/radio_group_multi_page1.pdf";
    public static final String[] LANGUAGES = {"English", "German", "French", "Spanish", "Dutch"};
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RadioGroupMultiPage1().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
        Rectangle rect = new Rectangle(40, 788, 20, 18);
 
        PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "Language", "");
 
        for (int page = 1; page <= LANGUAGES.length; page++) {
            pdfDoc.addNewPage();
            PdfFormField field = PdfFormField.createRadioButton(pdfDoc, rect, radioGroup, LANGUAGES[page - 1]);
            field.setPage(page);
            doc.showTextAligned(new Paragraph(LANGUAGES[page - 1]).setFont(font).setFontSize(18),
                    70, 786, page, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
        }
 
        PdfAcroForm.getAcroForm(pdfDoc, true).addField(radioGroup);
        doc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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 com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.test.annotations.type.SampleTest;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
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 com.itextpdf.samples.GenericTest;
 
import java.io.File;
import java.io.FileOutputStream;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class RadioGroupMultiPage2 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/acroforms/radio_group_multi_page2.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RadioGroupMultiPage2().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "answer", "answer 1");
 
        Table table = new Table(2);
        Cell cell;
        for (int i = 0; i < 25; i++) {
            cell = new Cell().add(new Paragraph("Question " + i));
            table.addCell(cell);
            cell = new Cell().add(new Paragraph("Answer " + i));
            table.addCell(cell);
        }
        for (int i = 0; i < 25; i++) {
            cell = new Cell().add(new Paragraph("Radio: " + i));
            table.addCell(cell);
            cell = new Cell();
            cell.setNextRenderer(new MyCellRenderer(cell, radioGroup, "answer " + i));
            table.addCell(cell);
        }
        doc.add(table);
 
        PdfAcroForm.getAcroForm(pdfDoc, true).addField(radioGroup);
        pdfDoc.close();
    }
 
 
    private class MyCellRenderer extends CellRenderer {
        protected PdfButtonFormField radioGroup;
        protected String value;
 
        public MyCellRenderer(Cell modelElement, PdfButtonFormField radioGroup, String value) {
            super(modelElement);
            this.radioGroup = radioGroup;
            this.value = value;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            PdfDocument document = drawContext.getDocument();
            PdfFormField field = PdfFormField.createRadioButton(document, getOccupiedAreaBBox(), radioGroup, value);
            PdfAcroForm.getAcroForm(document, true).addFieldAppearanceToPage(field, document.getPage(getOccupiedArea().getPageNumber()));
        }
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/33247348/add-pdfpcell-to-paragraph
 */
package com.itextpdf.samples.sandbox.events;
 
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfTextFormField;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TextRenderer;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class GenericFields extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/events/generic_fields.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new GenericFields().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Paragraph p = new Paragraph();
        p.add("The Effective Date is ");
        Text day = new Text("     ");
        day.setNextRenderer(new FieldTextRenderer(day, "day"));
        p.add(day);
        p.add(" day of ");
        Text month = new Text("     ");
        month.setNextRenderer(new FieldTextRenderer(month, "month"));
        p.add(month);
        p.add(", ");
        Text year = new Text("            ");
        year.setNextRenderer(new FieldTextRenderer(year, "year"));
        p.add(year);
        p.add(" that this will begin.");
        doc.add(p);
        doc.close();
    }
 
 
    protected class FieldTextRenderer extends TextRenderer {
        protected String fieldName;
 
        public FieldTextRenderer(Text textElement, String fieldName) {
            super(textElement);
            this.fieldName = fieldName;
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            PdfTextFormField field = PdfTextFormField.createText(drawContext.getDocument(), getOccupiedAreaBBox(), fieldName);
            PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(field);
        }
    }
}
Resources: 
File nameRaw URLUpdated
FreeSans.ttfFreeSans.ttf2016-08-04 3:45 pm