Create PDF Forms
- 7 minutes to read
To create a form field, do the following:
- Create a form field object.
- Add the field to the document’s field collection.
- Create a widget annotation associated with the field.
- Add the widget annotation to a PDF page.
Create a Text Field
- Create a TextBoxField with the specified name.
- Add the field to the document’s field collection. Use the PdfDocument.getFields() method to access the collection.
- Bind the field to a TextBoxWidgetAnnotation.
- Add the widget annotation to a page’s annotations collection. Use the Page.getAnnotations() method to access the collection.
The following code snippet creates a text field:
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.DXPaperKind;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (PdfDocument pdfDocument = new PdfDocument()) {
// Add an A4 page to the document.
Page page = pdfDocument.getPages().add(DXPaperKind.A4);
// Create a text field.
TextBoxField loginField = new TextBoxField("Login");
// Assign an initial value to the field before it is displayed.
loginField.setValue("John Doe");
pdfDocument.getFields().add(loginField);
// Bind the field to a widget and place it on the page.
RectangleF bounds = new RectangleF(120, 720, 220, 20);
TextBoxWidgetAnnotation widget = new TextBoxWidgetAnnotation(loginField, bounds);
widget.setFontSize(12);
page.getAnnotations().add(widget);
// Save the document.
try (WritableByteChannel channel =
FileChannel.open(Path.of("result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(channel);
}
}
}
}
Create a Checkbox
- Create a CheckBoxField with the specified name.
- Add the field to the document’s field collection. Use the PdfDocument.getFields() method to access the collection.
- Bind the field to a CheckBoxWidgetAnnotation.
- Add the widget annotation to a page’s annotations collection. Use the Page.getAnnotations() method to access the collection.
The following code snippet creates a checkbox field:
import com.devexpress.drawing.*;
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.DXPaperKind;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (PdfDocument pdfDocument = new PdfDocument()) {
// Add an A4 page to the document.
Page page = pdfDocument.getPages().add(DXPaperKind.A4);
float y = 750;
float step = 30;
float textHeight = 14f;
// Create a check box field and add it to the field collection.
CheckBoxField agreeField = new CheckBoxField("AgreeToTerms");
pdfDocument.getFields().add(agreeField);
// Bind the check box field to a widget and place it on the page.
RectangleF agreeBounds = new RectangleF(50, y - textHeight + 6, 18, 18);
CheckBoxWidgetAnnotation agreeWidget = new CheckBoxWidgetAnnotation(agreeField, agreeBounds);
page.getAnnotations().add(agreeWidget);
y -= step;
// Add a label for the check box field.
TextFragment label = new TextFragment();
label.setText("Agree to terms");
label.setLocation(new PointF(
agreeBounds.getX() + 30,
agreeBounds.getY() + 6));
page.getFragments().add(label);
// Save the document.
try (WritableByteChannel channel =
FileChannel.open(Path.of("result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(channel);
}
}
}
}
Create a Combo Box
- Create a ComboBoxField with the specified name.
- Add combo box items. Use the ComboBoxField.getItems() method to access the items collection.
- Add the field to the document’s field collection. Use the PdfDocument.getFields() method to access the collection.
- Bind the field to a ComboBoxWidgetAnnotation.
- Add the widget annotation to a page’s annotations collection. Use the Page.getAnnotations() method to access the collection.
The following code snippet creates a combo box field:
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.DXPaperKind;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (PdfDocument pdfDocument = new PdfDocument()) {
// Add an A4 page to the document.
Page page = pdfDocument.getPages().add(DXPaperKind.A4);
// Create a combo box field.
ComboBoxField field =
new ComboBoxField("Country");
// Add list items.
field.getItems().add(new ChoiceFieldItem("USA"));
field.getItems().add(new ChoiceFieldItem("Canada"));
field.getItems().add(new ChoiceFieldItem("Germany"));
// Select the default value.
field.setValue("USA");
pdfDocument.getFields().add(field);
// Bind the field to a widget.
ComboBoxWidgetAnnotation widget =
new ComboBoxWidgetAnnotation(field, new RectangleF(20, 700, 120, 24));
page.getAnnotations().add(widget);
// Save the document.
try (WritableByteChannel channel =
FileChannel.open(Path.of("result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(channel);
}
}
}
}
Create a List Box
- Create a ListBoxField with the specified name.
- Add list box items. Use the ListBoxField.addItem() method to access the items collection.
- Add the field to the document’s field collection. Use the PdfDocument.getFields() method to access the collection.
- Bind the field to a ListBoxWidgetAnnotation.
- Add the widget annotation to a page’s annotations collection. Use the Page.getAnnotations() method to access the collection.
The following code snippet creates a list box field:
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.DXPaperKind;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (PdfDocument pdfDocument = new PdfDocument()) {
// Add an A4 page to the document.
Page page = pdfDocument.getPages().add(DXPaperKind.A4);
// Create a list box field.
ListBoxField field = new ListBoxField("Country");
// Add list items.
field.getItems().add(new ChoiceFieldItem("USA"));
field.getItems().add(new ChoiceFieldItem("Canada"));
field.getItems().add(new ChoiceFieldItem("Germany"));
field.getItems().add(new ChoiceFieldItem("France"));
field.getItems().add(new ChoiceFieldItem("UK"));
field.getItems().add(new ChoiceFieldItem("Italy"));
field.getItems().add(new ChoiceFieldItem("Spain"));
pdfDocument.getFields().add(field);
// Bind the field to a widget.
ListBoxWidgetAnnotation widget =
new ListBoxWidgetAnnotation(
field,
new RectangleF(20, 640, 120, 70));
page.getAnnotations().add(widget);
// Save the document.
try (WritableByteChannel channel =
FileChannel.open(Path.of("result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(channel);
}
}
}
}
Create a Radio Button Group
- Create a RadioGroupField with the specified name.
- Add the field to the document’s field collection. Use the PdfDocument.getFields() method to access the collection.
- Create a RadioGroupItemWidgetAnnotation for each radio button option and associate it with the RadioGroupField. Specify the option value in the
RadioGroupItemWidgetAnnotationconstructor. This value uniquely identifies the radio button. TheRadioGroupFielduses this value to indicate the selected option. - Add the widget annotation to a page’s annotations collection. Use the Page.getAnnotations() method to access the collection.
The following code snippet creates a radio button group with three options:
package barcodes;
import com.devexpress.drawing.*;
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.DXPaperKind;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (PdfDocument pdfDocument = new PdfDocument()) {
// Add an A4 page to the document.
Page page = pdfDocument.getPages().add(DXPaperKind.A4);
float y = 750;
float step = 30;
float textHeight = 14f;
// Create a radio button group and add it to the field collection.
RadioGroupField radioGroupField = new RadioGroupField("radioGroup");
pdfDocument.getFields().add(radioGroupField);
// Add the first radio button widget and its label.
RectangleF radioBounds1 = new RectangleF(50, y - textHeight + 6, 18, 18);
RadioGroupItemWidgetAnnotation radioButton1 =
new RadioGroupItemWidgetAnnotation(radioGroupField, "Option1", radioBounds1);
page.getAnnotations().add(radioButton1);
TextFragment label1 = new TextFragment();
label1.setText("Option 1");
label1.setLocation(new PointF(radioBounds1.getX() + 30, radioBounds1.getY() + 6));
page.getFragments().add(label1);
y -= step;
// Add the second radio button widget and its label.
RectangleF radioBounds2 = new RectangleF(50, y - textHeight + 6, 18, 18);
RadioGroupItemWidgetAnnotation radioButton2 =
new RadioGroupItemWidgetAnnotation(radioGroupField, "Option2", radioBounds2);
page.getAnnotations().add(radioButton2);
TextFragment label2 = new TextFragment();
label2.setText("Option 2");
label2.setLocation(new PointF(radioBounds2.getX() + 30, radioBounds2.getY() + 6));
page.getFragments().add(label2);
y -= step;
// Add the third radio button widget and its label.
RectangleF radioBounds3 = new RectangleF(50, y - textHeight + 6, 18, 18);
RadioGroupItemWidgetAnnotation radioButton3 =
new RadioGroupItemWidgetAnnotation(radioGroupField, "Option3", radioBounds3);
page.getAnnotations().add(radioButton3);
TextFragment label3 = new TextFragment();
label3.setText("Option 3");
label3.setLocation(new PointF(radioBounds3.getX() + 30, radioBounds3.getY() + 6));
page.getFragments().add(label3);
// Select the third radio button by default.
radioGroupField.setValue("Option3");
// Save the document.
try (WritableByteChannel channel =
FileChannel.open(Path.of("result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(channel);
}
}
}
}
Create a Signature Field
- Create a SignatureField with the specified name.
- Add the field to the document’s field collection. Use the PdfDocument.getFields() method to access the collection.
- Bind the field to a SignatureWidgetAnnotation.
- Add the widget annotation to a page’s annotations collection. Use the Page.getAnnotations() method to access the collection.
The following code snippet creates a signature field:
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.DXPaperKind;
import java.nio.channels.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (PdfDocument pdfDocument = new PdfDocument()) {
// Add an A4 page to the document.
Page page = pdfDocument.getPages().add(DXPaperKind.A4);
// Create a signature field.
SignatureField field = new SignatureField("Signature");
field.setShowDate(true);
pdfDocument.getFields().add(field);
// Bind the field to a widget.
SignatureWidgetAnnotation widget =
new SignatureWidgetAnnotation(field, new RectangleF(20, 500, 180, 60));
page.getAnnotations().add(widget);
// Save the document.
try (WritableByteChannel channel =
FileChannel.open(Path.of("result.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
pdfDocument.save(channel);
}
}
}
}
Use the following methods to define the information displayed in the signature field:
- setShowDate(boolean value)
- setShowLabels(boolean value)
- setShowLocation(boolean value)
- setShowReason(boolean value)
- setShowSignerName(boolean value)
Use the following methods to retrieve signature information after the field is signed:
Remove Form Fields
To remove a form field, remove the field object from the PDF document’s field collection. The API automatically removes all widget annotations associated with the field.
The following code snippet removes the SignatureField with the specified name from the document:
FormField field = pdfDocument.getFields().findByName("Signature");
if(field != null)
pdfDocument.getFields().remove(field);