package it.stefanochizzolini.clown.samples; import it.stefanochizzolini.clown.documents.Document; import it.stefanochizzolini.clown.documents.Page; import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter; import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font; import it.stefanochizzolini.clown.files.File; import java.awt.geom.Point2D; /** This sample is a minimalist introduction to the use of PDF Clown. @author Stefano Chizzolini (http://www.stefanochizzolini.it) @version 0.0.6 */ public class HelloWorldSample implements ISample { public void run( PDFClownSampleLoader loader ) { // 1. Instantiate a new PDF file! /* NOTE: a File object is the low-level (syntactic) representation of a PDF file. */ File file = new File(); // 2. Get its corresponding document! /* NOTE: a Document object is the high-level (semantic) representation of a PDF file. */ Document document = file.getDocument(); // 3. Insert the contents into the document! populate(document); // (boilerplate metadata insertion -- ignore it) loader.buildAccessories(document,this.getClass(),"Hello world","a simple 'hello world'"); // 4. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)! loader.serialize(file,this.getClass().getSimpleName(),false); } /** Populates a PDF file with contents. */ private void populate( Document document ) { // 1. Add the page to the document! Page page = new Page(document); // Instantiates the page inside the document context. document.getPages().add(page); // Puts the page in the pages collection. // 2. Create a content builder for the page! PrimitiveFilter builder = new PrimitiveFilter(page); // 3. Inserting contents... // Set the font to use! builder.setFont( new StandardType1Font( document, StandardType1Font.FamilyNameEnum.Courier, true, false ), 32 ); // Show the text onto the page! builder.showText( "Hello World!", new Point2D.Double(32,48) ); // 4. Flush the contents into the page! builder.flush(); } }