Generate a PDF form in java - Apache PDFBox example

Generate a PDF form in java - Apache PDFBox example

In this example we will Apache PDFBox to create PDF form.
The form will contain the following elements
  • Labels
  • Input Text Fields
  • Check Boxes
  • Radio Buttons
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox;
import org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;


public class GeneratePDFForm {

	public static void main(String[] args) throws Exception{

		// Create a new document with an empty page.
		PDDocument document = new PDDocument();
		PDPage page = new PDPage(PDRectangle.LETTER);
		document.addPage(page);

		// Adobe Acrobat uses Helvetica as a default font and 
		// stores that under the name '/Helv' in the resources dictionary
		PDFont font = PDType1Font.HELVETICA;
		PDResources resources = new PDResources();
		resources.put(COSName.getPDFName("Helv"), font);

		// Add a new AcroForm and add that to the document
		PDAcroForm acroForm = new PDAcroForm(document);
		document.getDocumentCatalog().setAcroForm(acroForm);

		// Add and set the resources and default appearance at the form level
		acroForm.setDefaultResources(resources);

		// Acrobat sets the font size on the form level to be
		// auto sized as default. This is done by setting the font size to '0'
		String defaultAppearanceString = "/Helv 12 Tf 0 g";
		acroForm.setDefaultAppearance(defaultAppearanceString);

		int x = 25;
		int y = 750;

		addText(document, page, "Registration Form", x , y, true);
		y = y - 30;
		addText(document, page, "Name", x , y, false);
		addField(acroForm, page, "name", "Your Name", x + 75, y);
		y = y - 20;
		addText(document, page, "Address", x , y, false);
		addField(acroForm, page, "address", "", x + 75, y);
		y = y - 20;
		addText(document, page, "Phone No", x , y, false);
		addField(acroForm, page, "phone", " ", x + 75, y);

		y = y - 30;
		addText(document, page, "Hobbies", x , y, true);
		y = y - 20;
		addCheckBox(document, acroForm, page, "sports", false, x, y);
		addText(document, page, "Sports", x + 25 , y, false);
		y = y - 20;
		addCheckBox(document, acroForm, page, "cooking", false, x, y);
		addText(document, page, "Cooking", x + 25 , y, false);
		y = y - 20;
		addCheckBox(document, acroForm, page, "music", true, x, y);
		addText(document, page, "Music", x + 25 , y, false);
		y = y - 20;
		addCheckBox(document, acroForm, page, "travel", false, x, y);
		addText(document, page, "Travelling", x + 25 , y, false);

		y = y - 30;
		addText(document, page, "Gender", x , y, true);

		ArrayList

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.