/*
 * $Id: Chap1007.java,v 1.1 2001/08/01 10:27:01 blowagie Exp $
 * $Name:  $
 *
 * This code is free software. It may only be copied or modified
 * if you include the following copyright notice:
 *
 * --> Copyright 2001 by Bruno Lowagie <--
 *
 * This code is part of the 'iText Tutorial'.
 * You can find the complete tutorial at the following address:
 * http://www.lowagie.com/iText/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.
 *
 * ir-arch Bruno Lowagie,
 * Adolf Baeyensstraat 121
 * 9040 Sint-Amandsberg
 * BELGIUM
 * tel. +32 (0)9 228.10.97
 * bruno@lowagie.com
 */

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

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;

public class Chap1007 {

	public static void main(String[] args) {

		System.out.println("Chapter 10 example 7: Columns");

		// step 1: creation of a document-object
		Document document = new Document();

		try {

			// step 2:
			// we create a writer that listens to the document
			// and directs a PDF-stream to a file
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Chap1007.pdf"));

			// step 3: we open the document
			document.open();

			// step 4:
            BaseFont bf = BaseFont.createFont("Helvetica", "Cp1252", false);
            Font font = new Font(bf, 11, Font.NORMAL);
                        
            // we grab the ContentByte and do some stuff with it
            PdfContentByte cb = writer.getDirectContent();
            ColumnText ct = new ColumnText(cb);
            String[] address = new String[50];
            float[] left = {20, 210, 400};
            float[] right = {190, 380, 570};
            float[] verticalPosition = {720, 620, 520, 420, 320, 220, 120, 20};


			for(int i=0; i<50; i++) {
            address[i] = "First" + i + " Middle Last And fill it up, so that it will wrap. May be even 2 or more lines\nGroup\nStreet\nCity/AB  Postal\nWhat happens when it is too much\nAnd anoter line\n";
			}            

            int horizontal = 3;
            int vertical = 8;

            int label = 1;
            int column = 1;

            for(int i=0; i<50; i++) {
                System.out.println("page " + writer.getPageNumber() + " vertical " + label + " horizontal " + column);       
				ct.setSimpleColumn((new Phrase(address[i], new Font(Font.HELVETICA, 12))), left[column-1],
					verticalPosition[label-1], right[column-1], verticalPosition[label-1] + 90, 16,
					Element.ALIGN_JUSTIFIED);
				int test = ct.go();
				ct.clearChunks();

                column++;
				if (column >3) {
                    label++;
					column = 1;
                    if (label > 8) {
                        document.newPage();
                        label = 1;
                    }
                }
            } 
		}
		catch(DocumentException de) {
			System.err.println(de.getMessage());
		}
		catch(IOException ioe) {
			System.err.println(ioe.getMessage());
		}

		// step 5: we close the document
		document.close();
	}
}

