[html] view plain copy
1.import java.io.File;
2.
3.import org.apache.poi.POIXMLDocument;
4.import org.apache.poi.xwpf.usermodel.XWPFDocument;
5.import org.dom4j.Document;
6.import org.dom4j.DocumentException;
7.import org.dom4j.io.SAXReader;
8.
9.import com.jacob.activeX.ActiveXComponent;
10.import com.jacob.com.ComThread;
11.import com.jacob.com.Dispatch;
12.import com.jacob.com.Variant;
13.public class Word2pdf {
14. static final int wdFormatPDF = 17;// PDF 格式
15. public int wordToPDF(String sfileName,String toFileName) throws Exception{
16.
17. System.out.println("启动Word...");
18. long start = System.currentTimeMillis();
19. ActiveXComponent app = null;
20. Dispatch doc = null;
21. try {
22. app = new ActiveXComponent("Word.Application");
23. // 设置word不可见
24. app.setProperty("Visible", new Variant(false));
25. // 打开word文件
26. Dispatch docs = app.getProperty("Documents").toDispatch();
27.// doc = Dispatch.call(docs, "Open" , sourceFile).toDispatch();
28. doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {
29. sfileName, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
30. System.out.println("打开文档..." + sfileName);
31. System.out.println("转换文档到PDF..." + toFileName);
32. File tofile = new File(toFileName);
33. // System.err.println(getDocPageSize(new File(sfileName)));
34. if (tofile.exists()) {
35. tofile.delete();
36. }
37.// Dispatch.call(doc, "SaveAs", destFile, 17);
38. // 作为html格式保存到临时文件::参数 new Variant(8)其中8表示word转html;7表示word转txt;44表示Excel转html;17表示word转成pdf。
39. Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
40. toFileName, new Variant(17) }, new int[1]);
41. long end = System.currentTimeMillis();
42. System.out.println("转换完成..用时:" + (end - start) + "ms.");
43. } catch (Exception e) {
44. e.printStackTrace();
45. System.out.println("========Error:文档转换失败:" + e.getMessage());
46. }catch(Throwable t){
47. t.printStackTrace();
48. } finally {
49. // 关闭word
50. Dispatch.call(doc,"Close",false);
51. System.out.println("关闭文档");
52. if (app != null)
53. app.invoke("Quit", new Variant[] {});
54. }
55. //如果没有这句话,winword.exe进程将不会关闭
56. ComThread.Release();
57. return 1;
58. }
59. private static Document read(File xmlFile) throws DocumentException {
60. SAXReader saxReader = new SAXReader();
61. return saxReader.read(xmlFile);
62. }
63.// public String getDocPageSize(File file){
.// String pages = null;
65.// try{
66.// Document doc = read(file);
67.// List 68.// if(nodes != null && nodes.size() > 0){ 69.// pages = nodes.get(0).getText(); 70.// System.out.println("/////////////////"); 71.// System.out.println("该word文档的页数为:"+Integer.parseInt(pages)); 72.// System.out.println("/////////////////"); 73.// }else{ 74.// System.out.println("*********"); 75.// System.out.println("页面转换错误"); 76.// System.out.println("*********"); 77.// } 78.// }catch(Exception ex){ 79.// ex.printStackTrace(); 80.// } 81.// return pages; 82.// } 83. public int getDocPageSize(String filePath) throws Exception { 84. XWPFDocument docx = new XWPFDocument(POIXMLDocument.openPackage(filePath)); 85. int pages = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();//总页数 86. int wordCount = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getCharacters();// 忽略空格的总字符数 另外还有getCharactersWithSpaces()方法获取带空格的总字数。 87. System.out.println ("pages=" + pages + " wordCount=" + wordCount); 88. return pages; . } 90. 91. public static void main(String[] args) throws Exception { 92. Word2pdf d = new Word2pdf(); 93. System.err.println(d.getDocPageSize("d:\\\\exportWord.docx")); 94. d.wordToPDF("d:\\\1.docx", "d:\\\1.pdf"); 95. } 96. 97.} 可是能够完全运行的应该不可能,还要准备工作, 出错总结: 1.没引入jar包,这个错误我就不说了,jar包你可以去上网搜索下载,基本上所有人都可以避免.jacob.jar 2. 出现这个错误是因为缺少了jacob-1.18-x.dll(目前的最新版本,可在官网直接下载)这个东西,把jacob-1.18-x.dll复制到C:\\Program Files\\Java\\jdk1.6.0_17\jdk\\bin(项目中用到的jre和jdk)目录下即可。 注意:名字一定要改成和报错的时候一样的名字,差一点也不行。 3.由于出错没有截图,所以现在错误也不好去演示了,简单的就是下面的错误 com.jacob.com.ComFailException: Invoke of: SaveAs Source: Microsoft Word Description: 命令失败 出现这种错误网上也有很多解决办法:如下 Office版本使用2007,因为2007提供了一个加载项:Microsoft Save as PDF 或 XPS,可将文档另存为PDF格式。下载地址:http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=4D951911-3E7E-4AE6-B059-A2E79ED87041,安装即可使用。 ,或者你直接安装下office2013即可解决 4.转换的文档会显示修正页面,需加上 Dispatch.put(doc, "ShowRevisions", newVariant(false)); 5,提示命令文件是只读,无法运行 Dispatch.put(doc, "ShowRevisions", newVariant(false)); 打开文件时设置参数 Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { sfileName, new Variant(false),new Variant(false) }, newint[1]).toDispatch();下载本文