package util;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Pattern;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
/**
*
* @author hwl_sz
*
* @desc 需要OpenOffice第三插件的支持 ,支持window\linux\mac等系统
*/
public class Office2PDF
{
public static final String[] OFFICE_POSTFIXS = {"doc", "docx", "xls",
"xlsx", "ppt", "pptx"};
/**
* 根据操作系统的名称,获取OpenOffice的安装目录
* 如我的安装目录:C:/Program Files/OpenOffice 4
*/
private static String getOfficeHome()
{
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName))
{
return "/opt/openoffice.org3";
}
else if (Pattern.matches("Windows.*", osName))
{
System.out.println("进入window下的"+osName);
return "E:/openoffice";
//E:\openoffice\program\soffice.exe
//return "C:/Program Files/OpenOffice 4";
}
else if (Pattern.matches("Mac.*", osName))
{
return "/Application/OpenOffice.org.app/Contents";
}
return null;
}
/**
* 转换文件
*
* @param inputFilePath 转换的office源文件路径
* @param outputFilePath 输出目标文件路径
*/
private static void converterFile(String inputFilePath, String outputFilePath)
{
File inputFile = new File(inputFilePath);
File outputFile = new File(outputFilePath);
// 假如目标路径不存在,则新建该路径
if (!outputFile.getParentFile().exists())
{
outputFile.getParentFile().mkdirs();
}
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
// 获取OpenOffice 的安装目录
String officeHome = getOfficeHome();
System.out.println(officeHome);
config.setOfficeHome(officeHome);
// 启动OpenOffice的服务
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
OfficeDocumentConverter converter = new OfficeDocumentConverter(
officeManager);
converter.convert(inputFile, outputFile);
System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile
+ "\n成功!");
officeManager.stop();
}
/**
* 将(.doc|.docx|.xls|.xlsx|.ppt|.pptx)等office文件 转化为pdf文件
*
* @param inputFilePath 待转换的源文件路径
* @param outputFilePath 输出的目录文件路径,如果未指定(null),则按在源文件当前目录生成同名的pdf文件
* @return 处理结果
*/
public static boolean openOffice2Pdf(String inputFilePath, String outputFilePath)
{
boolean flag = false;
File inputFile = new File(inputFilePath);
ArrayList<String> office_Formats = new ArrayList<String>();
Collections.addAll(office_Formats, OFFICE_POSTFIXS);
if ((null != inputFilePath) && (inputFile.exists()))
{
// 判断目标文件路径是否为空
if (office_Formats.contains(getPostfix(inputFilePath)))
{
if (null == outputFilePath)
{
// 转换后的文件路径
String outputFilePath_new = inputFilePath.toLowerCase().replaceAll("."
+ getPostfix(inputFilePath), ".pdf");
converterFile(inputFilePath, outputFilePath_new);
flag = true;
}
else
{
converterFile(inputFilePath, outputFilePath);
flag = true;
}
}
}
return flag;
}
/**
* 获取文件的后缀名
*/
private static String getPostfix(String inputFilePath)
{
String[] p = inputFilePath.split("\\.");
if (p.length > 0)
{
return p[p.length - 1];
}
else
{
return null;
}
}
/**
* @param args
*/
public static void main(String[] args)
{
Office2PDF.openOffice2Pdf("E:/黄色地球商务PPT模板.ppt",null);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
利用openoffice将文档转化为PDF
共39个文件
jar:18个
class:3个
java:3个
2星 需积分: 33 73 下载量 78 浏览量
2017-09-13
20:44:27
上传
评论
收藏 5MB ZIP 举报
温馨提示
将"doc", "docx", "xls", "xlsx", "ppt", "pptx",txt等转化为PDF,原理是用到我们的工具OpenOffice。还有PDF转化img图片。具体操作请看我博客"doc", "docx", "xls", "xlsx", "ppt", "pptx",txt。等文件转化为pdf
资源推荐
资源详情
资源评论
收起资源包目录
转PDF源码.zip (39个子文件)
转PDF源码
readonline2
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
src
servlet
UploadServlet.java 3KB
util
Office2PDF.java 4KB
Office2Swf.java 3KB
.project 1KB
WebRoot
upload
readfile.jsp 634B
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 934B
classes
servlet
UploadServlet.class 5KB
util
Office2PDF.class 4KB
Office2Swf.class 3KB
lib
jodconverter-cli-2.2.2.jar 6KB
slf4j-api-1.5.6.jar 22KB
unoil-3.0.1.jar 1.3MB
jsf-impl.jar 1.78MB
ridl-3.0.1.jar 248KB
jurt-3.0.1.jar 106KB
jsf-api.jar 593KB
jodconverter-2.2.2.jar 31KB
jstl-impl.jar 379KB
commons-fileupload-1.2.2.jar 58KB
jcom.jar 67KB
jodconverter-core 3.0-beta-4.jar 68KB
commons-cli-1.2.jar 40KB
juh-3.0.1.jar 54KB
xstream-1.3.1.jar 421KB
javax.servlet.jsp.jstl.jar 44KB
slf4j-jdk14-1.5.6.jar 9KB
commons-io-1.4.jar 106KB
readonline.jsp 2KB
FlexPaper
swfFiles
FlexPaperViewer.swf 397KB
js
jquery.js 89KB
flexpaper_flash_debug.js 11KB
flexpaper_flash.js 10KB
.tmp_test.html.67482~ 2KB
.mymetadata 306B
.classpath 2KB
共 39 条
- 1
资源评论
- 雨人2017-11-28下载下来根本就打不开,能不能把分退给我
- 疯子的救赎2019-06-14可以用,正在使用
- 朗月寒阳2018-11-04垃圾 不行 用不了 退分啊
- deguji2018-04-13没试过,试试
yuwinter
- 粉丝: 9
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功