- 我无法在MAC上使用MONOCHROME选项打印 pdf ,即使选择了单色选项,但仍然在MAC上以COLOR打印, 但在 Windows 上,它对于MONOCHROME和COLOR选项都可以正常工作。
- 另外,即使连接了正确的打印机,在MAC上的设置也会被禁用。但是在 Windows 上运行的相同代码在每种情况下都可以正常工作。
我正在使用macOS High Sierra 版本 10.13.6
public boolean printFile(String fileUrl) { PrintService[] printServicesAll = PrintServiceLookup.lookupPrintServices(null, null); PrintService[] printServicesFiltered; PrintRequestAttributeSet attrib = new HashPrintRequestAttributeSet(); Rectangle screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds(); String OS = System.getProperty("os.name").toLowerCase(); attrib.add(new Copies(1)); attrib.add(new sun.print.DialogOnTop()); attrib.add(Chromaticity.MONOCHROME); attrib.add(DialogTypeSelection.NATIVE); PrintService selectedPrintService = null; if (OS.contains("win")) { if (printServicesAll.length > 0) { printServicesFiltered = removeLogicalPrinters(printServicesAll); selectedPrintService = ServiceUI.printDialog(null, screen.width / 3, screen.height / 3, printServicesFiltered, printServicesFiltered[0], null, attrib); } } else if (OS.contains("mac")) { if (printServicesAll.length > 0) { selectedPrintService = ServiceUI.printDialog(null, screen.width / 3, screen.height / 3, printServicesAll, printServicesAll[0], null, attrib); } } if (attrib.get(Destination.class) != null) { JOptionPane.showMessageDialog(null, "Print to file option not allowed!"); return false; } else { if (selectedPrintService != null) System.out.println("selected printer: " + selectedPrintService.getName()); else return false; try { DocPrintJob job = selectedPrintService.createPrintJob(); job.addPrintJobListener(new PrintJobAdapter() { public void printDataTransferCompleted(PrintJobEvent event) { System.out.println("data transfer complete"); } public void printJobNoMoreEvents(PrintJobEvent event) { System.out.println("received no more events"); } }); print(selectedPrintService, fileUrl, attrib); /*File file = new File(filePath); Desktop.getDesktop().print(file);*/ return true; } catch (Exception e) { e.printStackTrace(); return false; } } }
主要打印文件方法
private boolean print(PrintService printService, String fileUrl, PrintRequestAttributeSet attributes)
throws PrintException {
try {
PDDocument pdf = null;
String fileType = fileUrl.substring(fileUrl.lastIndexOf(".") + 1);
if (fileType.equalsIgnoreCase("bmp") || fileType.equalsIgnoreCase("gif") || fileType.equalsIgnoreCase("jpg") || fileType.equalsIgnoreCase("png")) {
try {
InputStream in = new URL(fileUrl).openStream();
PDDocument document = new PDDocument();
BufferedImage bImg = ImageIO.read(in);
float width = bImg.getWidth();
float height = bImg.getHeight();
PDPage page = new PDPage(new PDRectangle(width, height));
document.addPage(page);
PDImageXObject img = LosslessFactory.createFromImage(document, bImg);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(img, 0, 0);
contentStream.close();
in.close();
pdf = document;
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(printService);
job.setPageable(new PDFPageable(pdf));
job.print(attributes);
pdf.close();
} catch (Exception e) {
e.printStackTrace();
}
} else if (fileType.equalsIgnoreCase("txt")) {
JEditorPane jEditorPane = new JEditorPane(fileUrl);
jEditorPane.print(null, null, false, printService, null, false);
} else if (fileType.equalsIgnoreCase("pdf")) {
pdf = PDDocument.load(new URL(fileUrl).openStream());
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(printService);
job.setPageable(new PDFPageable(pdf));
job.print(attributes);
pdf.close();
}
} catch (Exception e) {
throw new PrintException("Printer exception", e);
}
return true;
}