KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > PrintPDF


1 /**
2  * Copyright (c) 2005, 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 org.pdfbox.pdmodel.PDDocument;
34
35 /**
36  * This is a command line program that will print a PDF document.
37  *
38  * @author <a HREF="ben@benlitchfield.com">Ben Litchfield</a>
39  * @version $Revision: 1.3 $
40  */

41 public class PrintPDF
42 {
43
44     private static final String JavaDoc PASSWORD = "-password";
45     private static final String JavaDoc SILENT = "-silentPrint";
46
47     /**
48      * private constructor.
49     */

50     private PrintPDF()
51     {
52         //static class
53
}
54
55     /**
56      * Infamous main method.
57      *
58      * @param args Command line arguments, should be one and a reference to a file.
59      *
60      * @throws Exception If there is an error parsing the document.
61      */

62     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
63     {
64         String JavaDoc password = "";
65         String JavaDoc pdfFile = null;
66         boolean silentPrint = false;
67         for( int i=0; i<args.length; i++ )
68         {
69             if( args[i].equals( PASSWORD ) )
70             {
71                 i++;
72                 if( i >= args.length )
73                 {
74                     usage();
75                 }
76                 password = args[i];
77             }
78             else if( args[i].equals( SILENT ) )
79             {
80                 silentPrint = true;
81             }
82             else
83             {
84                 pdfFile = args[i];
85             }
86         }
87
88         if( pdfFile == null )
89         {
90             usage();
91         }
92
93         PDDocument document = null;
94         try
95         {
96             document = PDDocument.load( pdfFile );
97
98             if( document.isEncrypted() )
99             {
100                 document.decrypt( password );
101             }
102             if( silentPrint )
103             {
104                 document.silentPrint();
105             }
106             else
107             {
108                 document.print();
109             }
110         }
111         finally
112         {
113             if( document != null )
114             {
115                 document.close();
116             }
117         }
118     }
119
120     /**
121      * This will print the usage requirements and exit.
122      */

123     private static void usage()
124     {
125         System.err.println( "Usage: java org.pdfbox.PrintPDF [OPTIONS] <PDF file>\n" +
126             " -password <password> Password to decrypt document\n" +
127             " -silentPrint Print without prompting for printer info\n"
128             );
129         System.exit( 1 );
130     }
131 }
Popular Tags