/**
 * Written by Bruno Lowagie as an extra example for the book
 * 'iText in Action'.
 */

package test;

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.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPTableEvent;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;
import com.lowagie.text.pdf.events.FieldPositioningEvents;

public class EIDForm implements PdfPTableEvent {

	public static final String ADDRESS = "address";
	public static final String CARD_NUMBER = "cardNumber";
	public static final String CHIP_NUMBER = "chipNumber";
	public static final String CARD_VALIDITY_BEGIN = "cardValidityBegin";
	public static final String CARD_VALIDITY_END = "cardValidityEnd";
	public static final String CARD_DELIVERY_MUNICIPALITY = "cardDeliveryMunicipality";
	public static final String NATIONAL_NUMBER = "nationalNumber";
	public static final String NAME = "name";
	public static final String TWO_FIRST_FIRST_NAMES = "twoFirstFirstNames";
	public static final String FIRST_LETTER_THIRD_FIRST_NAME = "firstLetterThirdFirstName";
	public static final String NATIONALITY = "nationality";
	public static final String BIRTH_LOCATION = "birthLocation";
	public static final String BIRTH_DATE = "birthDate";
	public static final String SEX ="sex";
	public static final String MUNICIPALITY = "municipality";
	public static final String NOBLE_CONDITION = "nobleCondition";
	public static final String DOCUMENT_TYPE = "documentType";
	public static final String SPECIAL_STATUS = "specialStatus";
	public static final String ZIP = "zip";
	
	public static final String CERT_AUTHENTICATION = "certAuthentication";
	public static final String CERT_NON_REPUDIATION = "certNonRepudiation";
	public static final String CERT_CITIZEN_CA = "certCitizenCA";
	public static final String CERT_ROOT_CA = "certRootCa";
	public static final String CERT_RNN_CA = "certRrnCa";
	
