001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox;
031:
032: import org.pdfbox.pdmodel.PDDocument;
033:
034: /**
035: * This is a command line program that will print a PDF document.
036: *
037: * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
038: * @version $Revision: 1.3 $
039: */
040: public class PrintPDF {
041:
042: private static final String PASSWORD = "-password";
043: private static final String SILENT = "-silentPrint";
044:
045: /**
046: * private constructor.
047: */
048: private PrintPDF() {
049: //static class
050: }
051:
052: /**
053: * Infamous main method.
054: *
055: * @param args Command line arguments, should be one and a reference to a file.
056: *
057: * @throws Exception If there is an error parsing the document.
058: */
059: public static void main(String[] args) throws Exception {
060: String password = "";
061: String pdfFile = null;
062: boolean silentPrint = false;
063: for (int i = 0; i < args.length; i++) {
064: if (args[i].equals(PASSWORD)) {
065: i++;
066: if (i >= args.length) {
067: usage();
068: }
069: password = args[i];
070: } else if (args[i].equals(SILENT)) {
071: silentPrint = true;
072: } else {
073: pdfFile = args[i];
074: }
075: }
076:
077: if (pdfFile == null) {
078: usage();
079: }
080:
081: PDDocument document = null;
082: try {
083: document = PDDocument.load(pdfFile);
084:
085: if (document.isEncrypted()) {
086: document.decrypt(password);
087: }
088: if (silentPrint) {
089: document.silentPrint();
090: } else {
091: document.print();
092: }
093: } finally {
094: if (document != null) {
095: document.close();
096: }
097: }
098: }
099:
100: /**
101: * This will print the usage requirements and exit.
102: */
103: private static void usage() {
104: System.err
105: .println("Usage: java org.pdfbox.PrintPDF [OPTIONS] <PDF file>\n"
106: + " -password <password> Password to decrypt document\n"
107: + " -silentPrint Print without prompting for printer info\n");
108: System.exit(1);
109: }
110: }
|