【发布时间】:2012-10-17 12:15:09
【问题描述】:
我需要在打印机上使用 java (Swing) 打印图像和一些数据,但都是徒劳的。我只能处理数据,但不能处理图像。我有一个 abc.png 文件和 6 个 jTextBoxes,必须在打印机上打印其中的值。我正在使用 FileWriter 和 PrinterJob 类来实现该作业。数据和图像可以分开打印,但不能一起打印。谁能给我一些建议。
谢谢。
图片打印代码:
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
if (pss.length == 0)
throw new RuntimeException("No printer services available.");
PrintService ps = pss[0];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream("C://a.gif");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
//Doc doc1=new SimpleDoc();
job.print(doc, pras);
//fin.close();
} catch (IOException ie) {
ie.printStackTrace();
} catch (PrintException pe) {
pe.printStackTrace();
}
打印文本的代码:
只需在 FileInputStream 中传递文件在硬盘上的位置。
【问题讨论】:
-
你能发布代码如何尝试将图像和文本发送到打印机吗?
-
Glen Lamb 我今天看到了,我会努力让它知道的。