	public static void main(String[] args) {
		Document document = new Document();
		try {
			PdfWriter writer = PdfWriter.getInstance(
				document, new FileOutputStream("EIDForm.pdf"));
			FieldPositioningEvents fpe = new FieldPositioningEvents();
			writer.setPageEvent(fpe);
			document.open();
			Font font = FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 14);
			
			// title
			Paragraph p;
			p = new Paragraph("IDENTITY CARD", font);
			p.setAlignment(Element.ALIGN_CENTER);
			document.add(p);
			
			// data about the owner 
			PdfPTable table;
			int[] widths = { 40, 30, 30, 30 };
			table = new PdfPTable(4);
			table.setWidthPercentage(80);
			table.setSpacingBefore(10f);
			table.setSpacingAfter(10f);
			table.setTableEvent(new EIDForm());
			table.setWidths(widths);
			
			PdfPCell cell;
			// line 1
			cell = new PdfPCell(new Paragraph("NAME OF OWNER:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, NAME));
			cell.setColspan(3);
			table.addCell(cell);

			// line 2
			cell = new PdfPCell(new Paragraph("FIRST NAME(S):"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, TWO_FIRST_FIRST_NAMES));
			cell.setColspan(3);
			table.addCell(cell);

			// line 3
			cell = new PdfPCell(new Paragraph("DATE OF BIRTH:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, BIRTH_DATE));
			table.addCell(cell);
			cell = new PdfPCell(new Paragraph("SEX:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, SEX));
			table.addCell(cell);

			// line 4
			cell = new PdfPCell(new Paragraph("PLACE OF BIRTH:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, BIRTH_LOCATION));
			cell.setColspan(3);
			table.addCell(cell);

			// line 5
			cell = new PdfPCell(new Paragraph("NATIONAL NUMBER:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, NATIONAL_NUMBER));
			table.addCell(cell);
			cell = new PdfPCell(new Paragraph("NATIONALITY:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, NATIONALITY));
			table.addCell(cell);

			// line 6
			cell = new PdfPCell(new Paragraph("ADDRESS:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, ADDRESS));
			cell.setColspan(3);
			table.addCell(cell);

			// line 5
			cell = new PdfPCell(new Paragraph("MUNICIPALITY:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, MUNICIPALITY));
			table.addCell(cell);
			cell = new PdfPCell(new Paragraph("ZIP CODE:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, ZIP));
			table.addCell(cell);
			
			document.add(table);
			
			// data about the card
			table = new PdfPTable(4);
			table.setWidthPercentage(80);
			table.setSpacingBefore(10f);
			table.setSpacingAfter(10f);
			table.setTableEvent(new EIDForm());
			table.setWidths(widths);

			// line 1
			cell = new PdfPCell(new Paragraph("CARDNUMBER:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, CARD_NUMBER));
			table.addCell(cell);
			cell = new PdfPCell(new Paragraph("DOC. TYPE:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, DOCUMENT_TYPE));
			table.addCell(cell);

			// line 2
			cell = new PdfPCell(new Paragraph("CHIPNUMBER:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, CHIP_NUMBER));
			cell.setColspan(3);
			table.addCell(cell);

			// line 3
			cell = new PdfPCell(new Paragraph("VALID FROM:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, CARD_VALIDITY_BEGIN));
			table.addCell(cell);
			cell = new PdfPCell(new Paragraph("TILL:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setCellEvent(new FieldPositioningEvents(writer, CARD_VALIDITY_END));
			table.addCell(cell);
			
			document.add(table);

			// certificats on the eID
			p = new Paragraph("CERTIFICATS", font);
			p.setAlignment(Element.ALIGN_CENTER);
			document.add(p);
			
			// authentication certificate
			table = new PdfPTable(1);
			table.setWidthPercentage(80);
			table.setSpacingBefore(10f);
			table.setSpacingAfter(10f);
			table.setTableEvent(new EIDForm());
			cell = new PdfPCell(new Paragraph("Authentication Certificate:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setMinimumHeight(180);
			TextField tf = new TextField(writer, new Rectangle(0, 0), CERT_AUTHENTICATION);
			tf.setOptions(TextField.MULTILINE);
			cell.setCellEvent(new FieldPositioningEvents(writer, tf.getTextField()));
			table.addCell(cell);
			document.add(table);

			// non-repudiation certificate
			table = new PdfPTable(1);
			table.setWidthPercentage(80);
			table.setSpacingBefore(10f);
			table.setSpacingAfter(10f);
			table.setTableEvent(new EIDForm());
			cell = new PdfPCell(new Paragraph("Non-Repudiation Certificate:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setMinimumHeight(180);
			tf = new TextField(writer, new Rectangle(0, 0), CERT_NON_REPUDIATION);
			tf.setOptions(TextField.MULTILINE);
			cell.setCellEvent(new FieldPositioningEvents(writer, tf.getTextField()));
			table.addCell(cell);
			document.add(table);
			
			document.newPage();

			// Citizen's CA Certificate
			table = new PdfPTable(1);
			table.setWidthPercentage(80);
			table.setSpacingBefore(10f);
			table.setSpacingAfter(10f);
			table.setTableEvent(new EIDForm());
			cell = new PdfPCell(new Paragraph("Citizen's CA Certificate:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setMinimumHeight(180);
			tf = new TextField(writer, new Rectangle(0, 0), CERT_CITIZEN_CA);
			tf.setOptions(TextField.MULTILINE);
			cell.setCellEvent(new FieldPositioningEvents(writer, tf.getTextField()));
			table.addCell(cell);
			document.add(table);

			// root CA certificate
			table = new PdfPTable(1);
			table.setWidthPercentage(80);
			table.setSpacingBefore(10f);
			table.setSpacingAfter(10f);
			table.setTableEvent(new EIDForm());
			cell = new PdfPCell(new Paragraph("Root CA Certificate:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setMinimumHeight(180);
			tf = new TextField(writer, new Rectangle(0, 0), CERT_ROOT_CA);
			tf.setOptions(TextField.MULTILINE);
			cell.setCellEvent(new FieldPositioningEvents(writer, tf.getTextField()));
			table.addCell(cell);
			document.add(table);

			// rrn CA certificate
			table = new PdfPTable(1);
			table.setWidthPercentage(80);
			table.setSpacingBefore(10f);
			table.setSpacingAfter(10f);
			table.setTableEvent(new EIDForm());
			cell = new PdfPCell(new Paragraph("RRN CA Certificate:"));
			cell.setBorder(PdfPCell.NO_BORDER);
			table.addCell(cell);
			cell = new PdfPCell();
			cell.setBorder(PdfPCell.NO_BORDER);
			cell.setMinimumHeight(180);
			tf = new TextField(writer, new Rectangle(0, 0), CERT_RNN_CA);
			tf.setOptions(TextField.MULTILINE);
			cell.setCellEvent(new FieldPositioningEvents(writer, tf.getTextField()));
			table.addCell(cell);
			document.add(table);
			
		} catch (DocumentException de) {
			System.err.println(de.getMessage());
		} catch (IOException ioe) {
			System.err.println(ioe.getMessage());
		}
		document.close();
	}

	public void tableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
		float widths[] = width[0];
        PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
        cb.rectangle(widths[0] - 5, heights[heights.length - 1] - 5, widths[widths.length - 1] - widths[0] + 10, heights[0] - heights[heights.length - 1] + 10);
        cb.stroke();
	}		
}
