分享

JAVA实现PDF范围打印 | 邹颢的博客

 WindySky 2016-08-03
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.cloud.print;
import java.awt.print.PrinterJob;
import java.io.File;
import javax.print.PrintService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageable;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.PageRanges;
public class PDFPrint {
private static final String PASSWORD     = "-password";
    private static final String SILENT       = "-silentPrint";
    private static final String PRINTER_NAME = "-printerName";
    /**
     * private constructor.
    */
    private PDFPrint()
    {
        //static class
    }
    /**
     * Infamous main method.
     *
     * @param args Command line arguments, should be one and a reference to a file.
     *
     * @throws Exception If there is an error parsing the document.
     */
    public static void main( String[] args,Integer pageStart,Integer pageEnd) throws Exception
    {
        String password = "";
        String pdfFile = null;
        boolean silentPrint = false;
        String printerName = null;
        for( int i=0; i<args.length; i++ )
        {
            if( args[i].equals( PASSWORD ) )
            {
                i++;
                if( i >= args.length )
                {
                    usage();
                }
                password = args[i];
            }
            else if( args[i].equals( PRINTER_NAME ) )
            {
                i++;
                if( i >= args.length )
                {
                    usage();
                }
                printerName = args[i];
            }
            else if( args[i].equals( SILENT ) )
            {
                silentPrint = true;
            }
            else
            {
                pdfFile = args[i];
            }
        }
        if( pdfFile == null )
        {
            usage();
        }
        PDDocument document = null;
        try
        {
            document = PDDocument.load( pdfFile );
            if( document.isEncrypted() )
            {
                document.decrypt( password );
            }
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setJobName(new File(pdfFile).getName());
            if(printerName != null )
            {
                PrintService[] printService = PrinterJob.lookupPrintServices();
                boolean printerFound = false;
                for(int i = 0; !printerFound && i < printService.length; i++)
                {
                    if(printService[i].getName().indexOf(printerName) != -1)
                    {
                        printJob.setPrintService(printService[i]);
                        //printJob.setCopies(2);
                        printerFound = true;
                    }
                }
            }
            printJob.setPageable(new PDPageable(document, printJob));
            if( silentPrint || printJob.printDialog())
            {
             PrintRequestAttributeSet p=new HashPrintRequestAttributeSet();
             if(pageStart==null&&pageEnd!=null){
             p.add(new PageRanges(1,pageEnd));
             }else if(pageStart!=null&&pageEnd!=null){
             p.add(new PageRanges(pageStart,pageEnd));
             }
                printJob.print(p);
            }
        }
        finally
        {
            if( document != null )
            {
                document.close();
            }
        }
    }
    /**
     * This will print the usage requirements and exit.
     */
    private static void usage()
    {
        System.err.println( "Usage: java -jar pdfbox-app-x.y.z.jar PrintPDF [OPTIONS] <PDF file>\n" +
            "  -password  <password>        Password to decrypt document\n" +
            "  -silentPrint                 Print without prompting for printer info\n"
            );
        System.exit( 1 );
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多