KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleSVG2PDF


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: ExampleSVG2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding;
21
22 //Java
23
import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27
28 //Batik
29
import org.apache.batik.transcoder.Transcoder;
30 import org.apache.batik.transcoder.TranscoderException;
31 import org.apache.batik.transcoder.TranscoderInput;
32 import org.apache.batik.transcoder.TranscoderOutput;
33
34 //FOP
35
import org.apache.fop.svg.PDFTranscoder;
36
37 /**
38  * This class demonstrates the conversion of an SVG file to PDF using FOP.
39  */

40 public class ExampleSVG2PDF {
41
42     /**
43      * Converts an FO file to a PDF file using FOP
44      * @param svg the SVG file
45      * @param pdf the target PDF file
46      * @throws IOException In case of an I/O problem
47      * @throws TranscoderException In case of a transcoding problem
48      */

49     public void convertSVG2PDF(File JavaDoc svg, File JavaDoc pdf) throws IOException JavaDoc, TranscoderException {
50         
51         //Create transcoder
52
Transcoder transcoder = new PDFTranscoder();
53         //Transcoder transcoder = new org.apache.fop.render.ps.PSTranscoder();
54

55         //Setup input
56
InputStream JavaDoc in = new java.io.FileInputStream JavaDoc(svg);
57         try {
58             TranscoderInput input = new TranscoderInput(in);
59             
60             //Setup output
61
OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(pdf);
62             out = new java.io.BufferedOutputStream JavaDoc(out);
63             try {
64                 TranscoderOutput output = new TranscoderOutput(out);
65                 
66                 //Do the transformation
67
transcoder.transcode(input, output);
68             } finally {
69                 out.close();
70             }
71         } finally {
72             in.close();
73         }
74     }
75
76
77     /**
78      * Main method.
79      * @param args command-line arguments
80      */

81     public static void main(String JavaDoc[] args) {
82         try {
83             System.out.println("FOP ExampleSVG2PDF\n");
84             System.out.println("Preparing...");
85             
86             //Setup directories
87
File JavaDoc baseDir = new File JavaDoc(".");
88             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
89             outDir.mkdirs();
90
91             //Setup input and output files
92
File JavaDoc svgfile = new File JavaDoc(baseDir, "xml/svg/helloworld.svg");
93             File JavaDoc pdffile = new File JavaDoc(outDir, "ResultSVG2PDF.pdf");
94
95             System.out.println("Input: SVG (" + svgfile + ")");
96             System.out.println("Output: PDF (" + pdffile + ")");
97             System.out.println();
98             System.out.println("Transforming...");
99             
100             ExampleSVG2PDF app = new ExampleSVG2PDF();
101             app.convertSVG2PDF(svgfile, pdffile);
102             
103             System.out.println("Success!");
104         } catch (Exception JavaDoc e) {
105             e.printStackTrace(System.err);
106             System.exit(-1);
107         }
108     }
109 }
110
Popular Tags