【发布时间】:2016-04-11 23:48:14
【问题描述】:
我想使用 java 打印一个文档,但是程序是成功的,但是我的打印机没有打印任何东西。为什么会这样?你有什么解决方案吗?如果我的打印机不支持pdf,有没有办法打印pdf文件甚至docx文件?
package useprintingserviceinjava;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public class UsePrintingServiceInJava {
private static boolean jobRunning = true;
public static void main(String[] args) throws Exception {
InputStream is;
is = new BufferedInputStream(new FileInputStream("PAPER_SENSOR.pdf"));
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = service.createPrintJob();
printJob.addPrintJobListener(new JobCompleteMonitor());
Doc doc = new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
printJob.print(doc, null);
while (jobRunning) {
Thread.sleep(1000);
}
System.out.println("Exiting app");
is.close();
}
private static class JobCompleteMonitor extends PrintJobAdapter {
@Override
public void printJobCompleted(PrintJobEvent jobEvent) {
System.out.println("Job completed");
jobRunning = false;
}
}
}
这是我研究的代码,但仍然无法打印。以下是基于我的研究的另一个代码:
package javaapplication24;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
public class HandlePrintJobEvents {
public static void main(String[] args) throws Exception {
// create a PDF doc flavor
try ( // Open the image file
InputStream is = new BufferedInputStream(new FileInputStream("C:\\Users\\JUSTINE\\Documents\\thesis document\\PAPER_SENSOR.pdf"))) {
// create a PDF doc flavor
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
// Locate the default print service for this environment.
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// Create and return a PrintJob capable of handling data from
// any of the supported document flavors.
DocPrintJob printJob = service.createPrintJob();
// register a listener to get notified when the job is complete
printJob.addPrintJobListener(new PrintJobMonitor());
// Construct a SimpleDoc with the specified
// print data, doc flavor and doc attribute set.
Doc doc = new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
// Print a document with the specified job attributes.
printJob.print(doc, null);
}
}
private static class PrintJobMonitor implements PrintJobListener {
@Override
public void printDataTransferCompleted(PrintJobEvent pje) {
// Called to notify the client that data has been successfully
// transferred to the print service, and the client may free
// local resources allocated for that data.
}
@Override
public void printJobCanceled(PrintJobEvent pje) {
// Called to notify the client that the job was canceled
// by a user or a program.
}
@Override
public void printJobCompleted(PrintJobEvent pje) {
// Called to notify the client that the job completed successfully.
}
@Override
public void printJobFailed(PrintJobEvent pje) {
// Called to notify the client that the job failed to complete
// successfully and will have to be resubmitted.
}
@Override
public void printJobNoMoreEvents(PrintJobEvent pje) {
// Called to notify the client that no more events will be delivered.
}
@Override
public void printJobRequiresAttention(PrintJobEvent pje) {
// Called to notify the client that an error has occurred that the
// user might be able to fix.
}
}
}
谢谢 :) *我已经尝试了 2 台打印机,但仍然无法打印。
【问题讨论】:
-
您使用的是默认打印机。通过添加以下内容尝试查看是否设置:
System.out.println("Default printer: " + PrintServiceLookup.lookupDefaultPrintService().getName()); -
默认打印机:佳能 iP2700 系列,即输出。
-
添加
PrintRequestAttributeSet params = new HashPrintRequestAttributeSet(); params.add(new Copies(1));有帮助吗?这将设置SimpleDoc的参数(因此将null替换为params)。 -
线程“主”java.lang.ClassCastException 中的异常:javax.print.attribute.HashPrintRequestAttributeSet 无法在 useprintingserviceinjava.UsePrintingServiceInJava.main 处转换为 javax.print.attribute.DocAttributeSet,这是发生的错误
-
对不起,我的错。我应该要求您保留
SimpleDoc的 parameter 参数,但将print方法的 parameter 参数更改为params