62,612
社区成员
发帖
与我相关
我的任务
分享
public class PrintTest
{
public static void main(String[] args)
{
FileInputStream psStream=null;
try
{
psStream=new FileInputStream("F:\\test.txt");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
if(psStream==null)
{
return;
}
//设置打印数据的格式,此处为图片gif格式
DocFlavor psInFormat=DocFlavor.INPUT_STREAM.AUTOSENSE;
//创建打印数据
Doc myDoc=new SimpleDoc(psStream,psInFormat,null);
//设置打印属性
PrintRequestAttributeSet aset=new HashPrintRequestAttributeSet();
//aset.add(new Copies(3));
//查找所有打印服务
PrintService[] services= PrintServiceLookup.lookupPrintServices(psInFormat,aset);
//将所有查找出来的打印机与自己想要的打印机进行匹配,找出自己想要的打印机
PrintService myPrint=null;
System.out.println(services.length);
for(int i=0;i<services.length;i++)
{
System.out.println("service found "+services[i]);
String svcName=services[i].toString();
if(svcName.contains("PDF"))
{
myPrint=services[i];
System.out.println("myPrinter found "+svcName);
System.out.println("myPrinter founr "+myPrint);
break;
}
}
//可以输出打印机的各项属性
AttributeSet att=myPrint.getAttributes();
for(Attribute a:att.toArray())
{
String attributeName;
String attributeValue;
attributeName=a.getName();
attributeValue=att.get(a.getClass()).toString();
System.out.println(attributeName+":"+attributeValue);
}
if(myPrint!=null)
{
DocPrintJob job=myPrint.createPrintJob();//创建文档打印作业
try
{
job.print(myDoc,aset);
}
catch(Exception e)
{
e.printStackTrace();
}
}
else
{
System.out.println("no print services found");
}
}
}
public static void printToFile(int fileType) throws PrinterException
{
//相关变量的定义和初始化
File file = FileHelper.getFile(fileType);
// 创建打印作业
PrinterJob job = PrinterJob.getPrinterJob();
//调用UI界面选择打印服务
PrintService service = ServiceUI.printDialog(null, 200, 200,
printServices, defaultService, flavor, pras);
job.setPrintService(service);
JEditorPane reportPane = new JEditorPane();
reportPane.setContentType("txt/html");
HTMLEditorKit kit=new HTMLEditorKit();
reportPane.setEditorKit(kit);
HTMLDocument doc= (HTMLDocument) reportPane.getDocument();
try
{
if (file != null)
{
FileInputStream reader = new FileInputStream(file);
kit.read(reader,doc,0);
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (BadLocationException e)
{
e.printStackTrace();
}
//把JEditorPane的可打印范围传入到打印作业中
job.setPrintable(reportPane.getPrintable(null, null));
//打印开始
job.print();
}
思路是创建一个JEditorPane,然后把内容输入到JEditorPane里面,然后获取JEditorPane的打印区域再打印。我是这样解决的,感觉很牵强[/quote]
好的,不过刚发现我跟楼主做的不是一个东西,我用pdfbox处理pdf 文件
public static void printToFile(int fileType) throws PrinterException
{
//相关变量的定义和初始化
File file = FileHelper.getFile(fileType);
// 创建打印作业
PrinterJob job = PrinterJob.getPrinterJob();
//调用UI界面选择打印服务
PrintService service = ServiceUI.printDialog(null, 200, 200,
printServices, defaultService, flavor, pras);
job.setPrintService(service);
JEditorPane reportPane = new JEditorPane();
reportPane.setContentType("txt/html");
HTMLEditorKit kit=new HTMLEditorKit();
reportPane.setEditorKit(kit);
HTMLDocument doc= (HTMLDocument) reportPane.getDocument();
try
{
if (file != null)
{
FileInputStream reader = new FileInputStream(file);
kit.read(reader,doc,0);
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (BadLocationException e)
{
e.printStackTrace();
}
//把JEditorPane的可打印范围传入到打印作业中
job.setPrintable(reportPane.getPrintable(null, null));
//打印开始
job.print();
}
思路是创建一个JEditorPane,然后把内容输入到JEditorPane里面,然后获取JEditorPane的打印区域再打印。我是这样解决的,感觉很牵强