1、使用jacob插件

2、使用方法

1)于word、ppt等上传文件转换为PDF格式文件的环境搭建,步骤如下:
① 首先电脑要先安装office软件(不可以是WPS软件)
② 需要把jacob.dll文件复制到JDK的bin目录下面,否则无法调用转换为PDF的功能。

2)使用的服务器上必须安装有office软件,因为原理是调用office的pdf转换器来实现的。

3)必须也要有PDF软件,因为office要通过调用本地的pdf软件来实现格式的转换。

3、office文件转PDF


  1. import java.io.File;
  2. import com.jacob.activeX.ActiveXComponent;
  3. import com.jacob.com.ComThread;
  4. import com.jacob.com.Dispatch;
  5. public class OfficeToPdf {
  6. private static final int wdFormatPDF = 17;
  7. private static final int xlTypePDF = 0;
  8. private static final int ppSaveAsPDF = 32;
  9. public static void main(String[] args) {
  10. convert2PDF("I:\\使用方法.txt","I:\\使用方法.pdf");
  11. }
  12. /*
  13. * 转换生存PDF文件,支持格式 doc docx txt ppt pptx xls xlsx
  14. * 1、需安装office软件,并有将office转化为PDF的插件 2、需有jacob(java com bridge),
  15. * java与com组件之间的桥梁
  16. */
  17. public static boolean convert2PDF(String inputFile, String pdfFile) {
  18. String suffix = getFileSufix(inputFile);
  19. File file = new File(inputFile);
  20. if (!file.exists()) {
  21. System.out.println("文件不存在!");
  22. return false;
  23. }
  24. if (suffix.equals("pdf")) {
  25. System.out.println("PDF not need to convert!");
  26. return false;
  27. }
  28. OfficeToPdf officeToPdf = new OfficeToPdf();
  29. if (suffix.equals("doc") || suffix.equals("docx")
  30. || suffix.equals("txt")) {
  31. return officeToPdf.word2PDF(inputFile, pdfFile);
  32. } else if (suffix.equals("ppt") || suffix.equals("pptx")) {
  33. return officeToPdf.ppt2PDF(inputFile, pdfFile);
  34. } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
  35. return officeToPdf.excel2PDF(inputFile, pdfFile);
  36. } else {
  37. System.out.println("文件格式不支持转换!");
  38. return false;
  39. }
  40. }
  41. public static String getFileSufix(String fileName) {
  42. int splitIndex = fileName.lastIndexOf(".");
  43. return fileName.substring(splitIndex + 1);
  44. }
  45. public boolean word2PDF(String inputFile, String pdfFile) {
  46. try {
  47. // 打开word应用程序
  48. ActiveXComponent app = new ActiveXComponent("Word.Application");
  49. // 设置word不可见
  50. app.setProperty("Visible", false);
  51. // 获得word中所有打开的文档,返回Documents对象
  52. Dispatch docs = app.getProperty("Documents").toDispatch();
  53. // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
  54. Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
  55. .toDispatch();
  56. // 调用Document对象的SaveAs方法,将文档保存为pdf格式
  57. /*
  58. * Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF
  59. * //word保存为pdf格式宏,值为17 );
  60. */
  61. Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF // word保存为pdf格式宏,值为17
  62. );
  63. // 关闭文档
  64. Dispatch.call(doc, "Close", false);
  65. // 关闭word应用程序
  66. app.invoke("Quit", 0);
  67. return true;
  68. } catch (Exception e) {
  69. return false;
  70. } finally {
  71. ComThread.Release();
  72. }
  73. }
  74. public boolean excel2PDF(String inputFile, String pdfFile) {
  75. try {
  76. ActiveXComponent app = new ActiveXComponent("Excel.Application");
  77. app.setProperty("Visible", false);
  78. Dispatch excels = app.getProperty("Workbooks").toDispatch();
  79. Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,
  80. true).toDispatch();
  81. Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
  82. Dispatch.call(excel, "Close", false);
  83. app.invoke("Quit");
  84. return true;
  85. } catch (Exception e) {
  86. return false;
  87. } finally {
  88. ComThread.Release();
  89. }
  90. }
  91. public boolean ppt2PDF(String inputFile, String pdfFile) {
  92. try {
  93. ActiveXComponent app = new ActiveXComponent(
  94. "PowerPoint.Application");
  95. // app.setProperty("Visible", msofalse);
  96. Dispatch ppts = app.getProperty("Presentations").toDispatch();
  97. Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly
  98. true,// Untitled指定文件是否有标题
  99. false// WithWindow指定文件是否可见
  100. ).toDispatch();
  101. Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);
  102. Dispatch.call(ppt, "Close");
  103. app.invoke("Quit");
  104. return true;
  105. } catch (Exception e) {
  106. return false;
  107. } finally {
  108. ComThread.Release();
  109. }
  110. }
  111. }

