office文档转换为pdf
程序员文章站
2024-01-31 16:09:58
...
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);
}
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文档转换为pdf
-
[转]使用百度JS模板引擎生成PDF和Word文档
-
java将DOC、Docx、PPT、pptx和ODT文档转换为pdf文件
-
Office文档上传后实时转换为PDF格式_图片文件上传后实时裁剪_实现在线预览Office文档
-
php 怎么让office文档和pdf文件直接显示在网页中
-
Python实现将DOC文档转换为PDF的方法
-
java调用openoffice将office系列文档转换为PDF的示例方法
-
Python实现将DOC文档转换为PDF的方法
-
linux平台的office文档转pdf的实例(程序员的菜)
-
ASP.NET Core 2.0集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)