package wcenter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.GrayColor;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class TestColumnTextAlign {

	public static void main(String[] args) throws DocumentException, FileNotFoundException {

		System.out.println("Test: Does columnText align properly?");
		
		// step 1: creation of a document-object
		Document document = new Document(PageSize.A4);

		// step 2:
		// we create a writer
		PdfWriter wtr = PdfWriter.getInstance(
				// that listens to the document
				document,
				// and directs a PDF-stream to a file
				new FileOutputStream("c:/myjavasoft/testColumnText.pdf"));
		// step 3: we open the document
		document.open();

		Rectangle leftBox = new Rectangle(10, 10, 250, 400);

		leftBox.setBackgroundColor(new GrayColor(0.6F));
		
		Rectangle rightBox = new Rectangle(300, 10, 550, 400);
		rightBox.setBackgroundColor(new GrayColor(0.6F));
		
		PdfContentByte dc = wtr.getDirectContentUnder();
		
		dc.saveState();
		
		dc.rectangle(leftBox);
		
		dc.rectangle(rightBox);
		
		dc.restoreState();

		ColumnText ct = new ColumnText(wtr.getDirectContent());
		ct.setUseAscender(true);

		Font fontBig = FontFactory.getFont("times", 22);
		Font fontSml = FontFactory.getFont("times", 10);

		String basetext = "The quick brown Fox jumps over the lazy dog.";
		
		String textTitle = basetext;
		
		String textBody = "";
		for (int i = 0; i < 50; i++) {
			textBody = textBody + basetext;
		}
		int status = 0;

		Paragraph paraBig = new Paragraph(textTitle, fontBig);
				
		Paragraph paraSml = new Paragraph(textBody, fontSml);

		ct.addElement(paraBig);
		ct.addElement(paraSml);

		ct.setSimpleColumn(leftBox.getLeft(), leftBox.getBottom(), leftBox.getRight(), leftBox.getTop());

		status = ct.go();

		if( ColumnText.hasMoreText(status) ){

			ct.setSimpleColumn(rightBox.getLeft(), rightBox.getBottom(), rightBox.getRight(), rightBox.getTop());

			status = ct.go();
		}
		
		document.close();

		
	}
}
