Source Code Cross Referenced for ExampleDOM2PDF.java in  » Graphic-Library » fop » embedding » 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 » Graphic Library » fop » embedding 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        /* $Id: ExampleDOM2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020:        package embedding;
021:
022:        // Java
023:        import java.io.File;
024:        import java.io.OutputStream;
025:        import javax.xml.parsers.DocumentBuilderFactory;
026:        import javax.xml.parsers.DocumentBuilder;
027:        import javax.xml.parsers.ParserConfigurationException;
028:
029:        //JAXP
030:        import javax.xml.transform.Transformer;
031:        import javax.xml.transform.TransformerFactory;
032:        import javax.xml.transform.Source;
033:        import javax.xml.transform.Result;
034:        import javax.xml.transform.dom.DOMSource;
035:        import javax.xml.transform.sax.SAXResult;
036:
037:        // DOM
038:        import org.w3c.dom.Document;
039:        import org.w3c.dom.Element;
040:        import org.w3c.dom.Node;
041:        import org.w3c.dom.Text;
042:
043:        // FOP
044:        import org.apache.fop.apps.FOUserAgent;
045:        import org.apache.fop.apps.Fop;
046:        import org.apache.fop.apps.FopFactory;
047:        import org.apache.fop.apps.MimeConstants;
048:
049:        /**
050:         * This class demonstrates the conversion of a DOM Document to PDF
051:         * using JAXP (XSLT) and FOP (XSL-FO).
052:         */
053:        public class ExampleDOM2PDF {
054:
055:            // configure fopFactory as desired
056:            private FopFactory fopFactory = FopFactory.newInstance();
057:
058:            /** xsl-fo namespace URI */
059:            protected static String foNS = "http://www.w3.org/1999/XSL/Format";
060:
061:            /**
062:             * Converts a DOM Document to a PDF file using FOP.
063:             * @param xslfoDoc the DOM Document
064:             * @param pdf the target PDF file
065:             */
066:            public void convertDOM2PDF(Document xslfoDoc, File pdf) {
067:                try {
068:                    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
069:                    // configure foUserAgent as desired
070:
071:                    // Setup output
072:                    OutputStream out = new java.io.FileOutputStream(pdf);
073:                    out = new java.io.BufferedOutputStream(out);
074:
075:                    try {
076:                        // Construct fop with desired output format and output stream
077:                        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
078:                                foUserAgent, out);
079:
080:                        // Setup Identity Transformer
081:                        TransformerFactory factory = TransformerFactory
082:                                .newInstance();
083:                        Transformer transformer = factory.newTransformer(); // identity transformer
084:
085:                        // Setup input for XSLT transformation
086:                        Source src = new DOMSource(xslfoDoc);
087:
088:                        // Resulting SAX events (the generated FO) must be piped through to FOP
089:                        Result res = new SAXResult(fop.getDefaultHandler());
090:
091:                        // Start XSLT transformation and FOP processing
092:                        transformer.transform(src, res);
093:                    } finally {
094:                        out.close();
095:                    }
096:
097:                } catch (Exception e) {
098:                    e.printStackTrace(System.err);
099:                    System.exit(-1);
100:                }
101:
102:            }
103:
104:            /**
105:             * Main method.
106:             * @param args command-line arguments
107:             */
108:            public static void main(String[] args) {
109:                try {
110:                    System.out.println("FOP ExampleDOM2PDF\n");
111:
112:                    //Setup directories
113:                    File baseDir = new File(".");
114:                    File outDir = new File(baseDir, "out");
115:                    outDir.mkdirs();
116:
117:                    //Setup output file
118:                    File pdffile = new File(outDir, "ResultDOM2PDF.pdf");
119:                    System.out.println("PDF Output File: " + pdffile);
120:                    System.out.println();
121:
122:                    Document foDoc = buildDOMDocument();
123:
124:                    ExampleDOM2PDF app = new ExampleDOM2PDF();
125:                    app.convertDOM2PDF(foDoc, pdffile);
126:
127:                    System.out.println("Success!");
128:
129:                } catch (Exception e) {
130:                    e.printStackTrace(System.err);
131:                    System.exit(-1);
132:                }
133:            }
134:
135:            /**
136:             * Builds the example FO document as a DOM in memory.
137:             * @return the FO document
138:             * @throws ParserConfigurationException In case there is a problem creating a DOM document
139:             */
140:            private static Document buildDOMDocument()
141:                    throws ParserConfigurationException {
142:                // Create a sample XSL-FO DOM document
143:                Document foDoc = null;
144:                Element root = null, ele1 = null, ele2 = null, ele3 = null;
145:
146:                DocumentBuilderFactory dbf = DocumentBuilderFactory
147:                        .newInstance();
148:                dbf.setNamespaceAware(true);
149:                DocumentBuilder db = dbf.newDocumentBuilder();
150:                foDoc = db.newDocument();
151:
152:                root = foDoc.createElementNS(foNS, "fo:root");
153:                foDoc.appendChild(root);
154:
155:                ele1 = foDoc.createElementNS(foNS, "fo:layout-master-set");
156:                root.appendChild(ele1);
157:                ele2 = foDoc.createElementNS(foNS, "fo:simple-page-master");
158:                ele1.appendChild(ele2);
159:                ele2.setAttributeNS(null, "master-name", "letter");
160:                ele2.setAttributeNS(null, "page-height", "11in");
161:                ele2.setAttributeNS(null, "page-width", "8.5in");
162:                ele2.setAttributeNS(null, "margin-top", "1in");
163:                ele2.setAttributeNS(null, "margin-bottom", "1in");
164:                ele2.setAttributeNS(null, "margin-left", "1in");
165:                ele2.setAttributeNS(null, "margin-right", "1in");
166:                ele3 = foDoc.createElementNS(foNS, "fo:region-body");
167:                ele2.appendChild(ele3);
168:                ele1 = foDoc.createElementNS(foNS, "fo:page-sequence");
169:                root.appendChild(ele1);
170:                ele1.setAttributeNS(null, "master-reference", "letter");
171:                ele2 = foDoc.createElementNS(foNS, "fo:flow");
172:                ele1.appendChild(ele2);
173:                ele2.setAttributeNS(null, "flow-name", "xsl-region-body");
174:                addElement(ele2, "fo:block", "Hello World!");
175:                return foDoc;
176:            }
177:
178:            /**
179:             * Adds an element to the DOM.
180:             * @param parent parent node to attach the new element to
181:             * @param newNodeName name of the new node
182:             * @param textVal content of the element
183:             */
184:            protected static void addElement(Node parent, String newNodeName,
185:                    String textVal) {
186:                if (textVal == null) {
187:                    return;
188:                } // use only with text nodes
189:                Element newElement = parent.getOwnerDocument().createElementNS(
190:                        foNS, newNodeName);
191:                Text elementText = parent.getOwnerDocument().createTextNode(
192:                        textVal);
193:                newElement.appendChild(elementText);
194:                parent.appendChild(newElement);
195:            }
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.