javax.print.DocPrintJob 除了 java.awt.print.PrinterJob 打印之外什么都不打印

问题描述

我试图打印任何文件 docx、jpg、eml、msg、txt、pdf、xls 等...

这是我的代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.JobName;

import com.PrintNative.PrintListener;

public class test {

    public static void main(String[] args) throws PrintException,IOException {
        PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
        
        PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE,attrs);
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        
        
        PrintService selection = ServiceUI.printDialog(null,200,services,service,null,attrs); 
        if(selection != null)
        {
            String fileName = "C:\\Temp\\test.docx";
            File f = new File(fileName);
            
            InputStream is = new FileInputStream(f);
            
            DocPrintJob job = selection.createPrintJob();

            Doc doc = new SimpleDoc(is,DocFlavor.INPUT_STREAM.AUTOSENSE,new HashDocAttributeSet());
            
            PrintRequestAttributeSet jAttrs = new HashPrintRequestAttributeSet();
            jAttrs.add(new JobName(fileName,null));
            
            job.addPrintJobListener(new PrintListener(is));
            
            job.print(doc,jAttrs);
            
            if(is != null)
                is.close();
            
        }
    }

}

此代码将文档添加到打印机队列但什么都不做... 这是队列:

enter image description here

但是当我使用带有 java.awt.print.PrinterJob 的打印机时,pdf 文件的打印工作完美:

public static void main(String[] args) {
    PrinterJob printerJob = PrinterJob.getPrinterJob();

    PageFormat pageFormat = printerJob.defaultPage();
    Paper paper = pageFormat.getPaper();
    paper.setImageableArea(0,pageFormat.getWidth(),pageFormat.getHeight());
    pageFormat.setPaper(paper);

    File f = new File("C:\\Temp\\test.pdf");

    document = PDDocument.load(f);
    printerJob.setPageable(new PDFPageable((PDDocument) document));

    if (printerJob.printDialog()) {
        printerJob.print();
    }
}

我能做什么? 打印机没有问题,我的电脑也没有问题,除了java应用程序,我可以从任何地方打印。

这一张也打印:

Desktop.getDesktop().print(f);

但是对于这个没有打印机对话框。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...