Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用单色选项Mac OS以黑白打印文档? 我无法在MAC上打印带有单色选项的pdf,甚至 已选择单色选项,但仍在MAC上打印 颜色但在windows上,它对单色和颜色两个选项都可以正常工作 此外,即使连接了正确的打印机,MAC上的设置也会被禁用。 但是同样的代码在windows上运行,它在每一个应用程序中都能正常工作 情景_Java_Swing_Pdfbox_Java Print - Fatal编程技术网

Java 如何使用单色选项Mac OS以黑白打印文档? 我无法在MAC上打印带有单色选项的pdf,甚至 已选择单色选项,但仍在MAC上打印 颜色但在windows上,它对单色和颜色两个选项都可以正常工作 此外,即使连接了正确的打印机,MAC上的设置也会被禁用。 但是同样的代码在windows上运行,它在每一个应用程序中都能正常工作 情景

Java 如何使用单色选项Mac OS以黑白打印文档? 我无法在MAC上打印带有单色选项的pdf,甚至 已选择单色选项,但仍在MAC上打印 颜色但在windows上,它对单色和颜色两个选项都可以正常工作 此外,即使连接了正确的打印机,MAC上的设置也会被禁用。 但是同样的代码在windows上运行,它在每一个应用程序中都能正常工作 情景,java,swing,pdfbox,java-print,Java,Swing,Pdfbox,Java Print,我正在使用macOS High Sierra 10.13.6版 public boolean printFile(String fileUrl) { PrintService[] printServicesAll = PrintServiceLookup.lookupPrintServices(null, null); PrintService[] printServicesFiltered; PrintRequestAttributeSet attrib = new HashPrintReq

我正在使用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;
    }
}
}
主打印文件方法


我也面临同样的问题。我试过用PDF框进行灰度显示,但是没有用。我也面临同样的问题。我试着用PDF框进行灰度显示,但没用。
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;
}