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.contents.colorSpaces.DeviceRGBColor;
06: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
07: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
08: import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
09: import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
10: import it.stefanochizzolini.clown.files.File;
11: import it.stefanochizzolini.clown.tokens.FileFormatException;
12: import it.stefanochizzolini.clown.tools.PageStamper;
13:
14: import java.awt.geom.Dimension2D;
15: import java.awt.geom.Point2D;
16: import java.util.Date;
17:
18: /**
19: This sample demonstrates how to simply insert (stamp) new contents on an existing document.
20: <h3>Remarks</h3>
21: <p>Stamping is just one of the several ways PDF contents can be manipulated using PDF Clown:
22: contents can be inserted as (raw) data chunks, mid-level content objects, external forms,
23: etc.</p>
24: */
25: public class PageStampSample implements ISample {
26: public void run(PDFClownSampleLoader loader) {
27: // (boilerplate user choice -- ignore it)
28: String filePath = loader
29: .getPdfFileChoice("Please select a PDF file");
30:
31: // 1. Open the PDF file!
32: File file;
33: try {
34: file = new File(filePath);
35: } catch (FileFormatException e) {
36: throw new RuntimeException(filePath
37: + " file has a bad file format.", e);
38: } catch (Exception e) {
39: throw new RuntimeException(
40: filePath + " file access error.", e);
41: }
42:
43: // Get the PDF document!
44: Document document = file.getDocument();
45:
46: // 2. Stamp the document!
47: stamp(document);
48:
49: // (boilerplate metadata insertion -- ignore it)
50: loader.buildAccessories(document, this .getClass(), "Stamping",
51: "content stamping inside an existing page");
52:
53: // 3. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)!
54: loader.serialize(file, this .getClass().getSimpleName());
55: }
56:
57: private void stamp(Document document) {
58: // Get the first page within the document!
59: Page page = document.getPages().get(0);
60:
61: // Instantiate a stamper associating it to the page!
62: /* NOTE: The PageStamper is optimized for dealing with pages. */
63: PageStamper stamper = new PageStamper(page);
64:
65: StandardType1Font font = new StandardType1Font(document,
66: StandardType1Font.FamilyNameEnum.Courier, true, false);
67: Dimension2D pageSize = page.getSize();
68: // Stamping...
69: // ...foreground.
70: {
71: PrimitiveFilter foreground = stamper.getForeground();
72: foreground.setFont(font, 16);
73: foreground.setFillColor(new DeviceRGBColor(1, 0, 0));
74: Date now = new Date();
75: foreground.showText(now.toString(), new Point2D.Double(32,
76: 32));
77: foreground.showText(now.toString(),
78: new Point2D.Double(pageSize.getWidth() - 32,
79: pageSize.getHeight() - 32),
80: AlignmentXEnum.Right, AlignmentYEnum.Bottom, 0);
81: }
82:
83: // ...background.
84: {
85: PrimitiveFilter background = stamper.getBackground();
86: background.setFont(font, 64);
87: background.setFillColor(new DeviceRGBColor(.5, .5, .8));
88: background.showText("PDFClown", new Point2D.Double(pageSize
89: .getWidth() / 2, pageSize.getHeight() / 2),
90: AlignmentXEnum.Center, AlignmentYEnum.Middle, 45);
91: }
92:
93: // End the stamping!
94: stamper.flush();
95: }
96: }
|