代码段如下:
private void printFileAction()
{
JFileChooser fileChooser = new JFileChooser();
int state = fileChooser.showOpenDialog(this);//弹出文件选择对话框
if (state == fileChooser.APPROVE_OPTION)//如果用户选定了文件
{
File file = fileChooser.getSelectedFile();//获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService
, defaultService, flavor, pras);
if (service != null)
{
try
{
DocPrintJob job = printService[0].createPrintJob();//创建打印作业
FileInputStream fis = new FileInputStream(file);//构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);//建立打印文件格式
job.print(doc,pras);//进行文件的打印
}
catch(Exception e)
{
e.printStackTrace();
} }
}
}
我不知道为什么,编译没有问题,java打印字符串,也没有问题,可是打印文件却总是打印机状态显示错误,无法打印,请大家帮忙
private void printFileAction()
{
JFileChooser fileChooser = new JFileChooser();
int state = fileChooser.showOpenDialog(this);//弹出文件选择对话框
if (state == fileChooser.APPROVE_OPTION)//如果用户选定了文件
{
File file = fileChooser.getSelectedFile();//获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService
, defaultService, flavor, pras);
if (service != null)
{
try
{
DocPrintJob job = printService[0].createPrintJob();//创建打印作业
FileInputStream fis = new FileInputStream(file);//构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);//建立打印文件格式
job.print(doc,pras);//进行文件的打印
}
catch(Exception e)
{
e.printStackTrace();
} }
}
}
我不知道为什么,编译没有问题,java打印字符串,也没有问题,可是打印文件却总是打印机状态显示错误,无法打印,请大家帮忙
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货