KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > pdmodel > ImageToPDF


1 /**
2  * Copyright (c) 2005-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.examples.pdmodel;
32
33 import java.io.File JavaDoc;
34 import java.io.FileInputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import org.pdfbox.exceptions.COSVisitorException;
38 import org.pdfbox.io.RandomAccessFile;
39
40 import org.pdfbox.pdmodel.PDDocument;
41 import org.pdfbox.pdmodel.PDPage;
42
43 import org.pdfbox.pdmodel.edit.PDPageContentStream;
44
45 import org.pdfbox.pdmodel.graphics.xobject.PDCcitt;
46 import org.pdfbox.pdmodel.graphics.xobject.PDJpeg;
47 import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
48
49
50 /**
51  * This is an example that creates a simple document.
52  *
53  * The example is taken from the pdf file format specification.
54  *
55  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
56  * @version $Revision: 1.7 $
57  */

58 public class ImageToPDF
59 {
60
61     /**
62      * create the second sample document from the PDF file format specification.
63      *
64      * @param file The file to write the PDF to.
65      * @param image The filename of the image to put in the PDF.
66      *
67      * @throws IOException If there is an error writing the data.
68      * @throws COSVisitorException If there is an error writing the PDF.
69      */

70     public void createPDFFromImage( String JavaDoc file, String JavaDoc image) throws IOException JavaDoc, COSVisitorException
71     {
72         // the document
73
PDDocument doc = null;
74         try
75         {
76             doc = new PDDocument();
77             
78             PDPage page = new PDPage();
79             doc.addPage( page );
80             
81             PDXObjectImage ximage = null;
82             if( image.toLowerCase().endsWith( ".jpg" ) )
83             {
84                 ximage = new PDJpeg(doc, new FileInputStream JavaDoc( image ) );
85             }
86             else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
87             {
88                 ximage = new PDCcitt(doc, new RandomAccessFile(new File JavaDoc(image),"r"));
89             }
90             else
91             {
92                 //BufferedImage awtImage = ImageIO.read( new File( image ) );
93
//ximage = new PDPixelMap(doc, awtImage);
94
throw new IOException JavaDoc( "Image type not supported:" + image );
95             }
96             PDPageContentStream contentStream = new PDPageContentStream(doc, page);
97             
98             contentStream.drawImage( ximage, 20, 20 );
99           
100             contentStream.close();
101             doc.save( file );
102         }
103         finally
104         {
105             if( doc != null )
106             {
107                 doc.close();
108             }
109         }
110     }
111
112     /**
113      * This will create a PDF document with a single image on it.
114      * <br />
115      * see usage() for commandline
116      *
117      * @param args Command line arguments.
118      */

119     public static void main(String JavaDoc[] args)
120     {
121         ImageToPDF app = new ImageToPDF();
122         try
123         {
124             if( args.length != 2 )
125             {
126                 app.usage();
127             }
128             else
129             {
130                 app.createPDFFromImage( args[0], args[1] );
131             }
132         }
133         catch (Exception JavaDoc e)
134         {
135             e.printStackTrace();
136         }
137     }
138
139     /**
140      * This will print out a message telling how to use this example.
141      */

142     private void usage()
143     {
144         System.err.println( "usage: " + this.getClass().getName() + " <output-file> <image>" );
145     }
146 }
Popular Tags