Package com.lowagie.examples.forms.create

Source Code of com.lowagie.examples.forms.create.StudentCardForm

/*
* $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.create;


import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.DocumentException;
import com.lowagie.text.LwgElement;
import com.lowagie.text.LwgFont;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
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;

/**
* Generates a StudentCard as a form
* @author blowagie
*/
public class StudentCardForm implements PdfPCellEvent {
 
  /** the writer with the acroform */
  private PdfFormField field;

  /**
   * Construct an implementation of PdfPCellEvent.
   *
   * @param field
   *            a form field
   * 
   */
  public StudentCardForm(PdfFormField field) {
    this.field = field;
  }

  /**
   * @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) {
    field.setWidget(position, null);
  }
 
    /**
     * Generates a StudentCard as a form
     * @param args no arguments needed here
     */
    public static void main(String[] args) {
       
        System.out.println("StudentCard as a form");
       
        // step 1: creation of a document-object
        LwgRectangle rect = new LwgRectangle(243, 153);
        rect.setBackgroundColor(new Color(0xFF, 0xFF, 0xCC));
        LwgDocument document = new LwgDocument(rect, 10, 10, 10, 10);
       
        try {
           
            // step 2:
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("studentcardform.pdf"));
           
            // step 3: we open the document
            document.open();
           
            // step 4:
            LwgFont font = FontFactory.getFont(FontFactory.HELVETICA, 14, LwgFont.BOLD, Color.BLUE);
            Paragraph p = new Paragraph("Ghent University", font);
            p.setAlignment(LwgElement.ALIGN_CENTER);
            document.add(p);
            LwgFont f = FontFactory.getFont(FontFactory.HELVETICA, 8);
            LwgPdfPTable outertable = new LwgPdfPTable(3);
            outertable.setTotalWidth(200);
            outertable.getDefaultCell().setBorder(LwgRectangle.NO_BORDER);
            float[] outer = { 60, 25, 15 };
            outertable.setWidths(outer);
            LwgPdfPTable innertable = new LwgPdfPTable(2);
            float[] inner = {35, 65};
            innertable.setWidths(inner);
            LwgPdfPCell cell;
            TextField text;
            innertable.add(new Paragraph("name:", f));
            cell = new LwgPdfPCell();
            text = new TextField(writer, new LwgRectangle(0, 0), "name");
            text.setOptions(TextField.MULTILINE);
            text.setFontSize(8);
            PdfFormField name = text.getTextField();
            cell.setCellEvent(new StudentCardForm(name));
            innertable.add(cell);
            innertable.add(new Paragraph("date of birth:", f));
            cell = new LwgPdfPCell();
            text = new TextField(writer, new LwgRectangle(0, 0), "birthday");
            text.setOptions(TextField.MULTILINE);
            text.setFontSize(8);
            PdfFormField birthdate = text.getTextField();
            cell.setCellEvent(new StudentCardForm(birthdate));
            innertable.add(cell);
            innertable.add(new Paragraph("Study Program:", f));
            cell = new LwgPdfPCell();
            text = new TextField(writer, new LwgRectangle(0, 0), "studyprogram");
            text.setOptions(TextField.MULTILINE);
            text.setFontSize(8);
            PdfFormField studyprogram = text.getTextField();
            studyprogram.setFieldName("studyprogram");
            cell.setCellEvent(new StudentCardForm(studyprogram));
            innertable.add(cell);
            innertable.add(new Paragraph("option:", f));
            cell = new LwgPdfPCell();
            text = new TextField(writer, new LwgRectangle(0, 0), "option");
            text.setOptions(TextField.MULTILINE);
            text.setFontSize(8);
            PdfFormField option = text.getTextField();
            option.setFieldName("option");
            cell.setCellEvent(new StudentCardForm(option));
            innertable.add(cell);
            outertable.add(innertable);
            cell = new LwgPdfPCell();
      cell.setBackgroundColor(new Color(0xFF, 0xDE, 0xAD));
            PdfFormField picture = PdfFormField.createPushButton(writer);
            picture.setFieldName("picture");
            cell.setCellEvent(new StudentCardForm(picture));
            outertable.add(cell);
            cell = new LwgPdfPCell();
      cell.setBackgroundColor(Color.WHITE);
            PdfFormField barcode = PdfFormField.createPushButton(writer);
            barcode.setFieldName("barcode");
            cell.setCellEvent(new StudentCardForm(barcode));
            outertable.add(cell);
            outertable.writeSelectedRows(0, -1, 20, 100, writer.getDirectContent());
            writer.addAnnotation(name);
            writer.addAnnotation(birthdate);
            writer.addAnnotation(studyprogram);
            writer.addAnnotation(option);
            writer.addAnnotation(picture);
            writer.addAnnotation(barcode);
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
       
        // step 5: we close the document
        document.close();
    }
}
TOP

Related Classes of com.lowagie.examples.forms.create.StudentCardForm

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.