import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.*;
import java.util.Random;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class TemplatePanel extends Canvas {
    private Random rand = new Random();
    private static final int WIDTH = 600;
    private static final int HEIGHT = 150;
    private static int counter = 0;
    private PdfWriter pdf;
    
    public void paint(Graphics g) {
        counter++;
        for (int i = 0; i < 100; i++) {
            g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
            g.drawLine(rand.nextInt(WIDTH),
            rand.nextInt(HEIGHT),
            rand.nextInt(WIDTH),
            rand.nextInt(HEIGHT));
        }
        g.setColor(Color.black);
        g.fillRect(WIDTH / 2, HEIGHT / 2, 30, 30);
        g.setColor(Color.white);
        g.drawString(Integer.toString(counter), WIDTH / 2 + 6, HEIGHT / 2 + 15);
    }
    
    /**
     * Shows the output of the TemplatePanel in a standard Frame.
     */
    public static void frameTest() {
        java.awt.Frame frm = new java.awt.Frame("test");
        frm.add(new TemplatePanel());
        frm.pack();
        
        frm.setSize(WIDTH, HEIGHT);
        frm.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) { System.exit(0); }
        });
        frm.show();
    }
    
    /**
     *
     * @throws Exception
     */
    public void init() throws Exception {
        Document doc = new Document(PageSize.A3, 18, 18, 36, 36);
        pdf = PdfWriter.getInstance(doc, new java.io.FileOutputStream("c:\\htmltest.pdf"));
        
        doc.open();
        addSomeText(doc);
        doc.newPage();
        doc.add(new Paragraph("hello"));
        
        pdfPTableTest(doc, pdf);
        doc.newPage();
        normalTableTest(doc, pdf);
        doc.close();
    }
    
    public void addSomeText(Document doc) throws Exception {
        for (int i = 0; i < 100; i++) {
            doc.add(new Phrase("This is a Portable Document Format file..."));
        }
    }
    
    public Image getImg() throws Exception {
        // create the PDF template
        PdfTemplate pt = pdf.getDirectContent().createTemplate(WIDTH, HEIGHT);
        Graphics g = pt.createGraphics(WIDTH, HEIGHT);
        TemplatePanel tempPanel = new TemplatePanel();
        
        //pdf.getDirectContent().saveState();
        tempPanel.paint(g);
        g.dispose();
        //pdf.getDirectContent().restoreState();
        return Image.getInstance(pt);
    }
    
    public void normalTableTest(Document doc, PdfWriter pdf)
    throws Exception {
        Table t = new Table(1);
        t.addCell(new Cell(getImg()));
        t.addCell(new Cell(getImg()));
        t.addCell(new Cell(getImg()));
        t.addCell(new Cell(getImg()));
        // if you uncomment the next line and run this method instead of pdfPTableTest
        // iText causes an ArrayIndexOutOfBoundsException
        t.addCell(new Cell(getImg()));
        doc.add(new Chunk("Using PDF templates with Table"));
        doc.add(t);
    }
    
    public void pdfPTableTest(Document doc, PdfWriter pdf)
    throws Exception {
        PdfPTable t = new PdfPTable(1);
        
        t.addCell(new PdfPCell(getImg()));
        t.addCell(new PdfPCell(getImg()));
        t.addCell(new PdfPCell(getImg()));
        t.addCell(new PdfPCell(getImg()));
        
        doc.add(new Chunk("Using PDF templates with PdfPTable"));
        doc.add(t);
    }
    
    public static void main(String[] args) throws Exception {
        new TemplatePanel().init();
    }
}