日期:2014-05-20  浏览次数:20645 次

Java 中如何调用windows的打印程序?
Java   中如何调用windows的打印程序?

------解决方案--------------------
沙发
------解决方案--------------------
在JDK1.6中有新的类 "Desktop ",有个Print()可以调用。
之前的嘛,自己写打印程序吧。不过JAVA关于打印的问题是最没人回答的。
------解决方案--------------------
<%@ page contentType= "text/html; charset=GBK " %>
<html>
<head>
<title>
sign6
</title>
  <script language= "javascript ">
  function printsetup(){
alert(wb);
  // 打印页面设置
  wb.execwb(8,1);
  }
  function printpreview(){
  // 打印页面预览
  wb.execwb(7,1);
  }
  function printit()
  {
alert(wb);
  if (confirm( '确定打印吗? ')) {
  wb.ExecWB(6,6);
  }
  }
   </script>

</head>

<body bgcolor= "#ffffff ">
<OBJECT classid= "CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 " height=0 id=wb name=wb width=0> </OBJECT>

<form>
<table>
<tr>
<td>
<input type=button name=button_print value= "打印 " onclick= "javascript:printit() ">
   <input type=button name=button_setup value= "打印页面设置 " onclick= "javascript:printsetup(); ">
   <input type=button name=button_show value= "打印预览 " onclick= "javascript:printpreview(); ">
   <input type=button name=button_fh value= "关闭 " onclick= "javascript:window.close(); "> </td>
</tr>
</table>
</form>
</body>
</html>


我做过,不过只能打能txt,和图片文件,如果是.doc文件我是用jacob先转换为txt文件进行打印的,不过格式被破坏了!
package com.szallcom.tools;

import java.io.File;
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
public class Printtupian
{
public void printjpg(String getfile,String fensu)
{
//构造一个文件选择器,默认为当前目录

File file = new File(getfile);//获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
//查找所有的可用打印服务
//PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
long j=Integer.parseInt(fensu);
for(int i=0;i <j;i++)
{
try {
DocPrintJob job = defaultService.createPrintJob(); //创建打印作业
FileInputStream fis = new FileInputStream(file); //构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式
job.print(doc, pras); //进行文件的打印
}
catch(Exception e) {
e.printStackTrace();
}
}
}

}

------解决方案--------------------
写个脚本,window.print()就可以了