利用aspose-words直接将Word转化为图片

之前遇到一个需求,需要在word文档中加入一些文字,并转化为图片。之前也试过几种方案,但是发现效果还不是很理想,且中间需要经过一次转化为pdf的过程,最近找到了最理想的方式,即利用aspose-words作为转化工具,直接将word转化为图片,不管是速度还是质量都满足了我们的需求

具体实现

首先需要弄一个破解的license文件,以及jar包,那么这里提供下载地址
下面是读取license的工具类

import java.io.InputStream;

import com.aspose.words.License;
import com.zfsoft.serviceManagement.copy.controller.CopyController;

public class ReadConfigFile {

	public static boolean getLicense() {
		boolean result = false;
		try {
			InputStream is = CopyController.class.getClassLoader().getResourceAsStream("\license.xml");
			License aposeLic = new License();
			aposeLic.setLicense(is);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}

最后是转化的过程

			File file = new File(pngPath);
			FileOutputStream os = new FileOutputStream(file);
			Document doc = new Document(wordDestPath);
			ImageSaveOptions iso = new ImageSaveOptions(SaveFormat.JPEG);
			iso.setResolution(200);
			doc.save(os, iso);
			// doc.save(os, SaveFormat.PNG);
			os.close();

就是这么简单就能实现word转化为png的功能,且还不需要office环境。你学会了吗?

原文地址:https://www.cnblogs.com/zhendiao/p/10181966.html