//* Copyright (c) 2002, 2003 by Windward Studios, Inc. All rights reserved.

package net.windward.format.pdf.test;

import java.io.*;
import java.util.*;

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


/**
 * Test the PdfOutputBuilder class
 */
public class PdfTest {

    public static void main(String[] args) throws Exception {
	
		try {
			System.out.println( "Creating the document" );
			Document document = new Document( new com.lowagie.text.Rectangle( 0, 0, 8.5f * 72.0f, 11 * 72 )); 
        
			System.out.println( "Writing headers" );
			document.addAuthor( "David Thielen" );
			document.addHeader( "My Header", "Hi there" );

			System.out.println( "opening document" );
			PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( "test.pdf" ));
			PdfContentByte cb = writer.getDirectContent();	
			document.open();
			
			System.out.println( "Writing text" );
			cb.beginText();
			BaseFont bf = BaseFont.createFont( BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED );
			cb.setFontAndSize( bf, 12.0f );
			for (int ind=1; ind<5; ind++)
				cb.showTextAligned( PdfContentByte.ALIGN_LEFT, "Some text at " + ind, 72 * ind, (11 - ind) * 72, 0 );
			cb.endText(); 
			
			System.out.println( "Drawing boxes" );
			for (int ind=1; ind<10; ind+=2) {
				cb.moveTo( 72 * ind,      (9 - ind) * 72 );
				cb.lineTo( 72 * ind + 72, (9 - ind) * 72 );
				cb.lineTo( 72 * ind + 72, (8 - ind) * 72 );
				cb.lineTo( 72 * ind,      (8 - ind) * 72 );
				cb.lineTo( 72 * ind,      (9 - ind) * 72 );
				cb.stroke(); 
			}

			System.out.println( "Loading image" );
			Image img = Image.getInstance( "windward.jpg" );
			if (img != null) {
				System.out.println( "Drawing image 72x108 at 5,1" );
				img.setAbsolutePosition( 5 * 72, 10 * 72 - 172 );
				System.out.println( "pos = " + img.absoluteX() + ',' + img.absoluteY() + "; dpi = " + img.getDpiX() + ',' +
						img.getDpiY() + "; bitmap size = " + img.plainWidth() + ',' + img.plainHeight() +
						"; scaled size = " + img.scaledWidth() + "," + img.scaledHeight() );
				cb.addImage( img );

				System.out.println( "Drawing image 72x108 at 1,5" );
				img.setAbsolutePosition( 1 * 72, 6 * 72 - 172 );
				System.out.println( "pos = " + img.absoluteX() + ',' + img.absoluteY() + "; dpi = " + img.getDpiX() + ',' +
						img.getDpiY() + "; bitmap size = " + img.plainWidth() + ',' + img.plainHeight() +
						"; scaled size = " + img.scaledWidth() + "," + img.scaledHeight() );
				cb.addImage( img );
			}

			System.out.println( "Closing the document" );
			document.close();
			
		} catch (DocumentException de) {
			throw new IOException( "DocumentException: " + de.getMessage() );
		}
		
		System.out.println( "all done" );
	}

}
