Source Code Cross Referenced for HelloWorld.java in  » PDF » PDFClown-0.0.5 » it » stefanochizzolini » clown » samples » servlets » 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.servlets 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package it.stefanochizzolini.clown.samples.servlets;
002:
003:        import it.stefanochizzolini.clown.bytes.Buffer;
004:        import it.stefanochizzolini.clown.documents.Document;
005:        import it.stefanochizzolini.clown.documents.Page;
006:        import it.stefanochizzolini.clown.documents.PageFormat;
007:        import it.stefanochizzolini.clown.documents.contents.Fonts;
008:        import it.stefanochizzolini.clown.documents.contents.colorSpaces.DeviceRGBColor;
009:        import it.stefanochizzolini.clown.documents.contents.composition.BlockFilter;
010:        import it.stefanochizzolini.clown.documents.contents.composition.PrimitiveFilter;
011:        import it.stefanochizzolini.clown.documents.contents.composition.AlignmentXEnum;
012:        import it.stefanochizzolini.clown.documents.contents.composition.AlignmentYEnum;
013:        import it.stefanochizzolini.clown.documents.contents.entities.Image;
014:        import it.stefanochizzolini.clown.documents.contents.fonts.StandardType1Font;
015:        import it.stefanochizzolini.clown.documents.contents.xObjects.XObject;
016:        import it.stefanochizzolini.clown.documents.interchange.metadata.Information;
017:        import it.stefanochizzolini.clown.files.File;
018:        import it.stefanochizzolini.clown.files.SerializationModeEnum;
019:
020:        import it.stefanochizzolini.clown.samples.SampleHelper;
021:
022:        import java.awt.Dimension;
023:        import java.awt.geom.Dimension2D;
024:        import java.awt.geom.Point2D;
025:        import java.awt.geom.Rectangle2D;
026:        import java.io.IOException;
027:        import java.util.Date;
028:        import java.util.List;
029:
030:        import javax.servlet.ServletException;
031:        import javax.servlet.ServletOutputStream;
032:        import javax.servlet.http.HttpServlet;
033:        import javax.servlet.http.HttpServletRequest;
034:        import javax.servlet.http.HttpServletResponse;
035:
036:        import org.apache.commons.fileupload.FileItem;
037:        import org.apache.commons.fileupload.FileItemFactory;
038:        import org.apache.commons.fileupload.FileItemIterator;
039:        import org.apache.commons.fileupload.disk.DiskFileItemFactory;
040:        import org.apache.commons.fileupload.servlet.ServletFileUpload;
041:
042:        /**
043:         This sample serves a PDF file dynamically generated combining user inputs (image and text contents).
044:         */
045:        public class HelloWorld extends HttpServlet {
046:            // <dynamic>
047:            // <interface>
048:            // <public>
049:            public void doPost(HttpServletRequest request,
050:                    HttpServletResponse response) throws IOException,
051:                    ServletException {
052:                if (!ServletFileUpload.isMultipartContent(request))
053:                    return;
054:
055:                // 1. Parsing the form fields...
056:                FileItem item = null;
057:                String comment = null;
058:                FileItem imageFileFormField = null;
059:                try {
060:                    FileItemFactory factory = new DiskFileItemFactory();
061:                    ServletFileUpload upload = new ServletFileUpload(factory);
062:                    List items = upload.parseRequest(request);
063:                    java.util.Iterator iter = items.iterator();
064:                    while (iter.hasNext()) {
065:                        item = (FileItem) iter.next();
066:                        if (item.getFieldName().equals("file")) {
067:                            imageFileFormField = item;
068:                        }
069:                        if (item.getFieldName().equals("comment")) {
070:                            comment = item.getString();
071:                        }
072:                    }
073:                } catch (Exception e) {
074:                    System.err
075:                            .println("An exception happened while parsing the form fields: "
076:                                    + e.getMessage());
077:                    e.printStackTrace();
078:                    return;
079:                }
080:
081:                // 2. Building the document...
082:                File file = null;
083:                try {
084:                    // Instantiate a new PDF document!
085:                    file = new File();
086:                    Document document = file.getDocument();
087:
088:                    buildPdf_page(document, imageFileFormField, comment);
089:
090:                    buildPdf_metadata(document);
091:                } catch (Exception e) {
092:                    System.err
093:                            .println("An exception happened while building the sample: "
094:                                    + e.getMessage());
095:                    e.printStackTrace();
096:                    return;
097:                }
098:
099:                // 3. Serializing the document...
100:                try {
101:                    file.writeTo(
102:                            new it.stefanochizzolini.clown.bytes.OutputStream(
103:                                    response.getOutputStream()),
104:                            SerializationModeEnum.Standard);
105:                    response.setContentType("application/pdf");
106:                } catch (Exception e) {
107:                    System.err
108:                            .println("An exception happened while serializing the sample: "
109:                                    + e.getMessage());
110:                    e.printStackTrace();
111:                    return;
112:                }
113:            }
114:
115:            // </public>
116:
117:            // <private>
118:            private void buildPdf_metadata(Document document) {
119:                // Document metadata.
120:                Information info = new Information(document);
121:                document.setInformation(info);
122:                info.setAuthor("Stefano Chizzolini");
123:                info.setCreationDate(new Date());
124:                info.setCreator(HelloWorld.class.getName());
125:                info.setTitle("Sample document");
126:                info.setSubject("Online PDF creation sample through servlet.");
127:            }
128:
129:            private void buildPdf_page(Document document,
130:                    FileItem imageFileFormField, String comment) {
131:                // Set default page size (A4)!
132:                document.setPageSize(PageFormat.getSize());
133:
134:                // Add page!
135:                Page page = new Page(document);
136:                document.getPages().add(page);
137:                Dimension2D pageSize = page.getSize();
138:
139:                PrimitiveFilter builder = new PrimitiveFilter(page);
140:                // Add the background template!
141:                builder.showXObject(SampleHelper.createTemplate(document));
142:
143:                // Wrap the content builder inside a block filter in order to achieve higher-level typographic control!
144:                /*
145:                  NOTE: BlockFilter is a new class of PDF Clown since version 0.0.3.
146:                  It provides block-level typographic features as text and paragraph alignment.
147:                  Flow-level typographic features are currently not supported: block-level typographic features
148:                  are the foundations upon which flow-level typographic features will sit.
149:                 */
150:                BlockFilter blockFilter = new BlockFilter(builder);
151:                blockFilter.setHyphenation(true);
152:
153:                Rectangle2D.Double frame = new Rectangle2D.Double(30, 150,
154:                        pageSize.getWidth() - 110, pageSize.getHeight() - 250);
155:
156:                blockFilter.begin(frame, AlignmentXEnum.Left,
157:                        AlignmentYEnum.Top);
158:                StandardType1Font titleFont = new StandardType1Font(document,
159:                        StandardType1Font.FamilyNameEnum.Times, true, false);
160:                builder.setFont(titleFont, 48);
161:                blockFilter.showText("Welcome");
162:                blockFilter.showBreak();
163:                StandardType1Font bodyFont = new StandardType1Font(document,
164:                        StandardType1Font.FamilyNameEnum.Times, false, false);
165:                builder.setFont(bodyFont, 16);
166:                blockFilter
167:                        .showText("This is an on-the-fly servlet-driven PDF sample document generated by PDF Clown for Java(TM) aka PDF Jester.");
168:                blockFilter.end();
169:
170:                // Move past the closed block!
171:                frame.y = blockFilter.getBoundBox().getMaxY() + 30;
172:                frame.height -= (blockFilter.getBoundBox().getHeight() + 30);
173:
174:                // Showing the posted image...
175:                // Instantiate a jpeg image object!
176:                Image image = null;
177:                try {
178:                    image = Image.get(new Buffer(imageFileFormField.get())); // Abstract image (entity).
179:                } catch (Exception e) {/* NOOP. */
180:                }
181:                if (image == null) {
182:                    blockFilter.begin(frame, AlignmentXEnum.Left,
183:                            AlignmentYEnum.Top);
184:                    builder.setFont(bodyFont, 12);
185:                    builder.setFillColor(new DeviceRGBColor(1, 0, 0));
186:                    blockFilter
187:                            .showText("The file you uploaded wasn't a valid JPEG image!");
188:                    blockFilter.end();
189:
190:                    // Move past the closed block!
191:                    frame.y = blockFilter.getBoundBox().getMaxY() + 20;
192:                    frame.height -= (blockFilter.getBoundBox().getHeight() + 20);
193:                } else {
194:                    blockFilter.begin(frame, AlignmentXEnum.Left,
195:                            AlignmentYEnum.Top);
196:                    builder.setFont(bodyFont, 12);
197:                    blockFilter.showText("Here it is the image you uploaded: ");
198:                    blockFilter.end();
199:
200:                    // Move past the closed block!
201:                    frame.y = blockFilter.getBoundBox().getMaxY() + 20;
202:                    frame.height -= (blockFilter.getBoundBox().getHeight() + 20);
203:
204:                    double width = image.getWidth(), height = image.getHeight();
205:                    if (width > frame.getWidth()) {
206:                        height *= frame.getWidth() / width;
207:                        width = frame.getWidth();
208:                    }
209:                    if (height > frame.getHeight() / 2) {
210:                        width *= frame.getHeight() / 2 / height;
211:                        height = frame.getHeight() / 2;
212:                    }
213:                    // Show the image!
214:                    builder
215:                            .showXObject(
216:                                    image.toXObject(document),
217:                                    new Point2D.Double(
218:                                            (pageSize.getWidth() - 90 - width) / 2 + 20,
219:                                            blockFilter.getBoundBox().getMaxY() + 20),
220:                                    new Dimension((int) width, (int) height));
221:                    // Move past the image closed block!
222:                    frame.x = (pageSize.getWidth() - 90 - width) / 2 + 20;
223:                    frame.y += (height + 7);
224:                    frame.height -= (height + 7);
225:                    frame.width = width;
226:                }
227:
228:                if (comment != null) {
229:                    blockFilter.begin(frame, AlignmentXEnum.Justify,
230:                            AlignmentYEnum.Top);
231:                    builder.setFont(new StandardType1Font(document,
232:                            StandardType1Font.FamilyNameEnum.Courier, false,
233:                            false), 7);
234:                    blockFilter.showText(comment);
235:                    blockFilter.end();
236:                }
237:
238:                builder.flush();
239:            }
240:            // </private>
241:            // </interface>
242:            // </dynamic>
243:            // </class>
244:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.