java使用Aspose实现 word文档转pdf文件高效不失真

您所在的位置:网站首页 word输出为pdf清晰度太低 java使用Aspose实现 word文档转pdf文件高效不失真

java使用Aspose实现 word文档转pdf文件高效不失真

2024-07-13 10:26| 来源: 网络整理| 查看: 265

java使用Aspose word文档转pdf功能实现 主要步骤 使用Aspose进行文档转换,首先引入相应的jar包到系统环境项目resource下导入license.xml文件使用Aspose时,需要调用设置License方法,设置完成第一次转换比较慢,再次就不需要设置License方法,效率会更高。

注意:使用Aspose时,每一个模块(words,pdf,cells)都有相同的类,如License类,SaveOptions类,SaveFormat类,功能各不相同。

获取license示例代码: package com.sinoif.common.utils; import com.aspose.words.License; import lombok.extern.slf4j.Slf4j; import java.io.InputStream; /** * @description: * @author: guanlj * @date: 2020/7/24 11:10 */ @Slf4j public class AsposeLicenseUtil { private static License aposeLic = new License(); /** * 获取License的输入流 * * @return */ private static InputStream getLicenseInput() { InputStream inputStream = null; ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { //获取项目resource下的文件流 inputStream = contextClassLoader.getResourceAsStream("license.xml"); } catch (Exception e) { log.error("license not found!"+ e); } return inputStream; } /** * 设置License * * @return true表示已成功设置License, false表示失败 */ public static boolean setWordsLicense() { if (!aposeLic.getIsLicensed()) { try { aposeLic.setLicense(getLicenseInput()); return aposeLic.getIsLicensed(); } catch (Exception e) { log.error("set words license error!", e); } } return aposeLic.getIsLicensed(); } } word转pdf工具类: package com.sinoif.common.utils; import com.aspose.words.*; import com.aspose.words.Shape; import lombok.extern.slf4j.Slf4j; import java.awt.*; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; /** * @description: * @author: guanlj * @date: 2020/7/24 11:37 */ @Slf4j public class WordToPdfUtil { /** * word 转 pdf * @param inputStream word文件流 * @param path 输出pdf 全路径名称 * @return */ public static Boolean convert2Pdf(InputStream inputStream, String path) { try { if (AsposeLicenseUtil.setWordsLicense()) { long start = System.currentTimeMillis(); Document doc = new Document(inputStream); // insertWatermarkText(doc, "测试水印"); // 添加水印 PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.setSaveFormat(SaveFormat.PDF); // 设置3级doc书签需要保存到pdf的heading中 pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(3); // 设置pdf中默认展开1级 pdfSaveOptions.getOutlineOptions().setExpandedOutlineLevels(1); doc.save(path, SaveFormat.PDF); long end = System.currentTimeMillis(); log.info("convert doc2pdf completed, elapsed " + (end - start) / 1000.0 + " seconds!"); return true; } else { return false; } } catch (Exception e) { log.error("convert doc2pdf error!", e); return false; } } /** * word 转 pdf * @param inputStream word文件流 * @param outputStream 输出pdf输出流 * @return */ public static Boolean convert2PdfStream(InputStream inputStream, OutputStream outputStream) { try { if (AsposeLicenseUtil.setWordsLicense()) { long start = System.currentTimeMillis(); Document doc = new Document(inputStream); // insertWatermarkText(doc, "测试水印"); // 添加水印 PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.setSaveFormat(SaveFormat.PDF); // 设置3级doc书签需要保存到pdf的heading中 pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(3); // 设置pdf中默认展开1级 pdfSaveOptions.getOutlineOptions().setExpandedOutlineLevels(1); //doc.save(outputStream, pdfSaveOptions); doc.save(outputStream, SaveFormat.PDF); long end = System.currentTimeMillis(); log.info("convert doc2pdf outputStream completed, elapsed " + (end - start) / 1000.0 + " seconds!"); return true; } else { return false; } } catch (Exception e) { log.error("convert doc2pdf outputStream error!", e); return false; } } /** * word 转 pdf * @param inputPath word文件path * @param outPath pdf文件path * @return */ public static Boolean convert2PdfPath(String inputPath, String outPath) { try { if (AsposeLicenseUtil.setWordsLicense()) { long start = System.currentTimeMillis(); File file = new File(outPath); FileOutputStream os = new FileOutputStream(file); Document doc = new Document(inputPath); doc.save(os, SaveFormat.PDF); long end = System.currentTimeMillis(); log.info("convert doc2pdf completed, elapsed " + (end - start) / 1000.0 + " seconds!"); return true; } else { return false; } } catch (Exception e) { log.error("convert doc2pdf error!", e); return false; } } }

 

所需jar包和license.xml文件地址:

https://download.csdn.net/download/glj6287/12710511

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3