4、office文件转html


  1. import java.io.File;
  2. import com.jacob.activeX.ActiveXComponent;
  3. import com.jacob.com.ComThread;
  4. import com.jacob.com.Dispatch;
  5. public class OfficeToHtml {
  6. public static final int WORD_HTML = 8;
  7. public static final int WORD_TXT = 7;
  8. public static final int EXCEL_HTML = 44;
  9. public static final int PPT_HTML = 44;
  10. public static void main(String[] args) {
  11. convert2HTML("I:\\使用方法.txt","I:\\使用方法.html");
  12. }
  13. /*
  14. * 转换生存PDF文件,支持格式 doc docx txt xls xlsx 1、需安装office软件,并有将office转化为PDF的插件
  15. * 2、需有jacob(java com bridge), java与com组件之间的桥梁
  16. */
  17. public static boolean convert2HTML(String inputFile, String pdfFile) {
  18. String suffix = getFileSufix(inputFile);
  19. File file = new File(inputFile);
  20. if (!file.exists()) {
  21. System.out.println("文件不存在!");
  22. return false;
  23. }
  24. OfficeToHtml officeToHtml = new OfficeToHtml();
  25. if (suffix.equals("doc") || suffix.equals("docx")
  26. || suffix.equals("txt")) {
  27. return officeToHtml.word2HTML(inputFile, pdfFile);
  28. } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
  29. return officeToHtml.excel2HTML(inputFile, pdfFile);
  30. } else {
  31. System.out.println("文件格式不支持转换!");
  32. return false;
  33. }
  34. }
  35. public static String getFileSufix(String fileName) {
  36. int splitIndex = fileName.lastIndexOf(".");
  37. return fileName.substring(splitIndex + 1);
  38. }
  39. /**
  40. * WORD转HTML
  41. *
  42. * @param docfile
  43. *            WORD文件全路径
  44. * @param htmlfile
  45. *            转换后HTML存放路径
  46. */
  47. public boolean word2HTML(String docfile, String htmlfile) {
  48. ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
  49. try {
  50. app.setProperty("Visible", false);
  51. app.setProperty("DisplayAlerts", false);// 设置不显示弹出覆盖警告
  52. Dispatch docs = app.getProperty("Documents").toDispatch();
  53. Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
  54. new Object[] { docfile, false, true }, new int[1])
  55. .toDispatch();
  56. Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
  57. htmlfile, WORD_HTML }, new int[1]);
  58. Dispatch.call(doc, "Close", false);
  59. app.invoke("Quit", 0);
  60. return true;
  61. } catch (Exception e) {
  62. return false;
  63. } finally {
  64. ComThread.Release();
  65. }
  66. }
  67. /**
  68. * EXCEL转HTML
  69. *
  70. * @param xlsfile
  71. *            EXCEL文件全路径
  72. * @param htmlfile
  73. *            转换后HTML存放路径
  74. */
  75. public boolean excel2HTML(String xlsfile, String htmlfile) {
  76. ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动exel
  77. try {
  78. app.setProperty("Visible", false);
  79. app.setProperty("DisplayAlerts", false);// 设置不显示弹出覆盖警告
  80. Dispatch excels = app.getProperty("Workbooks").toDispatch();
  81. Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method,
  82. new Object[] { xlsfile, false, true }, new int[1])
  83. .toDispatch();
  84. Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
  85. htmlfile, EXCEL_HTML }, new int[1]);
  86. Dispatch.call(excel, "Close", false);
  87. app.invoke("Quit");
  88. return true;
  89. } catch (Exception e) {
  90. return false;
  91. } finally {
  92. ComThread.Release();
  93. }
  94. }
  95. }

附件源码:

下载地址:https://gitee.com/KingXin666/FormatToPDFandHTML

