zoukankan      html  css  js  c++  java
  • 文件如何转换成pdf或html格式

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

    4、office文件转html

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

    附件源码:

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

  • 相关阅读:
    java多线程设计模式
    Java横向、纵向合并图片
    Oracle数据库当前连接数、最大连接数的查询与设置
    oracle获取一段时间内所有的小时、天、月
    Struts2+Spring3+Mybatis3开发环境搭建
    Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源方法
    spring+mybatis 多数据源切换
    Java与WCF交互(一):Java客户端调用WCF服务
    使用axis2进行WebService的开发
    axis2 WebService的发布与调用
  • 原文地址:https://www.cnblogs.com/yangcx666/p/8723883.html
Copyright © 2011-2022 走看看