文件如何转换成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
- import java.io.File;
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.ComThread;
- import com.jacob.com.Dispatch;
- public class OfficeToPdf {
- private static final int wdFormatPDF = 17;
- private static final int xlTypePDF = 0;
- private static final int ppSaveAsPDF = 32;
- public static void main(String[] args) {
- convert2PDF("I:\\使用方法.txt","I:\\使用方法.pdf");
- }
- /*
- * 转换生存PDF文件,支持格式 doc docx txt ppt pptx xls xlsx
- * 1、需安装office软件,并有将office转化为PDF的插件 2、需有jacob(java com bridge),
- * java与com组件之间的桥梁
- */
- public static boolean convert2PDF(String inputFile, String pdfFile) {
- String suffix = getFileSufix(inputFile);
- File file = new File(inputFile);
- if (!file.exists()) {
- System.out.println("文件不存在!");
- return false;
- }
- if (suffix.equals("pdf")) {
- System.out.println("PDF not need to convert!");
- return false;
- }
- OfficeToPdf officeToPdf = new OfficeToPdf();
- if (suffix.equals("doc") || suffix.equals("docx")
- || suffix.equals("txt")) {
- return officeToPdf.word2PDF(inputFile, pdfFile);
- } else if (suffix.equals("ppt") || suffix.equals("pptx")) {
- return officeToPdf.ppt2PDF(inputFile, pdfFile);
- } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
- return officeToPdf.excel2PDF(inputFile, pdfFile);
- } else {
- System.out.println("文件格式不支持转换!");
- return false;
- }
- }
- public static String getFileSufix(String fileName) {
- int splitIndex = fileName.lastIndexOf(".");
- return fileName.substring(splitIndex + 1);
- }
- public boolean word2PDF(String inputFile, String pdfFile) {
- try {
- // 打开word应用程序
- ActiveXComponent app = new ActiveXComponent("Word.Application");
- // 设置word不可见
- app.setProperty("Visible", false);
- // 获得word中所有打开的文档,返回Documents对象
- Dispatch docs = app.getProperty("Documents").toDispatch();
- // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
- Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
- .toDispatch();
- // 调用Document对象的SaveAs方法,将文档保存为pdf格式
- /*
- * Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF
- * //word保存为pdf格式宏,值为17 );
- */
- Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF // word保存为pdf格式宏,值为17
- );
- // 关闭文档
- Dispatch.call(doc, "Close", false);
- // 关闭word应用程序
- app.invoke("Quit", 0);
- return true;
- } catch (Exception e) {
- return false;
- } finally {
- ComThread.Release();
- }
- }
- public boolean excel2PDF(String inputFile, String pdfFile) {
- try {
- ActiveXComponent app = new ActiveXComponent("Excel.Application");
- app.setProperty("Visible", false);
- Dispatch excels = app.getProperty("Workbooks").toDispatch();
- Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,
- true).toDispatch();
- Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
- Dispatch.call(excel, "Close", false);
- app.invoke("Quit");
- return true;
- } catch (Exception e) {
- return false;
- } finally {
- ComThread.Release();
- }
- }
- public boolean ppt2PDF(String inputFile, String pdfFile) {
- try {
- ActiveXComponent app = new ActiveXComponent(
- "PowerPoint.Application");
- // app.setProperty("Visible", msofalse);
- Dispatch ppts = app.getProperty("Presentations").toDispatch();
- Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly
- true,// Untitled指定文件是否有标题
- false// WithWindow指定文件是否可见
- ).toDispatch();
- Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);
- Dispatch.call(ppt, "Close");
- app.invoke("Quit");
- return true;
- } catch (Exception e) {
- return false;
- } finally {
- ComThread.Release();
- }
- }
- }
4、office文件转html
- import java.io.File;
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.ComThread;
- import com.jacob.com.Dispatch;
- public class OfficeToHtml {
- public static final int WORD_HTML = 8;
- public static final int WORD_TXT = 7;
- public static final int EXCEL_HTML = 44;
- public static final int PPT_HTML = 44;
- public static void main(String[] args) {
- convert2HTML("I:\\使用方法.txt","I:\\使用方法.html");
- }
- /*
- * 转换生存PDF文件,支持格式 doc docx txt xls xlsx 1、需安装office软件,并有将office转化为PDF的插件
- * 2、需有jacob(java com bridge), java与com组件之间的桥梁
- */
- public static boolean convert2HTML(String inputFile, String pdfFile) {
- String suffix = getFileSufix(inputFile);
- File file = new File(inputFile);
- if (!file.exists()) {
- System.out.println("文件不存在!");
- return false;
- }
- OfficeToHtml officeToHtml = new OfficeToHtml();
- if (suffix.equals("doc") || suffix.equals("docx")
- || suffix.equals("txt")) {
- return officeToHtml.word2HTML(inputFile, pdfFile);
- } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
- return officeToHtml.excel2HTML(inputFile, pdfFile);
- } else {
- System.out.println("文件格式不支持转换!");
- return false;
- }
- }
- public static String getFileSufix(String fileName) {
- int splitIndex = fileName.lastIndexOf(".");
- return fileName.substring(splitIndex + 1);
- }
- /**
- * WORD转HTML
- *
- * @param docfile
- * WORD文件全路径
- * @param htmlfile
- * 转换后HTML存放路径
- */
- public boolean word2HTML(String docfile, String htmlfile) {
- ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
- try {
- app.setProperty("Visible", false);
- app.setProperty("DisplayAlerts", false);// 设置不显示弹出覆盖警告
- Dispatch docs = app.getProperty("Documents").toDispatch();
- Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
- new Object[] { docfile, false, true }, new int[1])
- .toDispatch();
- Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
- htmlfile, WORD_HTML }, new int[1]);
- Dispatch.call(doc, "Close", false);
- app.invoke("Quit", 0);
- return true;
- } catch (Exception e) {
- return false;
- } finally {
- ComThread.Release();
- }
- }
- /**
- * EXCEL转HTML
- *
- * @param xlsfile
- * EXCEL文件全路径
- * @param htmlfile
- * 转换后HTML存放路径
- */
- public boolean excel2HTML(String xlsfile, String htmlfile) {
- ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动exel
- try {
- app.setProperty("Visible", false);
- app.setProperty("DisplayAlerts", false);// 设置不显示弹出覆盖警告
- Dispatch excels = app.getProperty("Workbooks").toDispatch();
- Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method,
- new Object[] { xlsfile, false, true }, new int[1])
- .toDispatch();
- Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
- htmlfile, EXCEL_HTML }, new int[1]);
- Dispatch.call(excel, "Close", false);
- app.invoke("Quit");
- return true;
- } catch (Exception e) {
- return false;
- } finally {
- ComThread.Release();
- }
- }
- }
附件源码:
文件如何转换成pdf或html格式的更多相关文章
- DWG文件怎么转换成PDF格式
在CAD中,设计师们绘制的图纸都是以dwg文件来进行保存的.Dwg文件是不能够直接进行打开查看的,就需要将其格式进行转换一下.将dwg文件转换为PDF格式的进行查看.那具体要怎么来进行操作呢?下面小编 ...
- 怎样把Linux的私钥文件id_rsa转换成putty的ppk格式
在Linux VPS下产生的私钥文件putty是不认识的,putty只认识自己的ppk格式,要在这两种格式之间转换,需要PuTTYgen这个程序. puttygen是putty的配套程序,putty的 ...
- vue文件流转换成pdf预览(pdf.js+iframe)
参考文档:https://www.jianshu.com/p/242525315bf6 PDFJS: https://mozilla.github.io/pdf.js/ 支持获取文件流到客户端 ...
- dvi文件和将dvi文件转换成pdf格式
dvi文件和将dvi文件转换成pdf格式 Latex只能把tex文件编译成dvi文件, 在cmd 中: 使用xdvi查看dvi格式的文件 若用texstudio编辑tex文件,则可直接将已编译成功的. ...
- 【文件】使用jacob将word转换成pdf格式
使用jacob将word转换成pdf格式 1.需要安装word2007或以上版本,若安装07版本学确保该版本已安装2downbank0204MicrosoftSaveasPDF_ XPS,否则安装 ...
- C# 将PowerPoint文件转换成PDF文件
PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...
- ABBYY如何把图片转换成pdf格式
在制作工作文件的时候,有时候会遇到需要进行文件格式转换的情况,比较常见的文件格式转换就包含了Office与pdf格式之间的转换.但除此之外,图片与pdf格式也是可以进行转换的,那么图片要怎么操作,才能 ...
- Linux不用使用软件把纯文本文档转换成PDF文件的方法
当你有一大堆文本文件要维护的时候,把它们转换成PDF文档会好一些.比如,PDF更适合打印,因为PDF文档有预定义布局.除此之外,还可以减少文档被意外修改的风险. 要将文本文件转换成PDF格式,你要按照 ...
- python3将docx转换成pdf,html文件,pdf转doc文件
直接上代码 # -*- encoding:utf-8 -*- """ author:lgh 简单的doc转pdf,html,pdf转doc脚本 依赖库pdfminer3k ...
随机推荐
- 绕过Snoopy的记录功能
不讲原理,感兴趣请看http://blog.rchapman.org/posts/Bypassing_snoopy_logging/,这个只适合老版本内核的Linux 查看是否有snoopy加载了 l ...
- golang快速扫描
利用golang的并发优势快速扫描端口 Scanner startIp [endIp] port thread package main import ( "flag" " ...
- vue安装流程
一.环境搭建 vue推荐开发环境: Node.js: javascript运行环境(runtime),不同系统直接运行各种编程语言 npm: Nodejs下的包管理器.由于国内使用npm会很慢,这 ...
- 【转载】 PyTorch学习之六个学习率调整策略
原文地址: https://blog.csdn.net/shanglianlm/article/details/85143614 ----------------------------------- ...
- jsp-servlet(2)响应HTML文档-书籍管理系统
基础知识预备: 目标: 构建一个书籍管理系统,实现以下功能. 功能: 1 图书信息查询,(查) 2 书籍管理:添加书籍 3 书籍管理:修改书籍信息 4 书籍管理:删除书籍 一.预备工作 Book{ ...
- tf.contrib.rnn.core_rnn_cell.BasicLSTMCell should be replaced by tf.contrib.rnn.BasicLSTMCell.
For Tensorflow 1.2 and Keras 2.0, the line tf.contrib.rnn.core_rnn_cell.BasicLSTMCell should be repl ...
- 关于Adaboost——样本抽样的权值的实际意义
看这篇文章的前提:已经看了PRML中的Adaboost的算法流程 看懂下面的内容必须牢牢记住:Adaboost使用的误差函数是指数误差 文章主要目的:理解样本抽样的权值是为什么那样变化的. 得出的结论 ...
- Spark各个版本新特性
后续会添加spark生态系统中各个组件的兼容支持情况... Spark2.0.0 * 2016-07-27正式发布 * 它是2.x版本线的上的第一个版本. * 300位contributors的超过2 ...
- Blender 作的鸭脖
鸭脖...https://www.youtube.com/watch?v=JS8V4_Ncn0w 新建一段骨骼,编辑,[E]挤出生成3段, 或者[W]细分回到物体模式,选中骨骼,属性编辑器\物体数据\ ...
- 《DSP using MATLAB》Problem 5.9
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...