win环境下openoffice将word转换成PDF后,和原word格式不符

山那边的路 2016-02-23 05:59:47
如题,我用Java代码利用openoffice将word2003 转换成PDF后 PDF的内容格式和原word的不符。如图 这是PDF的 ,这是原word 这个明显出现不同 请教 怎么能让2个格式相符
...全文
1240 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dazer007 2017-09-04
  • 打赏
  • 举报
回复
楼主求分享您的经验。
Dazer007 2017-09-04
  • 打赏
  • 举报
回复
楼主解决了,这个问题。
qq_16462963 2016-11-14
  • 打赏
  • 举报
回复
楼主,这个问题解决了嘛,,可以分享一下吗
yongwenxiu 2016-09-02
  • 打赏
  • 举报
回复
解决了吗,现在我也遇到这样的问题
xiashany 2016-09-02
  • 打赏
  • 举报
回复
如果还不行的话就poi了
xiashany 2016-09-02
  • 打赏
  • 举报
回复
用LibreOffice吧,就是把这个按openoffice一样的处理
不写代码的钦 2016-09-02
  • 打赏
  • 举报
回复
两个不同的软件没办法的,在windows下用poi转吧
山那边的路 2016-02-24
  • 打赏
  • 举报
回复
不能沉了
可以将word,ppt转换成pdf格式, 1.安装《Apache_OpenOffice_4.1.5_Win_x86_install_zh-CN.exe》 2.将jar包加入到工程中 3.编写Java类,注意配置安装的位置 package com.han.office; import java.io.File; import java.util.Date; import java.util.regex.Pattern; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; import com.sun.star.util.FileIOException; /** * 这是一个工具类,主要是为了使Office2003-2007全部格式的文档(.doc|.docx|.ppt|.pptx) * 转化为pdf文件
* @date 2018-1-18 * @author hanshaobin * */ public class Officer { public static void main(String[] args) { Officer office2pdf = new Officer(); try { office2pdf.office2pdf("d:/test.doc", "d:/java_" + new Date().getTime() + ".pdf"); office2pdf.office2pdf("d:/test.pptx"); } catch (FileIOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 使Office2003-2007全部格式的文档(.doc|.docx|.ppt|.pptx) 转化为pdf文件
* * @param inputFilePath * 源文件路径,如:"d:/test.docx" * @param outputFilePath * 目标文件路径,如:"d:/test_docx.pdf" * @return * @throws FileIOException * 文件格式不正确 */ public boolean office2pdf(String inputFilePath, String outputFilePath) throws FileIOException { if(!isRigthFileFix(inputFilePath)){ throw new FileIOException("文件格式不正确"); } boolean flag = false; OfficeManager officeManager = getOfficeManager(); // 连接OpenOffice OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); // long begin_time = new Date().getTime(); // 判断目标文件路径是否为空 if (null == outputFilePath) { // 转换后的文件路径 outputFilePath = getOutputFilePath(inputFilePath); } try{ converterFile(inputFilePath, outputFilePath, converter); flag = true; }catch(Exception e){ e.printStackTrace(); } officeManager.stop(); // long end_time = new Date().getTime(); // System.out.println("文件转换耗时:[" + (end_time - begin_time) / 1000 + "]s"); return flag; } /** * 使Office2003-2007全部格式的文档(.doc|.docx|.ppt|.pptx) 转化为pdf文件
* 默认转换位置与源文件位置相同,文件名相同 * @param inputFilePath * 源文件路径,如:"d:/test.docx" * @return * @throws FileIOException * 文件格式不正确 */ public boolean office2pdf(String inputFilePath) throws FileIOException { return office2pdf(inputFilePath, null); } /** * 根据操作系统的名称,获取OpenOffice4的安装目录
* 如我的OpenOffice 4安装在:C:\\Program Files (x86)\\OpenOffice 4
* @return OpenOffice 4的安装目录 */ public String getOfficeHome() { String osName = System.getProperty("os.name"); if (Pattern.matches("Linux.*", osName)) { return "/opt/openoffice.org4"; } else if (Pattern.matches("Windows.*", osName)) { return "C:\\Program Files (x86)\\OpenOffice 4"; } else if (Pattern.matches("Mac.*", osName)) { return "/Application/OpenOffice.org.app/Contents"; } return null; } /** * 连接OpenOffice 并且启动OpenOffice * @return */ public OfficeManager getOfficeManager() { DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); // 获取OpenOffice 4的安装目录 String officeHome = getOfficeHome(); config.setOfficeHome(officeHome); // 设置超时时间为5分钟 config.setTaskExecutionTimeout(1000 * 60 * 5L); // 启动OpenOffice的服务 OfficeManager officeManager = config.buildOfficeManager(); officeManager.start(); return officeManager; } /** * 转换文件 * @param inputFilePath * 转换文件的路径及名称,例如:d:/test.doc * @param outputFilePath * 目标文件的路径及名称,例如:d:/test.pdf * @param converter */ public void converterFile(String inputFilePath, String outputFilePath, OfficeDocumentConverter converter) { File inputFile = new File(inputFilePath); File outputFile = new File(outputFilePath); // 假如目标路径不存在,则新建该路径 if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } // 文件转换 converter.convert(inputFile, outputFile); } /** * 获取输出文件 * @param inputFilePath * 输入文件的位置及文件名称,如:"d:/test.docx" * @return * 默认的输出文件路径:如:"d:/test.pdf" */ public String getOutputFilePath(String inputFilePath) { String outputFilePath = inputFilePath.substring(0,inputFilePath.lastIndexOf('.'))+".pdf"; return outputFilePath; } /** * 获取inputFilePath的后缀名,如:"d:/test.pptx"的后缀名为:"pptx"
* * @param inputFilePath * @return */ public String getPostfix(String inputFilePath) { return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1); } /** * 检查文件是否是指定格式 * @param inputFilePath * 输入文件的路径 * @return * 是指定格式返回true ,否则返回 false */ public boolean isRigthFileFix(String inputFilePath){ String[] fixes =new String[]{"doc","docx","ppt","pptx"}; boolean flag = false; String fix = getPostfix(inputFilePath); for (String f : fixes) { if(f.equalsIgnoreCase(fix)){ flag = true; break; } } return flag; } } 工具下载位置:https://pan.baidu.com/s/1brhhbdH

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