Source Code Cross Referenced for WatermarkSample.java in  » PDF » PDFClown-0.0.5 » it » stefanochizzolini » clown » samples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » PDFClown 0.0.5 » it.stefanochizzolini.clown.samples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.