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文件地址:

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