001: package it.stefanochizzolini.clown.samples;
002:
003: import it.stefanochizzolini.clown.documents.Document;
004: import it.stefanochizzolini.clown.documents.Page;
005: import it.stefanochizzolini.clown.documents.contents.colorSpaces.DeviceRGBColor;
006: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
007: import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
008: import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
009: import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
010: import it.stefanochizzolini.clown.documents.contents.xObjects.FormXObject;
011: import it.stefanochizzolini.clown.files.File;
012: import it.stefanochizzolini.clown.tokens.FileFormatException;
013: import it.stefanochizzolini.clown.tools.PageStamper;
014:
015: import java.awt.geom.Dimension2D;
016: import java.awt.geom.Point2D;
017:
018: /**
019: This sample demonstrates how to insert watermark text into an existing document.
020: <h3>Remarks</h3>
021: <p>This implementation uses a Form XObject [PDF:1.6:4.9] to conveniently achieve a consistent page
022: background. Form XObjects provide context independence encapsulating their contents (and resources)
023: in a single stream: such an approach allows content reuse.</p>
024: <p>The watermark is seamlessly inserted under each page content using the PageStamper class.</p>
025: */
026: public class WatermarkSample implements ISample {
027: public void run(PDFClownSampleLoader loader) {
028: // (boilerplate user choice -- ignore it)
029: String filePath = loader
030: .getPdfFileChoice("Please select a PDF file");
031:
032: // 1. Open the PDF file!
033: File file;
034: try {
035: file = new File(filePath);
036: } catch (FileFormatException e) {
037: throw new RuntimeException(filePath
038: + " file has a bad file format.", e);
039: } catch (Exception e) {
040: throw new RuntimeException(
041: filePath + " file access error.", e);
042: }
043:
044: // Get the PDF document!
045: Document document = file.getDocument();
046:
047: // 2. Create a watermark (form)!
048: FormXObject watermark = createWatermark(document);
049:
050: // 3. Apply the watermark to the pages of the document!
051: applyWatermark(watermark);
052:
053: // (boilerplate metadata insertion -- ignore it)
054: loader.buildAccessories(document, this .getClass(), "Watermark",
055: "how to place some content behind existing pages");
056:
057: // 4. Serialize the PDF file (again, boilerplate code -- see the PDFClownSampleLoader class source code)!
058: loader.serialize(file, this .getClass().getSimpleName());
059: }
060:
061: private void applyWatermark(FormXObject watermark) {
062: // 1. Instantiate the stamper!
063: /* NOTE: The PageStamper is optimized for dealing with pages. */
064: PageStamper stamper = new PageStamper();
065:
066: // 2. Inserting the watermark into each page of the document...
067: for (Page page : watermark.getDocument().getPages()) {
068: // 2.1. Associate the page to the stamper!
069: stamper.setPage(page);
070:
071: // 2.2. Stamping the watermark on the background...
072: // Get the background 'layer' of the page!
073: PrimitiveFilter background = stamper.getBackground();
074: // Show the watermark into the page background!
075: background.showXObject(watermark);
076:
077: // 2.3. End the stamping!
078: stamper.flush();
079: }
080: }
081:
082: private FormXObject createWatermark(Document document) {
083: // 1. Create a new external form object to represent the watermark!
084: FormXObject watermark = new FormXObject(document);
085: // Size.
086: Dimension2D size = document.getSize();
087: watermark.setSize(size);
088:
089: // 2. Inserting the contents of the watermark...
090: // 2.1. Create a content builder for the watermark!
091: PrimitiveFilter builder = new PrimitiveFilter(watermark);
092: // 2.2. Inserting the contents...
093: // Set the font to use!
094: builder.setFont(new StandardType1Font(document,
095: StandardType1Font.FamilyNameEnum.Courier, true, false),
096: 120);
097: // Set the color to fill the text characters!
098: builder.setFillColor(new DeviceRGBColor(115f / 255, 164f / 255,
099: 232f / 255));
100: // Show the text!
101: builder.showText("PDFClown", // Text to show.
102: new Point2D.Double(size.getWidth() / 2d, size
103: .getHeight() / 2d), // Anchor location: page center.
104: AlignmentXEnum.Center, // Horizontal placement (relative to the anchor): center.
105: AlignmentYEnum.Middle, // Vertical placement (relative to the anchor): middle.
106: 50 // Rotation: 50-degree-counterclockwise.
107: );
108: // 2.3. Flush the contents into the watermark!
109: builder.flush();
110:
111: return watermark;
112: }
113: }
|