import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.PrintStream;

public class TableTester {
    public static void main(String[] args) throws Exception {

        // create the PDF file
        Document doc = new Document(PageSize.LETTER, 9, 9, 36, 36);
        PdfWriter pdf = PdfWriter.getInstance(doc, new java.io.FileOutputStream("htmltest.pdf"));
        doc.open();

        doc.add(new Chunk("Output from \"Table\" class"));
        normalTableTest(doc);

        // quit
        doc.close();
    }

    public static void normalTableTest(Document doc) throws Exception
    {
        // table creation
        Table t1 = new Table(2);
        Cell c1 = new Cell(new Chunk("italic", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC)));

        Table t2 = new Table(1);
        Cell c2 = new Cell(new Chunk("bold", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD)));

        Table t3 = new Table(1);
        Cell c3 = new Cell("normal");

        // add cells
        t3.addCell(c3);
        c2.add(t3);
        t2.addCell(c2);
        c1.add(t2);
        t1.addCell(c1);
        t1.addCell("b");
        t1.addCell("c");
        t1.addCell("d");

        // debugging info
        printTableStructure(t1, new StringBuffer("\t"), System.out);

        // add to document
        doc.add(t1);
    }

    /**
     * Prints debugging info on the given table by
     * printing the location of each cell, including
     * all sub-tables and their respective cells.
     *
     * <p>The output to the specified PrintWriter is in
     * XML to enable easy reading/parsing of complicated
     * tables.
     *
     * @param t The table to print
     * @param lead A StringBuffer with a formatting character,
     * such as '\t', to make the console output easier to read.
     * @param ps A PrintStream the output should be sent to.
     */
    public static void printTableStructure(Table t,
                                           StringBuffer lead,
                                           PrintStream ps)
    {
        if (lead.length() == 1)
            ps.println("<DEBUG-TABLE note=\"positions are relative to parent table, not nested table\">");

        if (t == null)
            return;

        for (int i = 0; i < t.size(); i++)
            for (int j = 0; j < t.columns(); j++)
            {
                Object emt = t.getElement(i, j);
                if (emt instanceof Cell)
                {
                    ps.println(lead + "<Cell position=\"" + i + "," + j + "\" />");
                }
                else
                {
                    if (emt != null)
                    {
                        ps.println(lead + "<NESTED-TABLE position=\"" + i + "," + j + "\">");
                        printTableStructure((Table) emt, lead.append("\t"), ps);
                        ps.println(lead.deleteCharAt(lead.length() - 1) + "</NESTED-TABLE>");
                    }
                }
            }

        if (lead.length() == 1)
            ps.println("</DEBUG-TABLE>");
    }
}