public static void sendPdfToPrinter(String epsilon)
{
FileInputStream psStream = null;
try {
psStream = new FileInputStream(epsilon);
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++)
{
String svcName = services[i].toString();
System.out.println("service found: "+ svcName);
if (svcName.contains("series"))
{
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {pe.printStackTrace();}
} else {
System.out.println("no printer services found");
}
}
public static void main(String[] args) throws IOException
{
//@SuppressWarnings("unused")
//Testing t = new Testing();
String DEST = ("C:/Users/Brian/Desktop/SO046201R-17/TestingAlpha/6FS-2m.pdf");
sendPdfToPrinter(DEST);
}
我正在编写一个程序,它将文本写入一系列PDF文件,然后将它们发送到打印机。我已经完成了PDF部分的写作,但每当我尝试将文件传递给“sendPdfToPrinter”方法时,我都会遇到问题。到目前为止,我已经测试了HP Deskjet打印机和Canon Inkjet打印机没有成功(前者发出“Java Document error”消息,后者不会将文件添加到队列中)。我想我的问题归结为:
代码或我正在使用的打印机有问题吗?有解决方法吗?
使用Mark的编辑:
public static void sendPdfToPrinter(String epsilon) throws
InvalidPasswordException, IOException
{
FileInputStream psStream = null;
try {
psStream = new FileInputStream(epsilon);
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
PDDocument myDoc = PDDocument.load(new File(epsilon));
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services =
PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++)
{
String svcName = services[i].toString();
System.out.println("service found: "+ svcName);
if (svcName.contains("series"))
{
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
PrinterJob job = PrinterJob.getPrinterJob();
try {
job.setPrintService(myPrinter);
job.setPageable(new PDFPageable(myDoc));
job.print();
} catch (PrinterException e) {
// Handle the exception.
}
} else {
System.out.println("no printer services found");
}
}
答案 0 :(得分:1)
这可能是因为您的打印机本身不支持PDF。我实际上设法用您的代码打印PDF,但它打印出来就像将PDF文件作为文本文件打开一样。
一种解决方案是使用Apache的Java PDF库:PDFBox。该文件可以这样加载:
PDDocument myDoc = PDDocument.load(new File(epsilon));
要打印文件,请使用PrinterJob
代替DocPrintJob
:
PrinterJob job = PrinterJob.getPrinterJob();
try {
job.setPrintService(myPrinter);
job.setPageable(new PDFPageable(myDoc));
job.print();
} catch (PrinterException e) {
// Handle the exception.
}