import java.awt.print.PrinterJob; import java.util.Date; import java.util.Scanner; import javax.print.PrintService; import net.sf.json.JSONObject; public static void out_print_list() { // TODO Auto-generated method stub PrinterJob job = PrinterJob.getPrinterJob(); // 遍历查询打印机名称 boolean flag = false; int i=1; for (PrintService ps : PrinterJob.lookupPrintServices()) { String psName = ps.toString(); System.out.println(i+":"+psName); i++; /* if (psName.contains("导出为WPS PDF")) { System.out.println("就是这个"); job.setPrintService(ps); } */ } }
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.File; import java.io.IOException; import java.util.Date; import java.util.Random; import java.util.Scanner; import javax.print.PrintService; import javax.xml.stream.events.EndDocument; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.printing.PDFPageable; import org.apache.pdfbox.printing.PDFPrintable; import net.sf.json.JSONObject; /** * #接受文件路径,本地路径 ,执行打印操作。 #打印指定位置的pdf文件。 * * @author 作者微信 xiaomao0055 */ public static void print(String path, String printName, String paperA4_A5) throws Exception { File filePath = new File(path); /** * 判断根据path获取的filePath是否为文件夹,如果是则循环打印,如果不是则认为是文件路径,直接打印 */ if (filePath.isDirectory()) { File[] array = getFile(filePath); String fileName = null; for (int i = 0; i < array.length; i++) { fileName = array[i].getPath(); PDDocument document = PDDocument.load(new File(fileName)); printWithPaper(document, printName, paperA4_A5); } } else { PDDocument document = PDDocument.load(new File(path)); printWithPaper(document, printName, paperA4_A5); } } /** * 打印功能 * * @author 作者微信 xiaomao0055 */ public static void printWithPaper(PDDocument document, String printName, String paperA4_A5) throws IOException, PrinterException { PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); // 根据名子设置打印机 if (StringUtil.isNotEmpty(printName)) { for (PrintService ps : PrinterJob.lookupPrintServices()) { String psName = ps.toString(); if (psName.contains(printName)) { // System.out.println("就是这个"); job.setPrintService(ps); } } } // 根据名子设置打印机 // define custom paper Paper paper = new Paper(); // 纸张大小设置为A4 /* * A4纸张尺寸:210mm * 297mm A5 148mm×210mm 2.8476 1英寸:2.54cm 假设屏幕DPI(Dots per * inch)为72像素(pixels)每英寸,计算一下,结果为72px/2.54 = 28.34px 下面给出常见分辨率下,A4纸张大小在屏幕上的像素尺寸: * DPI:72px/inch,A4 size(598 * 842); DPI:96px/inch,A4 size(794* 1123);(default) * DPI:120px/inch,A4 size(1487* 2105); DPI:150px/inch,A4 size(1240* 1754); */ if (paperA4_A5.equals("A4")) { paper.setSize(598, 842); // 1/72 inch } if (paperA4_A5.equals("A5")) { paper.setSize(422, 598); // 1/72 inch A5 148mm×210mm } paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); // no margins // custom page format PageFormat pageFormat = new PageFormat(); // PageFormat.PORTRAIT竖向 // PageFormat.REVERSE_LANDSCAPE纵向 pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE); pageFormat.setPaper(paper); // override the page format Book book = new Book(); // append all pages book.append(new PDFPrintable(document), pageFormat, document.getNumberOfPages()); job.setPageable(book); job.print(); }
站长微信:xiaomao0055
站长QQ:14496453