01: package it.stefanochizzolini.clown.samples;
02:
03: import it.stefanochizzolini.clown.bytes.Buffer;
04: import it.stefanochizzolini.clown.documents.Document;
05: import it.stefanochizzolini.clown.documents.Page;
06: import it.stefanochizzolini.clown.documents.PageFormat;
07: import it.stefanochizzolini.clown.documents.contents.Contents;
08: import it.stefanochizzolini.clown.documents.contents.ContentScanner;
09: import it.stefanochizzolini.clown.documents.contents.Fonts;
10: import it.stefanochizzolini.clown.documents.contents.Resources;
11: import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
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;
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 appreciate the BIG simplification that has been
23: introduced with the current version (0.0.5): PDF Clown keeps unaltered its powerful facilities
24: for fine-grained access to the PDF domain, introducing at the same time a bunch of shortcuts
25: that automate routine tasks and ease users' experience. This way, users can opt either to gain
26: full control over the PDF model or to go straight to their high-level intended result.</p>
27: <p>PDF Clown's stacked architecture allows multiple levels of progressive
28: abstraction (byte, token, object, file, document, content streaming, content block typesetting,
29: content flow typesetting), to narrow the working scope so much as needed.</p>
30: <p>Since version 0.0.3, it has reached the content block typesetting (i.e. content
31: placement within a page's canvas) implementation. Content flow typesetting (i.e. content
32: placement across multiple pages) implementation will be the target of the following releases: it
33: will deliver a higher level of abstraction that shall permit further coding simplification, at
34: your will.</p>
35: */
36: public class HelloWorldSample implements ISample {
37: // <dynamic>
38: // <interface>
39: // <public>
40: // <ISample>
41: public void run(PDFClownSampleLoader loader) {
42: // 1. Instantiate a new PDF file!
43: /* NOTE: a File object is the low-level (syntactic) representation of a PDF file. */
44: File file = new File();
45:
46: // 2. Get its corresponding document!
47: /* NOTE: a Document object is the high-level (semantic) representation of a PDF file. */
48: Document document = file.getDocument();
49:
50: // 3. Insert the contents into the document!
51: populate(document);
52:
53: // (boilerplate metadata insertion -- ignore it)
54: loader.buildAccessories(document, this .getClass(),
55: "Hello world", "a simple 'hello world'");
56:
57: // 4. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)!
58: loader.serialize(file, this .getClass().getSimpleName(), false);
59: }
60:
61: // </ISample>
62: // </public>
63:
64: // <private>
65: /**
66: Populates a PDF file with contents.
67: */
68: private void populate(Document document) {
69: // 1. Add the page to the document!
70: Page page = new Page(document); // Instantiates the page inside the document context.
71: document.getPages().add(page); // Puts the page in the pages collection.
72:
73: // 2. Create a content builder for the page!
74: PrimitiveFilter builder = new PrimitiveFilter(page);
75:
76: // 3. Inserting contents...
77: // Set the font to use!
78: builder.setFont(new StandardType1Font(document,
79: StandardType1Font.FamilyNameEnum.Courier, true, false),
80: 32);
81: // Show the text onto the page!
82: builder.showText("Hello World!", new Point2D.Double(32, 48));
83:
84: // 4. Flush the contents into the page!
85: builder.flush();
86: }
87: // </private>
88: // </interface>
89: // </dynamic>
90: }
|