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

java使用Aspose word文档转pdf功能实现

主要步骤

  1. 使用Aspose进行文档转换,首先引入相应的jar包到系统环境
  2. 项目resource下导入license.xml文件
  3. 使用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

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Aspose是一种强大的文件处理库,可以用于实现Word文件换为PDF文件的功能。使用Aspose可以高效地将Word文件换为PDF文件,并且在换过程中不会丢失文档的质量。 要使用Aspose实现WordPDF文件的功能,首先需要安装相应的Aspose文件。然后,可以通过编程的方式使用Aspose库来读取Word文件并将其换为PDF格式。 Aspose提供了丰富的API和方法,可以灵活地控制换过程中的各种参数和选项。用户可以根据需要设置字体、布局、页眉页脚、文本样式、图片处理等功能,以确保换后的PDF文件符合预期。 使用Aspose进行WordPDF的好处是,它能够高效地处理大型的Word文件,并保持文档内容的准确性和完整性。无论是处理数百页的复杂文档,还是换包含大量图像和表格的文件Aspose都能够快速且准确地生成高质量的PDF文件。 此外,Aspose还支持将Word文件中的书签、超链接、目录等元数据保留在换后的PDF文件中。这意味着用户可以轻松地在PDF文件中进行导航和查找。 总之,Aspose是一种可靠且功能强大的工具,可以高效地将Word文件换为PDF文件。无论是为了保持文件格式的一致性,还是为了便于文件的共享和分发,Aspose都是一个值得考虑的选择。 ### 回答2: Aspose是一种强大的文件处理库,可以实现Word文档换为PDF格式的功能,而且换过程高效且不会损失文件的质量。 首先,使用Aspose提供的API,我们可以轻松地将Word文档加载到内存中,并进行各种操作,比如修改内容、格式化文本、插入表格、添加图片等。无论文档有多复杂,Aspose都能够正确地识别和处理。 接下来,我们可以使用Aspose提供的方法,将已经处理好的Word文档换为PDF格式。在换的过程中,Aspose会自动处理文档的布局、样式、图像和字体等方面,确保最终的PDF文件与原始的Word文档保持一致,不会出现任何失真Aspose换Word到PDF的过程也非常高效。它利用了优化的算法和多线程处理技术,可以快速地处理大型的Word文档。无论文档的复杂度和大小如何,Aspose都能够保持高效的性能,给用户提供快速而稳定的换体验。 总的来说,Aspose是一个功能强大且高效的工具,可以帮助用户轻松地实现Word文档PDF文件换。无论是对于个人用户还是企业用户来说,Aspose都是一个值得信赖的解决方案,可以满足各种换需求。同时,Aspose换过程也非常稳定,不会引起任何质量上的损失。 ### 回答3: Aspose是一种功能强大的文件处理库,它可以实现将Word文件换为PDF文件,并且在换过程中能够保持高效和不失真的特点。 首先,Aspose提供了丰富的API和功能,可以轻松地将Word文件加载到代码中进行处理。无论是doc还是docx格式的Word文件Aspose都可以处理。 其次,Aspose具有高效的处理能力,可以在短时间内将大量的Word文件批量换为PDF文件。无论是单个文件还是多个文件Aspose都能够快速而准确地完成换任务。 此外,Aspose换过程非常稳定,不会出现数据丢失或换错误的情况。它能够准确地将Word文件中的文本、表格、图片等元素换为PDF文件,并且保持原始文件的格式和布局。 另外,Aspose提供了丰富的设置选项,可以根据需要进行自定义设置。例如,可以设置PDF的页面大小、页面方向、页眉页脚、字体样式等。这些设置选项可以确保最终生成的PDF文件符合用户的需求和要求。 总结来说,Aspose是一种高效且不失真的工具,能够帮助我们实现Word文件PDF文件换。无论是在速度、准确性还是功能方面,Aspose都是一个理想的选择。通过使用Aspose,我们可以轻松地将Word文件换为PDF文件,并且确保换过程中不会出现任何质量损失。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值