gpt4 book ai didi

打印机忽略的 javax.print.attribute.PrintRequestAttributeSet 选项

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:24:04 24 4
gpt4 key购买 nike

我必须使用 javax.print api 打印文件(纯文本)。我能够查找打印机并提交打印作业。但我只能打印一份文件。以下是我一直在使用的代码。

打印机无法识别我使用 PrintRequestAttributeSet 指定的任何选项/属性。虽然我指定打印 2 份,但打印机只打印一份。我在这里做错了什么吗?

使用的打印机:佳能iR5050 PCL6


包 com.print;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Finishings;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.NumberUp;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.Sides;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class TestPrint {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "D:/test.log";
// Open the file
InputStream in = null;
try {
in = new FileInputStream(fileName);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

// Figure out what type of file we're printing
DocFlavor myFormat = getFlavorFromFilename(fileName);
// Create a Doc
Doc myDoc = new SimpleDoc(in, myFormat, null);
// Build a set of attributes
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.LANDSCAPE);
aset.add(new Copies(2));
aset.add(Sides.DUPLEX);
aset.add(MediaSizeName.NA_LETTER);
aset.add(new NumberUp(2));
aset.add(Finishings.STAPLE);
// discover the printers that can print the format according to the
// instructions in the attribute set
PrintService[] services = PrintServiceLookup.lookupPrintServices(
myFormat, aset);

// Create a print job from one of the print services
if (services.length > 0) {
System.out.println("The print sent to>>>" + services[0].getName());
DocPrintJob job = services[0].createPrintJob();

// Monitor the print job with a listener
job.addPrintJobListener(new PrintJobAdapter() {
public void printDataTransferCompleted(PrintJobEvent e) {
System.out.println("Data transfer completed!");
}

public void printJobNoMoreEvents(PrintJobEvent e) {
System.out.println("No more events!");
}

public void printJobRequiresAttention(PrintJobEvent e) {
System.out.println("Requires Attention!");
}

public void printJobFailed(PrintJobEvent e) {
System.out.println("Print Job Failed!");
}

public void printJobCompleted(PrintJobEvent e) {
System.out.println("Print Job Completed!");
}

public void printJobCanceled(PrintJobEvent e) {
System.out.println("Print Job Cancelled!");
}
});

try {
job.print(myDoc, aset);
} catch (PrintException pe) {
pe.printStackTrace();
}
System.out.println("The print job ........");
}
}

// A utility method to return a DocFlavor object matching the
// extension of the filename.
public static DocFlavor getFlavorFromFilename(String filename) {
String extension = filename.substring(filename.lastIndexOf('.') + 1);
extension = extension.toLowerCase();
if (extension.equals("gif"))
return DocFlavor.INPUT_STREAM.GIF;
else if (extension.equals("jpeg"))
return DocFlavor.INPUT_STREAM.JPEG;
else if (extension.equals("jpg"))
return DocFlavor.INPUT_STREAM.JPEG;
else if (extension.equals("png"))
return DocFlavor.INPUT_STREAM.PNG;
else if (extension.equals("ps"))
return DocFlavor.INPUT_STREAM.POSTSCRIPT;
else if (extension.equals("txt"))
return DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;
// Fallback: try to determine flavor from file content
else
return DocFlavor.INPUT_STREAM.AUTOSENSE;
}

}

最佳答案

经过长时间的研究和无数小时的测试,发现了以下推论,

1) DocFlavor.INPUT_STREAM.AUTOSENSE 不支持打印属性 ○ http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4876267

2) DocFlavor.INPUT_STREAM.POSTSCRIPT 也不支持 javax.print 属性 ○ 请引用 Bug ID 4722601(无法发布多个链接)

3) JDK 没有windows API 来查找打印是否支持PCL ○ 请引用 Bug ID 4529660(无法发布多个链接)

因此以上都不是 sun 支持论坛中指定的缺陷。所以我找到的唯一选择是将所有 POSTSCRIPT 命令嵌入到 Postscript 文件中,并使用 Javax.print api 打印该文档。确保将 flavor 设置为 AUTOSENSE。

还要确保您的打印机支持 POSTSCRIPT 格式。您也可以尝试使用 PJL。希望这些信息对您有所帮助!

问候,悟空

关于打印机忽略的 javax.print.attribute.PrintRequestAttributeSet 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4034491/

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