word转pdf

利用第三方jar包,可以非常方便的对word文档进行一系列的转换,而且是免费的,免费的功能上会有所阉割,限制可参考去官网查找:
官方网站
在这里对此工作者们表示深切的感谢!官网中对功能的操作给出了详细的代码示例,非常的清晰!
一开始jar包不晓得怎么下载,没有找到好的途径,最终费劲九牛二虎之力找到了,即以下链接:
jar包下载地址
可以选择自己需要的jar包下载。

说一下用此工具包遇到的坑:

1. word填充数据后在转换成pdf,功能实现了,但效果有个瑕疵:pdf的开头有这样一句警示语
这条警示语着实可恶,故此搜罗各大平台,大神的解决方法, 网上的解决方法思路:
因为警示语是在页面的第一行,所以就新增一页,然后再把这一页删掉。

PdfDocument pdfDocument = new PdfDocument();
pdfDocument.Pages.Add();
pdfDocument.Pages.RemoveAt(0);

经本人测试,无此方法,本人测试的方法如下:

PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("D:\\haha.pdf");
pdf.getPages().add();
pdf.getPages().removeAt(0);

想法很美好,现实很骨感呀!经测试,此方法无效!若哪位大神想到了好的方法,非常欢迎在下方评论区留言,感谢! 思前想后,猜测原因可能是因为我引用的Spire.Doc for JAVA,而不是Spire.pdf。总之,最后没有得到有效的解决。

2. 若手动在项目中引入jar包,则需要注意,添加依赖的时候不要同时添加两种类型的jar,譬如:
在这里插入图片描述
这样导致jar包无法引入,故此本项目引入一个jar包,就成功了:
在这里插入图片描述
倘若后期找到去除pdf警示语的好方法,本人必更新!若转载,请说明出处!
(本文完)

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以将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文件<br> * @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文件<br> * * @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文件<br> * 默认换位置与源文件位置相同,文件名相同 * @param inputFilePath * 源文件路径,如:"d:/test.docx" * @return * @throws FileIOException * 文件格式不正确 */ public boolean office2pdf(String inputFilePath) throws FileIOException { return office2pdf(inputFilePath, null); } /** * 根据操作系统的名称,获取OpenOffice4的安装目录<br> * 如我的OpenOffice 4安装在:C:\\Program Files (x86)\\OpenOffice 4<br> * @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"<br> * * @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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_函数_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值