public class Print {
public static void main(String[] args) {
FileInputStream fin=null;
try {
fin = new FileInputStream("D:/image/image1.jpg");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (fin != null) // 当打印内容不为空时
{
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
// 定位默认的打印服务
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// 创建打印作业
DocPrintJob job = printService.createPrintJob();
// 设置打印属性
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// 设置纸张大小,也可以新建MediaSize类来自定义大小
pras.add(MediaSizeName.ISO_A4);
DocAttributeSet das = new HashDocAttributeSet();
// 指定打印内容
Doc doc = new SimpleDoc(fin, flavor, das);
// 不显示打印对话框,直接进行打印工作
try {
job.print(doc, pras); // 进行每一页的具体打印操作
} catch (PrintException pe) {
pe.printStackTrace();
}
} else {
// 如果打印内容为空时,提示用户打印将取消
JOptionPane.showConfirmDialog(null,
"Sorry, Printer Job is Empty, Print Cancelled!",
"Empty", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE);
}
}