帮我用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);
}
}
}
}
从此功能打印机无法打印,但打印机指示灯仅在我调用此功能时亮起