KickJava   Java API By Example, From Geeks To Geeks.

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


1 package it.stefanochizzolini.clown.samples;
2
3 import it.stefanochizzolini.clown.bytes.Buffer;
4 import it.stefanochizzolini.clown.bytes.FileInputStream;
5 import it.stefanochizzolini.clown.documents.Document;
6 import it.stefanochizzolini.clown.documents.Page;
7 import it.stefanochizzolini.clown.documents.Pages;
8 import it.stefanochizzolini.clown.documents.contents.ContentStream;
9 import it.stefanochizzolini.clown.documents.contents.ContentStreams;
10 import it.stefanochizzolini.clown.documents.contents.Fonts;
11 import it.stefanochizzolini.clown.documents.contents.Resources;
12 import it.stefanochizzolini.clown.documents.contents.XObjects;
13 import it.stefanochizzolini.clown.documents.contents.colorSpaces.DeviceRGBColor;
14 import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
15 import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
16 import it.stefanochizzolini.clown.documents.contents.composition.ContentBuilder;
17 import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
18 import it.stefanochizzolini.clown.documents.contents.xObjects.FormXObject;
19 import it.stefanochizzolini.clown.files.File;
20 import it.stefanochizzolini.clown.objects.PdfName;
21 import it.stefanochizzolini.clown.tokens.FileFormatException;
22
23 import java.awt.geom.Point2D JavaDoc;
24 import java.io.RandomAccessFile JavaDoc;
25
26 /**
27   This sample demonstrates how to insert a watermark text on a preexisting document.
28   <h3>Remarks</h3>
29   <p>This implementation uses a Form XObject [PDF:1.6:4.9] to conveniently achieve
30   a consistent page background. Form XObjects provide context independence
31   encapsulating their contents (and resources) in a single stream: such an approach
32   allows content reuse.</p>
33 */

34 public class WatermarkSample
35   implements ISample
36 {
37   public void run(
38     PDFClownSampleLoader loader
39     )
40   {
41     /*
42       NOTE: This procedure is made up of this sequence of actions:
43       1. User choice.
44       2. Document editing (core).
45       3. Serialization.
46     */

47
48     // 1. User choice.
49
String JavaDoc filePath = loader.getPdfFileChoice("Please select a PDF file");
50
51     // 2. Document editing.
52
File file;
53     try
54     {
55       // Open the PDF file!
56
file = new File(
57         new FileInputStream(
58           new RandomAccessFile JavaDoc(filePath,"r")
59           )
60         );
61     }
62     catch(FileFormatException e)
63     {throw new RuntimeException JavaDoc(filePath + " file has a bad file format.",e);}
64     catch(Exception JavaDoc e)
65     {throw new RuntimeException JavaDoc(filePath + " file access error.",e);}
66
67     // Get the PDF document!
68
Document document = file.getDocument();
69     // Get the page collection!
70
Pages pages = document.getPages();
71
72     // Create a watermark (form)!
73
FormXObject watermark = new FormXObject(document);
74     {
75       // Size.
76
// Try default page size!
77
double height = document.getPageHeight();
78       double width = document.getPageWidth();
79       // No default page size available?
80
if(height == 0)
81       {
82         // Get 1st page size!
83
Page page = pages.get(0);
84         height = page.getHeight();
85         width = page.getWidth();
86       }
87       watermark.setHeight(height);
88       watermark.setWidth(width);
89
90       // Resources.
91
// Set form resources!
92
Resources resources = new Resources(document);
93       watermark.setResources(resources);
94       // Set form fonts!
95
Fonts fonts = new Fonts(document);
96       resources.setFonts(fonts);
97       // Add a font to the form!
98
PdfName fontName = new PdfName("it.stefanochizzolini.clown.samples:Times");
99       fonts.put(
100         fontName,
101         new StandardType1Font(
102           document,
103           StandardType1Font.FamilyNameEnum.Times,
104           true,
105           false
106           )
107         );
108       // Get form content stream!
109
ContentStream content = watermark.getContent();
110       ContentBuilder contentBuilder = new ContentBuilder(content,new Buffer());
111       contentBuilder.beginLocalState();
112       contentBuilder.setFont(fontName,120);
113       // Begin the text context!
114
contentBuilder.beginText();
115       contentBuilder.setFillColor(
116         new DeviceRGBColor(115f/255, 164f/255, 232f/255)
117         );
118       contentBuilder.showText(
119         "PDFClown",
120         new Point2D.Double JavaDoc(width*.2d,height*.65d),
121         AlignmentXEnum.Left,
122         AlignmentYEnum.Top,
123         45
124         );
125       // End the text context!
126
contentBuilder.endText();
127       contentBuilder.endLocalState();
128       // Append the content fragment to the content stream!
129
content.append(contentBuilder.getBuffer());
130     }
131
132     PdfName watermarkName = new PdfName("it.stefanochizzolini.clown.samples:watermark");
133     // Create a new background content stream (within the document)!
134
ContentStream backgroundContent = new ContentStream(document);
135
136     XObjects commonXObjects = null;
137     for(Page page : pages)
138     {
139       // 2.1. Insert the watermark among the page's resources.
140
Resources resources = page.getResources();
141       XObjects xObjects = resources.getXObjects();
142       if(xObjects == null)
143       {
144         if(commonXObjects == null)
145         {commonXObjects = new XObjects(document);}
146
147         xObjects = commonXObjects;
148         resources.setXObjects(xObjects);
149         resources.update(); // Fundamental to override original resources collection.
150
}
151       if(!xObjects.containsKey(watermarkName))
152       {
153         xObjects.put(watermarkName,watermark);
154         xObjects.update(); // Fundamental to override original xobjects collection.
155
}
156
157       // 2.2. Insert the background content into the page.
158
// Get page's contents!
159
ContentStreams contents = page.getContents();
160       // Insert the background content under all the preexisting ones!
161
contents.add(0,backgroundContent);
162       contents.update(); // Fundamental to override original contents collection.
163
}
164     // Show the watermark!
165
ContentBuilder contentBuilder = new ContentBuilder(backgroundContent,new Buffer());
166     contentBuilder.showXObject(watermarkName);
167     backgroundContent.append(contentBuilder.getBuffer());
168
169     // 3. Serialization.
170
loader.serialize(file,this.getClass().getSimpleName());
171   }
172 }
Popular Tags