import java.io.FileOutputStream;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfPTable;

public class TestPageSplitting {
  public static void main(String args[])
    throws Exception
  {
    boolean fitToPage = true;
    String fileName = "TestPageSplitting.pdf";

    if(args.length < 1 || args.length > 2) {
      System.err.println("Usage: TestPageSplitting [nosplit] outputFile");
      System.err.println(" nosplit");
      System.err.println("   specify this option if you want to run the test without 
      System.err.println("   splitting up the table");
      System.err.println(" outputFile");
      System.err.println("   output to this file");
      return;
    }
    if(args.length == 1) {
      fileName = args[0];
    }
    else {
      String noSplit = args[0];
      fitToPage = ! noSplit.equalsIgnoreCase("nosplit");
      fileName = args[1];
    }

    FileOutputStream pdfFile = new FileOutputStream(fileName);
    PdfWriter writer = null;
    Document document = new Document(PageSize.LETTER, 50, 50, 50, 50);
    writer = PdfWriter.getInstance(document, pdfFile);

    String textA = "Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.";  
    String textB = "Beware the Jabberwock, my son!\nThe jaws that bite, the claws that catch!\nBeware the Jubjub bird, and shun\nThe frumious Bandersnatch!";
    String textC = "He took his vorpal sword in hand:\nLong time the manxome foe he sought --\nSo rested he by the Tumtum tree,\nAnd stood awhile in thought.";
    String textD = "And, as in uffish thought he stood,\nThe Jabberwock, with eyes of flame,\nCame whiffling through the tulgey wood,\nAnd burbled as it came!";
    String textE = "One, two! One, two! And through and through\nThe vorpal blade went snicker-snack!\nHe left it dead, and with its head\nHe went galumphing back.";
    String textF = "And, has thou slain the Jabberwock?\nCome to my arms, my beamish boy!\nO frabjous day! Callooh! Callay!'\nHe chortled in his joy.";

      float[] widths = {30, 70};
      PdfPTable topTable = new PdfPTable(widths);
      topTable.setWidthPercentage(100);
      topTable.setHeaderRows(1);
      topTable.getDefaultCell().setPadding(0);
      topTable.addCell("poem");
      topTable.addCell("text");

      topTable.addCell("Jabberwocky");

      Font cellFont = new Font(Font.TIMES_ROMAN, 25);

      PdfPTable nestedTable = new PdfPTable(1);
      nestedTable.setWidthPercentage(100);
      nestedTable.getDefaultCell().setPadding(0);
      nestedTable.addCell(new Phrase (textA, cellFont));
      nestedTable.addCell(new Phrase (textB, cellFont));
      nestedTable.addCell(new Phrase (textC, cellFont));
      nestedTable.addCell(new Phrase (textD, cellFont));
      nestedTable.addCell(new Phrase (textE, cellFont));
      nestedTable.addCell(new Phrase (textF, cellFont));
      nestedTable.addCell(new Phrase (textA, cellFont));

      topTable.addCell(nestedTable);

      document.open();
      if(fitToPage) {
        writer.fitCellsToPageSize(topTable);
      }
      document.add(topTable);
      document.close();
      pdfFile.close();

  }
}



