- MACでMONOCHROMEオプションを使用して pdf を印刷できません。モノクロ オプションが選択されていても、MACではCOLORで印刷さ れますが、Windows ではMONOCHROMEとCOLORの両方のオプションで正常に動作します。
- さらに、適切なプリンターが接続されていても、On 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;
}