gpt4 book ai didi

java - 打印机服务 未找到?

转载 作者:行者123 更新时间:2023-12-01 15:52:26 25 4
gpt4 key购买 nike

当我调试这个程序时,服务数组是空的?

 import java.io.FileInputStream;
import java.io.FileNotFoundException;

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.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSize;

import javax.print.attribute.standard.Sides;


public class PrintFileWithSpec {


public static void printFile(String filename){

FileInputStream psStream=null;
try {
psStream = new FileInputStream(filename);
} catch (FileNotFoundException ffne) {
}
if (psStream == null) {
return;
}

DocFlavor psInFormat = DocFlavor.INPUT_STREAM.POSTSCRIPT;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();
aset.add(new Copies(5));
//aset.add(MediaSize.ISO_A4);
aset.add(Sides.DUPLEX);
PrintService[] services =
PrintServiceLookup.lookupPrintServices(psInFormat, aset);
if (services.length > 0) {
DocPrintJob job = services[0].createPrintJob();
try
{
job.print(myDoc, aset);
} catch (PrintException pe) {}
}
}

public static void main(String [] args){

printFile("D:/Resume.doc");
}
}

最佳答案

这是因为没有找到与指定的 DocFlavor 对应的 PrintService和属性集。除非您的打印机硬件非常最新,否则可能很难找到支持 PostScript 的打印机。您可以像这样检查所有 DocFlavors 受支持的内容:

DocFlavor[] docFalvor = prnSvc.getSupportedDocFlavors();
for (int i = 0; i < docFalvor.length; i++) {
System.out.println(docFalvor[i].getMimeType());
}

要查找特定的打印服务,您可以执行以下操作:

PrintService prnSvc = null;

/* locate a print service that can handle it */
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(null, null);
if (pservices.length > 0) {
int ii=0;
while(ii < pservices.length)
{
System.out.println("Named Printer found: "+pservices[ii].getName());
if (pservices[ii].getName().endsWith("YourPrinterName")) {
prnSvc = pservices[ii];
System.out.println("Named Printer selected: " + pservices[ii].getName() + "*");
break;
}
ii++;
}

关于java - 打印机服务 未找到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5742143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com