一、java原生方式打印PDF文件
正反面都打印,还未研究出只打印单面的方法,待解决
public static void printFile(String path) throws Exception { File file = new File(path); File[] fies=file.listFiles(); for(File f:fies){ System.out.println("file "+f.getName()); String fileExt=f.getName().substring(f.getName().indexOf(".")+1,f.getName().length()); if("pdf".equalsIgnoreCase(fileExt)){ String filepath=path+File.separator+f.getName(); File pdfFile=new File(filepath); //构建打印请求属性集 PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); //设置打印格式,因为未确定文件类型,这里选择AUTOSENSE DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; //查找所有的可用打印服务 PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); //定位默认的打印服务 PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); //显示打印对话框 //PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras); if(defaultService!=null){ DocPrintJob job = defaultService.createPrintJob(); //创建打印作业 FileInputStream fis = new FileInputStream(pdfFile); //构造待打印的文件流 DocAttributeSet das = new HashDocAttributeSet(); Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式 job.print(doc, pras); //进行文件的打印 } f.delete(); } } } public static void main(String[] args) { //System.out.println("Value:"+test()); //打印pdf的一个方法,首先安装下PDFCreator软件 try { printFile("D:"+File.separator); } catch (Exception e) { System.out.println("打印文件异常:"+e.getMessage()); e.printStackTrace(); } }
二、使用pdfBox方式此问题解决
1.maven依赖
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.3</version> </dependency>
2.代码(有打印预览)
import java.awt.print.Book; import java.awt.print.PageFormat; import java.awt.print.Paper; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.PageRanges; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.printing.PDFPageable; import org.apache.pdfbox.printing.PDFPrintable; public final class PrintUtils { public static void main(String[] args) { String fileURL = "http"; String fileName = ".pdf文件"; downloadFile(fileURL,fileName); printWithDialog(getFilePath(fileName)); } public static void printWithDialog(String filePath) { try { PDDocument document = PDDocument.load(new File(filePath)); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); if (job.printDialog()){ job.print(); } } catch (IOException e) { e.printStackTrace(); } catch (PrinterException e) { e.printStackTrace(); } } public static String getFilePath(String fileName){ File path = new File(System.getProperty("user.dir").concat("/downFile")); if (!path.exists() && !path.isDirectory()) { path.mkdir(); } String filePath = path + "/" + fileName; return filePath; } public static boolean downloadFile(String fileURL,String fileName) { try { URL url = new URL(fileURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); DataInputStream in = new DataInputStream(connection.getInputStream()); DataOutputStream out = new DataOutputStream(new FileOutputStream(getFilePath(fileName))); byte[] buffer = new byte[4096]; int count = 0; while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } out.close(); in.close(); return true; } catch (Exception e) { return false; } } }