officetopdf

来源:互联网 发布:部落冲突城墙升级数据 编辑:程序博客网 时间:2024/05/17 20:31

OfficeToPdf.java



package com.ekingstar.zcgl.bean;


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


/**
 * @author DXJ


 * @describe 进行PDF文件的各种转换
 * @serialData 2013-10-30 
 */


public class OfficeToPdf {


static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// word转PDF 格式
static final int ppSaveAsPDF = 32;// ppt 转PDF 格式


public static void word2pdf(String inputFile, String pdfFile) {
ActiveXComponent app = null;
Dispatch doc = null;
try {
// 打开word应用程序
app = new ActiveXComponent("Word.Application");
// 设置word不可见
app.setProperty("Visible", false);
// 获得word中所有打开的文档,返回Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
// 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
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
);
} catch (Exception e) {
System.out.println("word2pdf出现错误");
} finally {
// 关闭文档
if (doc != null) {
Dispatch.call(doc, "Close", false);
}
// 关闭word应用程序
if (app != null) {
app.invoke("Quit", 0);
app = null;
}


}


}


// 转换文档到PDF
public static void ppt2pdf(String source, String target) {
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Powerpoint.Application"); // 启动PPT
// app.setProperty("Visible", new Variant(false)); PPT转换时不允许隐藏


Dispatch presentations = app.getProperty("Presentations").toDispatch();
Dispatch presentation = Dispatch.call(presentations, //
"Open", source, // FileName
true, // ReadOnly
true, // Untitled 指定文件是否有标题。
false // WithWindow 指定文件是否可见。
).toDispatch();


File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(presentation, //
"SaveAs", //
target, // FileName
ppSaveAsPDF);


Dispatch.call(presentation, "Close");
long end = System.currentTimeMillis();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (app != null)
app.invoke("Quit");
}
}


public static void excel2pdf(String source, String target) {
long start = System.currentTimeMillis();
ActiveXComponent app = null;
Dispatch workbook = null;
try {
app = new ActiveXComponent("Excel.Application"); // 启动excel
app.setProperty("Visible", false); // 隐藏转换窗口
Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,
new Object[] { source, new Variant(false), new Variant(false) }, new int[3]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method,
new Object[] { target, new Variant(57), new Variant(false), new Variant(57), new Variant(57),
new Variant(false), new Variant(true), new Variant(57), new Variant(true),
new Variant(true), new Variant(true) },
new int[1]);
// long end = System.currentTimeMillis();
} catch (Exception e) {
// e.printStackTrace();
System.out.println("excel2pdf出现错误");
} finally {
if (workbook != null) {
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
}
if (app != null) {
app.invoke("Quit", new Variant[] {});
app = null;
}
ComThread.Release();
System.gc();


}
}


// pdf转swf,
public static int pdf2swf(String swfToolsPath, String sourceFile, String destFile) {
// 目标路径不存在则建立目标路径
File dest = new File(destFile);
if (!dest.getParentFile().exists())
dest.getParentFile().mkdirs();


// 源文件不存在则返回 -1
File source = new File(sourceFile);
if (!source.exists())
return -1;


Reader reader = null;


// 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
try {
// 调用pdf2swf命令进行转换,windows下的格式是:pdf2swf -i - sourceFilePath.pdf -o
// destFilePath.swf
// LINUX下的格式是:/usr/bin/pdf2swf -o cwh1.swf -f cwh1.pdf -s
// languagedir=/usr/local/share/xpdf/chinese-simplified -T 9 -t -s
// storeallcharacters -s poly2bitmap
// 如果文明名里有空格,命令行里执行时会找不到文件
if (sourceFile.indexOf(" ") >= 0)
sourceFile = "\"" + sourceFile + "\"";
if (destFile.indexOf(" ") >= 0)
destFile = "\"" + destFile + "\"";
String command = swfToolsPath + " -i " + sourceFile + " -o " + destFile + " -T 9 -s languagedir=C:/xpdf/xpdf-chinese-simplified";
//String command = swfToolsPath + " " + sourceFile + " " + destFile + " -s languagedir=C:/xpdf/xpdf-chinese-simplified";
//String command = swfToolsPath + " -o " + destFile + " -t " + sourceFile + " -s languagedir=C:/xpdf/xpdf-chinese-simplified";
//System.out.println(command);
Process pro = Runtime.getRuntime().exec(command);


reader = new InputStreamReader(pro.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
while (bufferedReader.readLine() != null)
;
pro.waitFor();
return pro.exitValue();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


return 1;
}






public static void main(String[] args) {
excel2pdf(("d:" + File.separator+ "1.docx"),("e:" + File.separator+ "xind.docx"));
}


}


原创粉丝点击