是否可以考虑使用WordDocument.SendFax方法?参考一下这个java的程序,你有一些收获。java 实现word转为 tif格式??急1.我用打印的方式没有得到任何文件(用的是虚拟传真打印机)2.我用JACOB老是缺少组建异常3.用jawin调用word转为pdf的方法出异常1.我用打印的方式没有得到任何文件(用的是虚拟传真打印机)public class Y {/*打印指定的文件*/public void printFileAction(){//构造一个文件选择器,默认为当前目录JFileChooser fileChooser = new JFileChooser("c:\"); int state = fileChooser.showOpenDialog(null);//弹出文件选择对话框 if (state == fileChooser.APPROVE_OPTION)//如果用户选定了文件 { File file = fileChooser.getSelectedFile();//获取选择的文件//构建打印请求属性集 PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;//查找所有的可用打印服务 PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);//定位默认的打印服务 PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); //显示打印对话框PrintService service = ServiceUI.printDialog(null, 200, 200, printService , defaultService, flavor, pras); if (service != null) { try { DocPrintJob job = service.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(); } } }}}2.我用JACOB老是缺少组建异常package com;import com.jacob.com.*;import com.jacob.activeX.*;public class Dispatch_MSWord {private ActiveXComponent wordCom=null;private Object wordDoc=null;private final Variant False=new Variant(false);private final Variant True=new Variant(true);/** * 打开word文档 * @param filePath word文档 * @return 返回word文档对象 */public boolean openWord(String filePath){//建立ActiveX部件wordCom=new ActiveXComponent("Word.Application");try{//返回wrdCom.Documents的DispatchObject wrdDocs=wordCom.getProperty("Documents").toDispatch();//调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDocwordDoc=Dispatch.invoke((Dispatch) wrdDocs,"Open",Dispatch.Method,new Object[]{filePath},new int[1]).toDispatch();return true;}catch(Exception ex){ex.printStackTrace();}return false;}/** * 关闭word文档 */public void closeWord(){//关闭word文件wordCom.invoke("Quit",new Variant[]{});}/** * 打开word,调用word中的宏 * @param filePath word文件路径 * @param macroName 被调用的宏名字 * @param parameter 调用宏的参数数组 */public void callWordMacro(String filePath,String macroName,Object parameter[]){if (!openWord(filePath)){closeWord();return;}else{//使用方法传入的参数parameter调用word文档中的MyWordMacro宏Dispatch.invoke((Dispatch)wordDoc,macroName,Dispatch.Method,parameter,new int[1]);closeWord();}}/** * 打开word,替换其中的文字 * @param filePath word文件路径 * @param sourceStr 源文字 * @param replaceStr 替换后的文字 */public void callReplaceWord(String filePath,String sourceStr,String replaceStr){if (!openWord(filePath)){closeWord();return;}try{//获得Selection对象Dispatch selectDoc=wordCom.getProperty("Selection").toDispatch();//获得Find对象Dispatch find = Dispatch.call(selectDoc,"Find").toDispatch();//设置替换的方法属性,但是不支持ReplaceWith的属性,而且使用Replancement.Text属性也无济于事。Dispatch.put(find,"Text",sourceStr);//所以使用Find对象的Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)方法//详细内容见MSDN的office2000开发文档Variant True=new Variant(true);Variant False=new Variant(false);Variant FindText=new Variant(sourceStr);Variant ReplaceWith=new Variant(replaceStr);Variant Format=False;Variant Forward=True;Variant MatchCase=True;Variant MatchWholeWord=True;Variant MatchWildcards=False;Variant MatchSoundsLike=False;Variant MatchAllWordForms=False;int wdFindWrap_FindContinue=1;Variant Wrap=new Variant(wdFindWrap_FindContinue);int wdReplace_ReplaceAll=2;Variant Replace=new Variant(wdReplace_ReplaceAll);//使用callN方法调用execute方法Dispatch.callN(find,"Execute",new Variant[]{FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace});Dispatch.invoke((Dispatch) wordDoc,"SaveAs",Dispatch.Method,new Object[]{"c:\111.doc"},new int[1]);closeWord();}catch(Exception ex){ex.printStackTrace();}finally{closeWord();}}/** * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件 * @param sourceFilePath 源文件路径 * @param destinPSFilePath 首先生成的PS文件路径 * @param destinPDFFilePath 生成PDF文件路径 */public void docToPDF(String sourceFilePath,String destinPSFilePath,String destinPDFFilePath){if (!openWord(sourceFilePath)){closeWord();return;}//建立Adobe Distiller的com对象ActiveXComponent distiller=new ActiveXComponent("PDFDistiller.PDFDistiller.1");try{//设置当前使用的打印机,我的Adobe Distiller打印机名字为"Adobe PDF"wordCom.setProperty("ActivePrinter",new Variant("Adobe PDF"));//设置printout的参数,将word文档打印为postscript文档。目前只使用了前5个参数,如果要使用更多的话可以参考MSDN的office开发相关api//是否在后台运行 Variant Background=False; //是否追加打印 Variant Append =False; //打印所有文档 int wdPrintAllDocument=0; Variant Range =new Variant(wdPrintAllDocument); //输出的postscript文件的路径 Variant OutputFileName =new Variant(destinPSFilePath); //调用word文档对象的PrintOut方法:将word文档打印为postscript文档,简称ps文档 Dispatch.callN((Dispatch)wordDoc, "PrintOut", new Variant[]{Background,Append,Range,OutputFileName}) ; System.out.println("由word文档打印为ps文档成功!"); //调用Distiller对象的FileToPDF方法所用的参数,详细内容参考Distiller Api手册 //作为输入的ps文档路径 Variant inputPostScriptFilePath=new Variant(destinPSFilePath); //作为输出的pdf文档的路径 Variant outputPDFFilePath=new Variant(destinPDFFilePath); //定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件 Variant PDFOption=new Variant(""); //调用FileToPDF方法将ps文档转换为pdf文档 Dispatch.callN(distiller,"FileToPDF",new Variant[]{inputPostScriptFilePath,outputPDFFilePath,PDFOption}); System.out.println("由ps文档转换为pdf文档成功!"); }catch(Exception ex){ex.printStackTrace();}finally{closeWord();}}public static void main(String[] argv){Dispatch_MSWord d=new Dispatch_MSWord();//d.callWordMacro("E:/eclipse3.1RC3/workspace/jacobPractice/src/com/bjinfotech/practice/jacob/MacroTest.doc","MyWordMacro",new String[]{"这是调用word宏的测试程序"});//d.callReplaceWord("E:/eclipse3.1RC3/workspace/jacobPractice/src/com/bjinfotech/practice/jacob/MacroTest.doc","$TITLE$","文字已经被替换");d.docToPDF("c:\1.doc","c:\1p.ps","c:\1p.pdf");}} 供你参考啊
2023-08-11 00:50:334