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
| public class PrintService {
private static final Logger logger = LoggerFactory.getLogger(PrintService.class);
public void print(InputStream in,boolean isDuplex,int copies,boolean isPortrait) throws Exception { if(in == null) { throw new TXException("文件流为空"); } if(copies <= 0) { throw new TXException("打印份数不应小于0"); } logger.info("打印文件: 是否双页打印: "+isDuplex+",份数: "+copies+"是否竖打"+isPortrait); try(PDDocument document = PDDocument.load(in);) { Book book =new PDFPageable(document); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(book); HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet(); pars.add(MediaName.ISO_A4_WHITE); if(isDuplex) { if(isPortrait){ pars.add(Sides.DUPLEX); } else { pars.add(Sides.TWO_SIDED_LONG_EDGE); } } if(isPortrait){ pars.add(OrientationRequested.PORTRAIT); } else { pars.add(OrientationRequested.LANDSCAPE); } for(int i=0;i<copies;i++) { job.print(pars); } } catch (Exception e) { throw e; } } public static void main(String[] args) throws Exception { InputStream in = new FileInputStream("D:/test.pdf"); new PrintService().print(in, false,1,false); in.close(); } }
|