Package com.lowagie.examples.forms

Source Code of com.lowagie.examples.forms.SimpleRegistrationForm

/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package com.lowagie.examples.forms;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.DocumentException;
import com.lowagie.text.LwgRectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.LwgPdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.LwgPdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;

/**
* General example using TableEvents and CellEvents.
*/
public class SimpleRegistrationForm implements PdfPCellEvent {
 
  /** the writer with the acroform */
  private PdfWriter writer;

  /** the current fieldname */
  private String fieldname = "NoName";

  /**
   * Construct an implementation of PdfPCellEvent.
   *
   * @param writer
   *            the writer with the Acroform that will have to hold the
   *            fields.
   */
  public SimpleRegistrationForm(PdfWriter writer) {
    this.writer = writer;
  }

  /**
   * Construct an implementation of PdfPCellEvent.
   *
   * @param writer
   *            the writer with the Acroform that will have to hold the
   *            fields.
   * @param fieldname
   *            the name of the TextField
   * 
   */
  public SimpleRegistrationForm(PdfWriter writer, String fieldname) {
    this.writer = writer;
    this.fieldname = fieldname;
  }

  /**
   * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
   *      com.lowagie.text.LwgRectangle, com.lowagie.text.pdf.PdfContentByte[])
   */
  public void cellLayout(LwgPdfPCell cell, LwgRectangle position,
      PdfContentByte[] canvases) {
    TextField tf = new TextField(writer, position, fieldname);
    tf.setFontSize(12);
    try {
      PdfFormField field = tf.getTextField();
      writer.addAnnotation(field);
    } catch (IOException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }

  /**
   * Example originally written by Wendy Smoak to generate a Table with
   * 'floating boxes'. Adapted by Bruno Lowagie.
   *
   * @param args
   */
  public static void main(String[] args) {
    // step 1
    LwgDocument document = new LwgDocument();
    try {
      // step 2

      PdfWriter writer = PdfWriter.getInstance(document,
          new FileOutputStream("SimpleRegistrationForm.pdf"));
      // step 3
      document.open();
      // step 4
      LwgPdfPTable table = new LwgPdfPTable(2);
      LwgPdfPCell cell;
      table.getDefaultCell().setPadding(5f);

      table.add("Your name:");
      cell = new LwgPdfPCell();
      cell.setCellEvent(new SimpleRegistrationForm(writer, "name"));
      table.add(cell);

      table.add("Your home address:");
      cell = new LwgPdfPCell();
      cell.setCellEvent(new SimpleRegistrationForm(writer, "address"));
      table.add(cell);

      table.add("Postal code:");
      cell = new LwgPdfPCell();
      cell
          .setCellEvent(new SimpleRegistrationForm(writer,
              "postal_code"));
      table.add(cell);

      table.add("Your email address:");
      cell = new LwgPdfPCell();
      cell.setCellEvent(new SimpleRegistrationForm(writer, "email"));
      table.add(cell);

      document.add(table);

    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
  }
}
TOP

Related Classes of com.lowagie.examples.forms.SimpleRegistrationForm

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.