如何在java中使用DocPrintJob打印

如何在java中使用DocPrintJob打印,java,swing,pdf,printing,Java,Swing,Pdf,Printing,帮助我使用DocPrintJob打印 我尝试过以下代码: private void printToPrinter() { Object selection = null; FileInputStream textStream = null; try { textStream = new FileInputStream("G:\\Bill\\Bill1.pdf"); } catch (java.io.FileNotFoundException e)

帮助我使用DocPrintJob打印

我尝试过以下代码:

private void printToPrinter() {
    Object selection = null;
    FileInputStream textStream = null;
    try {
        textStream = new FileInputStream("G:\\Bill\\Bill1.pdf");
    } catch (java.io.FileNotFoundException e) {
        System.out.println("Error trying to find the print file created "
                + "in the printToPrinter() method");
    }
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc mydoc = new SimpleDoc(textStream, flavor, null);
    //Look up available printers.
    PrintService[] printers = PrintServiceLookup.lookupPrintServices(flavor, null);
    if (printers.length == 0) {
        // No printers found. Inform user.
        JOptionPane.showMessageDialog(this, 
                "No printers could be found on your system!", 
                "Error!", JOptionPane.ERROR_MESSAGE);
    } else {
        selection = JOptionPane.showInputDialog(this, 
                "Please select the desired printer:", "Print",
                JOptionPane.INFORMATION_MESSAGE, null, printers,
                PrintServiceLookup.lookupDefaultPrintService());
        if (selection instanceof PrintService) {
            PrintService chosenPrinter = PrintServiceLookup.lookupDefaultPrintService();
            //JOptionPane.showMessageDialog(null, "Default : "+chosenPrinter.getName());
            DocPrintJob printJob = chosenPrinter.createPrintJob();
            try {
                PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                pras.add(new Copies(1));
                printJob.print(mydoc, pras);
                JOptionPane.showMessageDialog(null, "Size = " + mydoc.toString());
                //(mydoc, pras);
                textStream.close();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, 
                        "Error occured while attempting to print." + e, 
                        "Error!", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

从这个函数中,打印机无法打印,但打印机指示灯只有在我调用这个函数时才会亮起。上面的函数没有给出任何错误。Java一般不支持直接PDF打印。我写了一篇关于如何在Java中打印PDF的博客文章,感谢回复我,先生!我用PDFRender代替上面的代码。PDFRender有什么问题吗?