文件如何转换成pdf或html格式

来源:互联网 发布:阿里云域名解析时间 编辑:程序博客网 时间:2024/06/05 20:48

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
 * 1officeofficePDF 2jacob(java com bridge
 * javacom
 */
public static boolean convert2PDF(String inputFileString 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(inputFilepdfFile);
else if (suffix.equals("ppt"|| suffix.equals("pptx")) {
return officeToPdf.ppt2PDF(inputFilepdfFile);
else if (suffix.equals("xls"|| suffix.equals("xlsx")) {
return officeToPdf.excel2PDF(inputFilepdfFile);
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 inputFileString pdfFile) {
try {
// word
ActiveXComponent app = new ActiveXComponent("Word.Application");
// word
app.setProperty("Visible"false);
// word,Documents
Dispatch docs = app.getProperty("Documents").toDispatch();
// DocumentsOpenDocument
Dispatch doc = Dispatch.call(docs"Open"inputFilefalsetrue)
.toDispatch();
// DocumentSaveAspdf
/*
 * Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF
 * //wordpdf17 );
 */
Dispatch.call(doc"ExportAsFixedFormat"pdfFilewdFormatPDF // wordpdf17
);
// 
Dispatch.call(doc"Close"false);
// word
app.invoke("Quit"0);
return true;
catch (Exception e) {
return false;
finally {
ComThread.Release();
}
}
public boolean excel2PDF(String inputFileString pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent("Excel.Application");
app.setProperty("Visible"false);
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.call(excels"Open"inputFilefalse,
true).toDispatch();
Dispatch.call(excel"ExportAsFixedFormat"xlTypePDFpdfFile);
Dispatch.call(excel"Close"false);
app.invoke("Quit");
return true;
catch (Exception e) {
return false;
finally {
ComThread.Release();
}
}
public boolean ppt2PDF(String inputFileString pdfFile) {
try {
ActiveXComponent app = new ActiveXComponent(
"PowerPoint.Application");
// app.setProperty("Visible", msofalse);
Dispatch ppts = app.getProperty("Presentations").toDispatch();
Dispatch ppt = Dispatch.call(ppts"Open"inputFiletrue,// ReadOnly
true,// Untitled
false// WithWindow
).toDispatch();
Dispatch.call(ppt"SaveAs"pdfFileppSaveAsPDF);
Dispatch.call(ppt"Close");
app.invoke("Quit");
return true;
catch (Exception e) {
return false;
finally {
ComThread.Release();
}
}
}

4、office文件转html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 1officeofficePDF
 * 2jacob(java com bridge javacom
 */
public static boolean convert2HTML(String inputFileString 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(inputFilepdfFile);
else if (suffix.equals("xls"|| suffix.equals("xlsx")) {
return officeToHtml.excel2HTML(inputFilepdfFile);
else {
System.out.println("!");
return false;
}
}
public static String getFileSufix(String fileName) {
int splitIndex = fileName.lastIndexOf(".");
return fileName.substring(splitIndex + 1);
}
/**
 * WORDHTML
 * 
 * @param docfile
 *            WORD
 * @param htmlfile
 *            HTML
 */
public boolean word2HTML(String docfileString 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[] { docfilefalsetrue }, new int[1])
.toDispatch();
Dispatch.invoke(doc"SaveAs"Dispatch.Methodnew Object[] {
htmlfileWORD_HTML }, new int[1]);
Dispatch.call(doc"Close"false);
app.invoke("Quit"0);
return true;
catch (Exception e) {
return false;
finally {
ComThread.Release();
}
}
/**
 * EXCELHTML
 * 
 * @param xlsfile
 *            EXCEL
 * @param htmlfile
 *            HTML
 */
public boolean excel2HTML(String xlsfileString 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[] { xlsfilefalsetrue }, new int[1])
.toDispatch();
Dispatch.invoke(excel"SaveAs"Dispatch.Methodnew Object[] {
htmlfileEXCEL_HTML }, new int[1]);
Dispatch.call(excel"Close"false);
app.invoke("Quit");
return true;
catch (Exception e) {
return false;
finally {
ComThread.Release();
}
}
}

附件源码:

下载地址:http://pan.baidu.com/s/1c1PnkS4

 

0 0
原创粉丝点击