TextFieldsPDF.java Source code

Java tutorial

Introduction

Here is the source code for TextFieldsPDF.java

Source

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfBorderDictionary;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;

public class TextFieldsPDF {
    public static void main(String[] args) {
        Document document = new Document(PageSize.A4.rotate());
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TextFieldsPDF.pdf"));
            document.open();

            TextField tf = new TextField(writer, new Rectangle(100, 300, 100 + 100, 300 + 50), "asdf");
            tf.setBackgroundColor(Color.WHITE);
            tf.setBorderColor(Color.BLACK);
            tf.setBorderWidth(1);
            tf.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
            tf.setText("text...");
            tf.setAlignment(Element.ALIGN_CENTER);
            tf.setOptions(TextField.MULTILINE | TextField.REQUIRED);
            tf.setRotation(90);
            PdfFormField field = tf.getTextField();
            writer.addAnnotation(field);

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
    }
}