KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > samples > HelloWorldSample


1 package it.stefanochizzolini.clown.samples;
2
3 import it.stefanochizzolini.clown.bytes.Buffer;
4 import it.stefanochizzolini.clown.documents.Document;
5 import it.stefanochizzolini.clown.documents.Page;
6 import it.stefanochizzolini.clown.documents.PageFormat;
7 import it.stefanochizzolini.clown.documents.contents.ContentStream;
8 import it.stefanochizzolini.clown.documents.contents.Fonts;
9 import it.stefanochizzolini.clown.documents.contents.Resources;
10 import it.stefanochizzolini.clown.documents.contents.colorSpaces.DeviceGrayColor;
11 import it.stefanochizzolini.clown.documents.contents.composition.ContentBuilder;
12 import it.stefanochizzolini.clown.documents.contents.fonts.Font;
13 import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
14 import it.stefanochizzolini.clown.files.File;
15 import it.stefanochizzolini.clown.objects.PdfName;
16
17 import java.awt.geom.Point2D JavaDoc;
18
19 /**
20   This sample is a minimalist introduction to the use of PDF Clown.
21   <h3>Remarks</h3>
22   <p>Skimming through its source code you may feel that this sample is quite elaborate to be a
23   simple 'Hello world': that's because PDF Clown's approach to PDF file manipulation is all about
24   providing full control over the PDF model.</p>
25   <p>Its stacked architecture, at the same time, allows multiple levels of progressive abstraction
26   (byte, token, object, file, document, content streaming, content block typesetting, content flow
27   typesetting), to narrow the working scope so much as needed.</p>
28   <p>Current version (0.0.3) has just reached the content block typesetting (i.e. content placement
29   within a page's canvas) implementation. Content flow typesetting (i.e. content placement across
30   multiple pages) implementation will be the target of the following releases: it will deliver a
31   higher level of abstraction that shall permit further coding simplification, at your will.</p>
32 */

33 public class HelloWorldSample
34   implements ISample
35 {
36   // <static>
37
// <fields>
38
private static final PdfName ResourceName_DefaultFont = new PdfName("default");
39   // </fields>
40
// </static>
41

42   // <dynamic>
43
// <interface>
44
// <public>
45
// <ISample>
46
public void run(
47     PDFClownSampleLoader loader
48     )
49   {
50     // 1. Instantiate a new PDF file!
51
/* NOTE: a File object is the low-level (syntactic) representation of a PDF file. */
52     File file = new File();
53
54     try
55     {
56       // 2. Get its corresponding document!
57
/* NOTE: a Document object is the high-level (semantic) representation of a PDF file. */
58       Document document = file.getDocument();
59
60       // 3. Set the document properties and resources!
61
initialize(document);
62
63       // 4. Insert the contents into the document!
64
populate(document);
65     }
66     catch(Exception JavaDoc e)
67     {throw new RuntimeException JavaDoc(e);}
68
69     // 5. Serialize the PDF file!
70
loader.serialize(file,this.getClass().getSimpleName(),false);
71   }
72   // </ISample>
73
// </public>
74

75   // <private>
76
/**
77     Prepares the basic settings for populating a PDF file.
78   */

79   private void initialize(
80     Document document
81     )
82   {
83     // 1. Set default page size (A4)!
84
document.setPageSize(PageFormat.getSize());
85
86     // 2. Setting the document resources...
87
// 2.1. Resources collection.
88
Resources resources = new Resources(document); // Instantiates the resources collection inside the document context.
89
document.setResources(resources); // Puts the resources collection in the common resources role.
90
// 2.2. Fonts collection.
91
Fonts fonts = new Fonts(document); // Instantiates the fonts collection inside the document context.
92
resources.setFonts(fonts); // Puts the fonts collection in the common resources role.
93
// Add a font to the fonts collection!
94
fonts.put(
95       ResourceName_DefaultFont,
96       new StandardType1Font(
97         document,
98         StandardType1Font.FamilyNameEnum.Courier,
99         true,
100         false
101         )
102       );
103   }
104
105   /**
106     Populates a PDF file with contents.
107   */

108   private void populate(
109     Document document
110     )
111   {
112     // 1. Add the page to the document!
113
Page page = new Page(document); // Instantiates the page inside the document context.
114
document.getPages().add(page); // Puts the page in the pages collection.
115
// 2. Add a content stream to the page!
116
ContentStream content = new ContentStream(document); // Instantiates the content stream inside the document context.
117
page.getContents().add(content); // Puts the content stream in the page's content streams collection.
118
// 3. Create a content fragment for the content stream!
119
ContentBuilder contentBuilder = new ContentBuilder(content,new Buffer());
120     // 4. Inserting contents...
121
// Set the filling color!
122
contentBuilder.setFillColor(
123       new DeviceGrayColor(0)
124       );
125     // Begin the text contents!
126
contentBuilder.beginText();
127     // Set the font to use!
128
contentBuilder.setFont(ResourceName_DefaultFont,32);
129     // Show the text onto the page!
130
contentBuilder.showText(
131       "Hello World!",
132       new Point2D.Double JavaDoc(32,48)
133       );
134     // End the text contents!
135
contentBuilder.endText();
136     // 5. Append the content fragment to the content stream!
137
content.append(contentBuilder.getBuffer());
138   }
139   // </private>
140
// </interface>
141
// </dynamic>
142
}
Popular Tags