
import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.CMYKColor;
import com.lowagie.text.Rectangle;


class PSFtest02
{

	public static void main (String[] args) throws Exception
	{

		// Define the input and output documents

        String outputPdf = "PSFtest02.pdf";
		Document doc = new Document (PageSize.LETTER);
		PdfWriter pdfWriter = PdfWriter.getInstance (doc, new FileOutputStream (outputPdf));
		doc.open();


		// Set the Content Byte and Appearance for the checkBox

		float[] colSizes = { 20f, 556f };


		PdfContentByte cb = pdfWriter.getDirectContent();

		PdfAppearance[] buttonStates = new PdfAppearance[2];
		buttonStates[0] = cb.createAppearance(10, 10);
		buttonStates[1] = cb.createAppearance(10, 10);
		buttonStates[1].moveTo(0, 0);
		buttonStates[1].lineTo(10, 10);
		buttonStates[1].moveTo(0, 10);
		buttonStates[1].lineTo(10, 1);
		buttonStates[1].stroke();

		// Create a table line

		PdfPTable detailTable = new PdfPTable(2);
		detailTable.setTotalWidth(colSizes);
		detailTable.setLockedWidth(true);

		// Select checkbox

		PdfPTable workTable = new PdfPTable(1);
		workTable.setTotalWidth(576);
		workTable.setLockedWidth(true);

		Paragraph workPara = new Paragraph();
		PdfPCell workCell = new PdfPCell(workPara);

		workCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		workCell.setBorderWidth(1f);
		workCell.setPadding(1);
		workPara.setLeading(11f);
		workCell.setFixedHeight(30f);

        float ux = 4;
        float uy = 4;
        Rectangle rect = new Rectangle(ux, uy);

        PdfFormField checkBox = PdfFormField.createCheckBox(pdfWriter);
        checkBox.setWidget (rect, PdfAnnotation.HIGHLIGHT_INVERT);
        checkBox.setFieldName("chkBox");
        checkBox.setValueAsName("Off");
        checkBox.setAppearanceState("Off");
        checkBox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", buttonStates[0]);
        checkBox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", buttonStates[1]);

        pdfWriter.addAnnotation(checkBox);
        workCell.setCellEvent(new CheckboxEvent(checkBox));

        detailTable.addCell(workCell);

		Paragraph paraLeft = new Paragraph();
		PdfPCell cellLeft = new PdfPCell(paraLeft);
		cellLeft.setBorderWidth(0.0f);
		cellLeft.setVerticalAlignment(Table.ALIGN_MIDDLE);
		cellLeft.setHorizontalAlignment(Element.ALIGN_LEFT);
		cellLeft.setPadding(4);
		paraLeft.setLeading(7);
		paraLeft.setAlignment(Element.ALIGN_LEFT);
		paraLeft.add(new Phrase ("Click to select", new Font (Font.HELVETICA, 9, Font.BOLD, new CMYKColor(0f, 0f, 0f, 1f))));

	    detailTable.addCell(cellLeft);

        workTable.addCell(detailTable);

        doc.add(workTable);

        // Load a dash line to a cell

        workPara = new Paragraph();
        workCell = new PdfPCell(workPara);

        detailTable = new PdfPTable(1);
		detailTable.setTotalWidth(576);
		detailTable.setLockedWidth(true);

		workPara.add (new Phrase ("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -", new Font (Font.COURIER, 14, Font.BOLD, new CMYKColor(0f, 0f, 0f, 1f))));
        workCell.setColspan(1);
        workCell.setBorderWidth(1f);
        workCell.setFixedHeight(30f);
        workCell.setVerticalAlignment(Element.ALIGN_MIDDLE);

        //workCell.setCellEvent(cellEvent);
        //workCell.addElement(lineDash);

        detailTable.addCell(workCell);
        doc.add(detailTable);

        // Print a dash line

        PdfContentByte pcb = pdfWriter.getDirectContent();


        pcb.saveState();
        pcb.setLineDash(6, 0);
        pcb.moveTo(0, 600);
        pcb.lineTo(576, 600);
        pcb.stroke();
        pcb.restoreState();


		doc.close();

	}

}

class CheckboxEvent implements PdfPCellEvent
{
	private PdfFormField boxField;

	public CheckboxEvent(PdfFormField field)
	{
		boxField = field;
	}

	public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
	{
		boxField.setWidget(position, PdfAnnotation.HIGHLIGHT_INVERT);
		canvases[0].getPdfWriter().addAnnotation(boxField);
	}
}