文件如何转换成pdf或html格式的更多相关文章

  1. DWG文件怎么转换成PDF格式

    在CAD中,设计师们绘制的图纸都是以dwg文件来进行保存的.Dwg文件是不能够直接进行打开查看的,就需要将其格式进行转换一下.将dwg文件转换为PDF格式的进行查看.那具体要怎么来进行操作呢?下面小编 ...

  2. 怎样把Linux的私钥文件id_rsa转换成putty的ppk格式

    在Linux VPS下产生的私钥文件putty是不认识的,putty只认识自己的ppk格式,要在这两种格式之间转换,需要PuTTYgen这个程序. puttygen是putty的配套程序,putty的 ...

  3. vue文件流转换成pdf预览(pdf.js+iframe)

    参考文档:https://www.jianshu.com/p/242525315bf6 PDFJS: https://mozilla.github.io/pdf.js/     支持获取文件流到客户端 ...

  4. dvi文件和将dvi文件转换成pdf格式

    dvi文件和将dvi文件转换成pdf格式 Latex只能把tex文件编译成dvi文件, 在cmd 中: 使用xdvi查看dvi格式的文件 若用texstudio编辑tex文件,则可直接将已编译成功的. ...

  5. 【文件】使用jacob将word转换成pdf格式

    使用jacob将word转换成pdf格式   1.需要安装word2007或以上版本,若安装07版本学确保该版本已安装2downbank0204MicrosoftSaveasPDF_ XPS,否则安装 ...

  6. C# 将PowerPoint文件转换成PDF文件

    PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...

  7. ABBYY如何把图片转换成pdf格式

    在制作工作文件的时候,有时候会遇到需要进行文件格式转换的情况,比较常见的文件格式转换就包含了Office与pdf格式之间的转换.但除此之外,图片与pdf格式也是可以进行转换的,那么图片要怎么操作,才能 ...

  8. Linux不用使用软件把纯文本文档转换成PDF文件的方法

    当你有一大堆文本文件要维护的时候,把它们转换成PDF文档会好一些.比如,PDF更适合打印,因为PDF文档有预定义布局.除此之外,还可以减少文档被意外修改的风险. 要将文本文件转换成PDF格式,你要按照 ...

  9. python3将docx转换成pdf,html文件,pdf转doc文件

    直接上代码 # -*- encoding:utf-8 -*- """ author:lgh 简单的doc转pdf,html,pdf转doc脚本 依赖库pdfminer3k ...

随机推荐

  1. Day11作业及默写

    1.写函数,传入n个数,返回字典{'max':最大值,'min':最小值} 例如:min_max(2,5,7,8,4) 返回:{'max':8,'min':2}(此题用到max(),min()内置函数 ...

  2. ORACLE提示表名无效

    在创建ORACLE数据库时,创建表 提示表名无效 请查看数据库表名是否出现了小写字母或者关键字,如USER

  3. 第76课 最小生成树(Prim)

    图解: 添加prim函数: #ifndef GRAPH_H #define GRAPH_H #include "Object.h" #include "SharedPoi ...

  4. canvas默认是黑色全透明,不是白色全透明。

  5. Android Hook框架adbi源码浅析(一)

    adbi(The Android Dynamic Binary Instrumentation Toolkit)是一个Android平台通用hook框架,基于动态库注入与inline hook技术实现 ...

  6. Android动态添加Device Admin权限

    /********************************************************************** * Android动态添加Device Admin权限 ...

  7. search的 制作

    <meta charset="utf-8">  <title>search的制作</title> <style type="te ...

  8. linux rpm yum 安装 软件

    rpm 安装: 1.rpm包的了解:  rpm  安装  升级  删除 rpm -ivh  ****.rpm   安装 rpm -Uvh  ****.rpm  升级 rpm -e name    删除 ...

  9. spring IOC简单分析

    Spring IOC 体系结构 BeanFactory(BeanFactory 里只对 IOC 容器的基本行为作了定义,根本不关心你的 bean 是如何定义怎样加载的.正如我们只关心工厂里得到什么的产 ...

  10. AangularJS入门总结二

    双向数据绑定:在Mode(JS)中改变数据,而这些变动立刻就会自动出现在View上,反之亦然.一方面可以做到model变化驱动了DOM中元素变化,另一方面也可以做到DOM元素的变化也会影响到Model ...