Java 实现批量静默打印PDF文件

    对于java的打印功能我本人从来没有使用过,不过也看过网上的一些文章,都说使用起来不方便且对各种文档类型的支持不是很好。经过几番折腾,我对批量打印PDF文件有了比较深入的了解。起初想通过使用JAVA 代码操纵PDF文件打印,但最终还是失败了,没能达到最终的效果。后来了解到可以用JAVA调用第三方软件的形式进行间接打印,这其中我试过了好几款软件也都没达到要求,像福昕、gsview、adobe什么的。有的不能在批量打印时切换打印机,有的对PDF文件内嵌的中文字体支持不好,最终全部放弃。

   后来发现了一款国外软件叫pdfPrint的软件,大家可以去官网找一下,该公司有很多有关PDF 文件操作的软件,很好用,但都是收费使用。下面说说pdfPrint的使用方法吧。

   该软件是通过命令行的方式打印PDF文件的,打印的方式都是通过命令行参数的方式进行配置的,参数有很多,相当的方便。包括边界、份数、起始页、缩放、位置方向等应有尽有。基本可以满足大部分对于PDF文件的打印需求。当然我的打印问题就是有它完美解决的。下面就我在使用过程中遇到的难点重点解释一下:

   Usage: pdfprint.exe [options] [Options] 
  -firstpage              : first page to print, from 1 to max page
  -lastpage               : last page to print, from 1 to max page
  -prompt                      : prompt the user with the print dialog so they can change printer settings and/or select pages manually
  -printer             : printer name to print

   指定打印机,打印机名称中如果有空格,需用""引起来。
  -savedevmode         : prompt the user with print dialog and save the printer settings into a disk file
  -loaddevmode         : restore printer settings which saved by -savedevmode parameter
  -promptdevmode               : prompt the user with print dialog and print the printer settings to screen
  -devmode             : restore printer settings which outputed by -promptdevmode parameter
  -copies                 : set number of copies to print
  -setcopyto                   : set '-copies' value to printer instead of print it several times
  -paper               : paper size to printer
    pdf    : retrieve paper size from PDF file
    number : standard paper size
    others : user defined paper size

   打印机纸张类型,一般A4 = 9,A3=8 
  -scalex                 : X axis scale factors, default is 100
    100 : do not scale PDF page
      0 : scale the width of PDF page to fit the printer's paper size
     -1 : keep the minimum constant scaling in both paper width and height
  -scaley                 : Y axis scale factors, default is 100
    100 : do not scale PDF page
      0 : scale the height of PDF page to fit the printer's paper size
     -1 : keep the minimum constant scaling in both paper width and height
  -pdforient              : select the orientation of the show PDF page,
    0 : upright, default
    1 : upside down
    2 : left side down
    3 : right side down
    4 : auto detect orientation for PDF pages
  -orient                 : select the orientation of the printer paper,
    1 is portrait (default) and 2 is landscape

    纸张打印方向,有些打印机不能自动判别文档方向,需加此参数,否则文档打印不完整。

  -duplex                 : select duplex or double-sided printing for printers capable of duplex printing,
    1 : simplex
    2 : horizontal
    3 : vertical
  -color                  : specify color or monochrome to printer,
    1 : monochrome
    2 : color
  -xres                   : specify the printer x-resolution,
    -4    : high
    -3    : medium
    -2    : low
    -1    : draft
    number: the number of dots per inch (DPI) and is therefore device dependent
  -yres                   : specify the printer y-resolution
  -xoffset                : specify a page offset in the horizontal axis
  -yoffset                : specify a page offset in the vertical axis
  -collate                : specify whether collation should be used when
     printing multiple copies, 0 is disable and 1 is enable
  -scale                  : specify the factor by which the printed output
     is to be scaled, The apparent page size is scaled from the physical page
     size by a factor of scale/100.
  -shell                       : Call default PDF viewer to print PDF file
  -shell2                      : Call default PDF viewer to print PDF file
  -shelltime              : set timeout for shell printing,in milliseconds
  -raster                      : render PDF page to image before printing
  -raster2                     : render PDF page to image before printing
  -rasterbitcount         : set bitcount for raster image, e.g., 1, 8, 24
  -rasterbwtext                : disable halftone for color text, for -raster2 only
  -preproc                     : process PDF file before printing, useful for some damaged PDF files
  -printtofile         : save print spooling data to a disk file
  -mergeprintjobs              : combine all print jobs into one print job
  -listprinter                 : list printers in system
  -listbins                    : list bins/trays of a printer
  -listjobs                    : list print jobs in printer's queue
  -listall                     : list printers, ports, monitors etc.
  -chgbin                 : change bin/tray for printer by number
  -papersource         : change bin/tray for printer by name

   打印机纸盒
  -winfont                     : use Windows fonts to instead of embedded fonts
  -winfont2                    : force to use Windows fonts to instead of all embedded fonts
  -useembedfont                : use embedded fonts from PDF file
  -useunicode             : enable or disable unicode conversion for PDF printing
  -antifonts                   : enable anti-aliased font bitmaps for emulated fonts
  -nochgprinter                : don't change default printer during printing

   该参数相当重要,如果不加该参数的话,在打印的时候不能正常随意切换打印机,系统总是将所有打印任务发送到第一个任务指定的打印机
  -restoreprinter              : restore original settings to printer after printing
  -printermargins              : reduce to printer margins when printing

   打印边框,有些PDF文件边框比较窄,不使用此参数可能导致边框打印不完全。

  -checkjobstatus              : check status for print jobs
  -checkjobtime           : delay some time before check status for print jobs, in millisecond
  -nodelfailjobs               : don't delete failed print jobs
  -movetotop                   : move print area to top if printer paper not same as PDF paper, default is 'center'
  -openpassword        : set open password to PDF file
  -ispasswordprotected         : check if a PDF file protected by 'open password' or not
  -getpagecount                : read page count from input PDF file
  -jobusername         : specify the name of the user who owns the print job
  -jobdocname          : specify document name of print job
  -ownerservicename    : specify the name of OwnerService on Windows Server 2008
  -deleteownerservice  : remove the OwnerService from Windows
  -silent                      : print PDF files silently

   对于中文特别说明的地方需特别注意,其它的参数使用比较少。

   言归正传,JAVA程序打印PDF在上述的基础之上,通过JAVA 语言操作命令行的方式即可轻松实现。

   语法为:Runtime.getRuntime().exec("");而批量打印的实现即是隐藏在此处。

  同时官网下载的版本为试用版本,打印时有水印,可以通过脱壳软件对.exe文件进行脱壳后,通过查找的方式将代码中的水印文字替换成"",再

 进行重新打包即可,过程不是很复杂,本人成功操作过。

 另该软件网址为:www.verypdf.com

你可能感兴趣的:(Java,Eclipse,RCP)