01: package it.stefanochizzolini.clown.samples;
02:
03: import it.stefanochizzolini.clown.documents.Document;
04: import it.stefanochizzolini.clown.documents.Page;
05: import it.stefanochizzolini.clown.documents.Pages;
06: import it.stefanochizzolini.clown.documents.PageFormat;
07: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
08: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
09: import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
10: import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
11: import it.stefanochizzolini.clown.files.File;
12:
13: import java.awt.geom.Dimension2D;
14: import java.awt.geom.Point2D;
15: import java.util.EnumSet;
16:
17: /**
18: This sample generates a series of pages from the default page formats available,
19: both in size and orientation.
20: */
21: public class PageFormatSample implements ISample {
22: // <dynamic>
23: // <interface>
24: // <public>
25: public void run(PDFClownSampleLoader loader) {
26: // 1. PDF file instantiation.
27: File file = new File();
28:
29: // 2. Get the document associated to the file!
30: Document document = file.getDocument();
31:
32: // 3. Populate the document!
33: populate(document);
34:
35: // (boilerplate metadata insertion -- ignore it)
36: loader.buildAccessories(document, this .getClass(),
37: "Page Format", "page formats");
38:
39: // 4. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)!
40: loader.serialize(file, this .getClass().getSimpleName(), false);
41: }
42:
43: // </public>
44:
45: // <private>
46: private void populate(Document document) {
47: StandardType1Font bodyFont = new StandardType1Font(document,
48: StandardType1Font.FamilyNameEnum.Courier, true, false);
49:
50: Pages pages = document.getPages();
51: for (PageFormat.SizeEnum pageFormat : EnumSet
52: .allOf(PageFormat.SizeEnum.class)) {
53: for (PageFormat.OrientationEnum pageOrientation : EnumSet
54: .allOf(PageFormat.OrientationEnum.class)) {
55: // Add a page to the document!
56: Page page = new Page(document); // Instantiates the page inside the document context.
57: pages.add(page); // Puts the page in the pages collection.
58:
59: // Set the page size!
60: page.setSize(PageFormat.getSize(pageFormat,
61: pageOrientation));
62:
63: // Drawing the text label on the page...
64: Dimension2D pageSize = page.getSize();
65: PrimitiveFilter builder = new PrimitiveFilter(page);
66: builder.setFont(bodyFont, 32);
67: builder.showText(pageFormat + " (" + pageOrientation
68: + ")", // Text.
69: new Point2D.Double(pageSize.getWidth() / 2,
70: pageSize.getHeight() / 2), // Location: page center.
71: AlignmentXEnum.Center, // Place the text on horizontal center of the location.
72: AlignmentYEnum.Middle, // Place the text on vertical middle of the location.
73: 45 // Rotate the text 45 degrees counterclockwise.
74: );
75: builder.flush();
76: }
77: }
78: }
79: }
|