import java.io.File;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
/**
* 这是一个工具类,主要是为了使Office2003-2007全部格式的文档
* (.doc|.docx|.xls|.xlsx|.ppt|.pptx)转化为pdf文件
*/
public class OfficeToPDF {
public static final String OFFICE_DOC = "doc";
public static final String OFFICE_DOCX = "docx";
public static final String OFFICE_XLS = "xls";
public static final String OFFICE_XLSX = "xlsx";
public static final String OFFICE_PPT = "ppt";
public static final String OFFICE_PPTX = "pptx";
public static final String OFFICE_TO_PDF = "pdf";
/**
* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br>
*
* @param inputFilePath
* 源文件路径,如:"e:/test.docx"
* @param outputFilePath
* 目标文件路径,如:"e:/test_docx.pdf"
* @return
*/
public static boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
return office2pdf(inputFilePath, outputFilePath);
}
/**
* 获取OpenOffice.org 3的安装目录
*
* @return OpenOffice.org 3的安装目录
*/
public static String getOfficeHome() {
String path = PropertiesUtil.getValueByPropertyName(
ClassLoader.getSystemResourceAsStream("toPdf.properties"),
"OPENOFFICE_PATH");
return path;
}
/**
* 连接OpenOffice.org 并且启动OpenOffice.org
*
* @return
*/
public static OfficeManager getOfficeManager() {
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
// 获取OpenOffice.org 3的安装目录
String officeHome = getOfficeHome();
config.setOfficeHome(officeHome);
// 启动OpenOffice的服务
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
return officeManager;
}
/**
* 转换文件
*
* @param inputFile
* @param outputFilePath_end
* @param inputFilePath
* @param outputFilePath
* @param converter
*/
public static void converterFile(File inputFile, String outputFilePath_end,
String inputFilePath, String outputFilePath,
OfficeDocumentConverter converter) {
File outputFile = new File(outputFilePath_end);
// 假如目标路径不存在,则新建该路径
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
converter.convert(inputFile, outputFile);
}
/**
* 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
*
* @param inputFilePath
* 源文件路径,如:"e:/test.docx"
* @param outputFilePath
* 目标文件路径,如:"e:/test_docx.pdf"
* @return
*/
public static boolean office2pdf(String inputFilePath, String outputFilePath) {
boolean flag = false;
OfficeManager officeManager = getOfficeManager();
// 连接OpenOffice
OfficeDocumentConverter converter = new OfficeDocumentConverter(
officeManager);
if (null != inputFilePath) {
File inputFile = new File(inputFilePath);
// 判断目标文件路径是否为空
if (null == outputFilePath) {
// 转换后的文件路径
String outputFilePath_end = getOutputFilePath(inputFilePath);
if (inputFile.exists()) {// 找不到源文件, 则返回
converterFile(inputFile, outputFilePath_end, inputFilePath,
outputFilePath, converter);
flag = true;
}
} else {
if (inputFile.exists()) {// 找不到源文件, 则返回
converterFile(inputFile, outputFilePath, inputFilePath,
outputFilePath, converter);
flag = true;
}
}
officeManager.stop();
} else {
flag = false;
}
return flag;
}
/**
* 获取输出文件
*
* @param inputFilePath
* @return
*/
public static String getOutputFilePath(String inputFilePath) {
String outputFilePath = inputFilePath.replaceAll("."
+ getPostfix(inputFilePath), ".pdf");
return outputFilePath;
}
/**
* 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"
*
* @param inputFilePath
* @return
*/
public static String getPostfix(String inputFilePath) {
return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1);
}
分享到:
相关推荐
将office文档(word,excel, ppt)转换为PDF的小工具。PC上运行,需要安装微软的。net框架
一个JavaEE服务Office文档转换为PDF
Gotenberg:用于将Markdown文件,HTML文件和Office文档转换为PDF的无状态API
Office文件转PDF、XPS工具,转换快速准确,支持Word、Excel、PowerPoint转换,支持Office2007、Office2010,微软出品,品质保证。
易语言OFFICE文档转换成PDF源码,OFFICE文档转换成PDF,拖放对象注册,拖放对象撤销,WindowProc,拖放对象得到文件,转换到HTML,转换到HTML路径,取文件类型_程序,SetWindowLongA,CallWindowProcA,DragAcceptFiles,...
Gotenberg一个由Docker驱动的无状态API,用于将HTML,Markdown和Office文档转换为PDF。 文档·在TheCodingMachine上,我们构建了许多Web应用程序(内联网,外联网等)。whit Gotenberg一个由Docker驱动的无状态API,...
易语言OFFICE文档转换成PDF源码。@资源源码站。
用于将office文档转换为pdf格式文档,进行在线预览操作
Office批量打印精灵支持Word、Excel、powerpoint等文档的批量打印,且可使用虚拟打印机,将多个Office文档转换为pdf文件。 支持多种格式Office文档的批量打印,支持Excel格式:xls,xlsx;支持Powerpoint格式:ppt,...
OFFICE文档转换成PDF.rar
Docker驱动的无状态API,用于将HTML,Markdown和Office文档转换为PDF。 · 在TheCodingMachine,我们构建了许多Web应用程序(内联网,外部网等),这些应用程序需要从各种来源生成PDF。 每次,我们都通过将解决方案...
内含office转pdf的工具类jar包以及验证文件,可以轻松将word,excel,ppt转换为pdf格式,便于在网页上显示
霄鹞Office文档批量转PDF助手是由霄鹞软件工作室推出的一款批量将Office文档转换到PDF文档的应用软件。软件界面简洁,使用简单方便,特别适合将众多Office文档(Word/Excel/PPT)一次性批量转成PDF文档,是您办公...
易语言源码易语言OFFICE文档转换成PDF源码.rar
office文档转化为pdf文件 c++代码-office to pdf (包含WORD EXCEL POWPOINT) (office document into a pdf file c ++ code -office to pdf (containing WORD EXCEL POWPOINT))
很好用的office 文档转换为pdf的软件。值得收藏和使用。
Office批量打印精灵是一款Office文档批量打印软件,支持Word、Excel、Powerpoint等文档的批量打印,且可使用虚拟打印机,将多个Office文档转换为pdf文件的办公必备小工具。 软件特色 1.支持多种格式Office文档的...
1、 Word转换为Pdf,程序很多,但转换后的Pdf,还可以复制,虽可以加密禁止复制,但市场上太多的Pdf,还可以把Pdf转换Word等其他格式,很容易被别人利用和剽窃,如何彻底的自己的Word文档,可以被人浏览,查看,而不...
通过Jcom调用JNI将Office文档转换Pdf