KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > PDFToImage


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;
32
33 import java.awt.image.BufferedImage JavaDoc;
34 import java.io.File JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 import javax.imageio.IIOException JavaDoc;
39 import javax.imageio.IIOImage JavaDoc;
40 import javax.imageio.ImageIO JavaDoc;
41 import javax.imageio.ImageWriteParam JavaDoc;
42 import javax.imageio.ImageWriter JavaDoc;
43 import javax.imageio.stream.ImageOutputStream JavaDoc;
44
45 import org.pdfbox.exceptions.InvalidPasswordException;
46
47 import org.pdfbox.pdmodel.PDDocument;
48 import org.pdfbox.pdmodel.PDPage;
49
50 /**
51  * Convert a PDF document to an image.
52  *
53  * @author <a HREF="ben@benlitchfield.com">Ben Litchfield</a>
54  * @version $Revision: 1.5 $
55  */

56 public class PDFToImage
57 {
58
59     private static final String JavaDoc PASSWORD = "-password";
60     private static final String JavaDoc START_PAGE = "-startPage";
61     private static final String JavaDoc END_PAGE = "-endPage";
62     private static final String JavaDoc IMAGE_TYPE = "-imageType";
63     private static final String JavaDoc OUTPUT_PREFIX = "-outputPrefix";
64
65     /**
66      * private constructor.
67     */

68     private PDFToImage()
69     {
70         //static class
71
}
72
73     /**
74      * Infamous main method.
75      *
76      * @param args Command line arguments, should be one and a reference to a file.
77      *
78      * @throws Exception If there is an error parsing the document.
79      */

80     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
81     {
82         String JavaDoc password = "";
83         String JavaDoc pdfFile = null;
84         String JavaDoc outputPrefix = null;
85         String JavaDoc imageType = "jpg";
86         int startPage = 1;
87         int endPage = Integer.MAX_VALUE;
88         for( int i=0; i<args.length; i++ )
89         {
90             if( args[i].equals( PASSWORD ) )
91             {
92                 i++;
93                 if( i >= args.length )
94                 {
95                     usage();
96                 }
97                 password = args[i];
98             }
99             else if( args[i].equals( START_PAGE ) )
100             {
101                 i++;
102                 if( i >= args.length )
103                 {
104                     usage();
105                 }
106                 startPage = Integer.parseInt( args[i] );
107             }
108             else if( args[i].equals( END_PAGE ) )
109             {
110                 i++;
111                 if( i >= args.length )
112                 {
113                     usage();
114                 }
115                 endPage = Integer.parseInt( args[i] );
116             }
117             else if( args[i].equals( IMAGE_TYPE ) )
118             {
119                 i++;
120                 imageType = args[i];
121             }
122             else if( args[i].equals( OUTPUT_PREFIX ) )
123             {
124                 i++;
125                 outputPrefix = args[i];
126             }
127             else
128             {
129                 if( pdfFile == null )
130                 {
131                     pdfFile = args[i];
132                 }
133             }
134         }
135
136         if( pdfFile == null )
137         {
138             usage();
139         }
140         else
141         {
142             if(outputPrefix == null)
143             {
144                 outputPrefix = pdfFile.substring( 0, pdfFile.lastIndexOf( '.' ));
145             }
146     
147             PDDocument document = null;
148             try
149             {
150                 document = PDDocument.load( pdfFile );
151     
152     
153                 //document.print();
154
if( document.isEncrypted() )
155                 {
156                     try
157                     {
158                         document.decrypt( password );
159                     }
160                     catch( InvalidPasswordException e )
161                     {
162                         if( args.length == 4 )//they supplied the wrong password
163
{
164                             System.err.println( "Error: The supplied password is incorrect." );
165                             System.exit( 2 );
166                         }
167                         else
168                         {
169                             //they didn't suppply a password and the default of "" was wrong.
170
System.err.println( "Error: The document is encrypted." );
171                             usage();
172                         }
173                     }
174                 }
175                 List JavaDoc pages = document.getDocumentCatalog().getAllPages();
176                 for( int i=startPage-1; i<endPage && i<pages.size(); i++ )
177                 {
178                     ImageOutputStream JavaDoc output = null;
179                     ImageWriter JavaDoc imageWriter = null;
180                     try
181                     {
182                         PDPage page = (PDPage)pages.get( i );
183                         BufferedImage JavaDoc image = page.convertToImage();
184                         String JavaDoc fileName = outputPrefix + (i+1) + "." + imageType;
185                         System.out.println( "Writing:" + fileName );
186                         output = ImageIO.createImageOutputStream( new File JavaDoc( fileName ) );
187                         
188                         boolean foundWriter = false;
189                         Iterator JavaDoc writerIter = ImageIO.getImageWritersByFormatName( imageType );
190                         while( writerIter.hasNext() && !foundWriter )
191                         {
192                             try
193                             {
194                                 imageWriter = (ImageWriter JavaDoc)writerIter.next();
195                                 ImageWriteParam JavaDoc writerParams = imageWriter.getDefaultWriteParam();
196                                 if(writerParams.canWriteCompressed() )
197                                 {
198                                     writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
199                                     writerParams.setCompressionQuality(1.0f);
200                                 }
201                                 
202                                 
203                                 imageWriter.setOutput( output );
204                                 imageWriter.write( null, new IIOImage JavaDoc( image, null, null), writerParams );
205                                 foundWriter = true;
206                             }
207                             catch( IIOException JavaDoc io )
208                             {
209                                 //ignore exception
210
}
211                             finally
212                             {
213                                 if( imageWriter != null )
214                                 {
215                                     imageWriter.dispose();
216                                 }
217                             }
218                         }
219                         if( !foundWriter )
220                         {
221                             throw new RuntimeException JavaDoc( "Error: no writer found for image type '" + imageType + "'" );
222                         }
223                     }
224                     finally
225                     {
226                         if( output != null )
227                         {
228                             output.flush();
229                             output.close();
230                         }
231                     }
232                 }
233             }
234             finally
235             {
236                 if( document != null )
237                 {
238                     document.close();
239                 }
240             }
241         }
242     }
243
244     /**
245      * This will print the usage requirements and exit.
246      */

247     private static void usage()
248     {
249         System.err.println( "Usage: java org.pdfbox.PDFToImage [OPTIONS] <PDF file>\n" +
250             " -password <password> Password to decrypt document\n" +
251             " -imageType <image type> (" + getImageFormats() + ")\n" +
252             " -outputPrefix <output prefix> Filename prefix for image files\n" +
253             " -startPage <number> The first page to start extraction(1 based)\n" +
254             " -endPage <number> The last page to extract(inclusive)\n" +
255             " <PDF file> The PDF document to use\n"
256             );
257         System.exit( 1 );
258     }
259     
260     private static String JavaDoc getImageFormats()
261     {
262         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
263         String JavaDoc[] formats = ImageIO.getReaderFormatNames();
264         for( int i=0; i<formats.length; i++ )
265         {
266             retval.append( formats[i] );
267             if( i+1<formats.length )
268             {
269                 retval.append( "," );
270             }
271         }
272         return retval.toString();
273     }
274 }
Popular Tags