以前遇到一个需求,须要在word文档中加入一些文字,并转化为图片。以前也试过几种方案,可是发现效果还不是很理想,且中间须要通过一次转化为pdf的过程,最近找到了最理想的方式,即利用aspose-words做为转化工具,直接将word转化为图片,无论是速度仍是质量都知足了咱们的需求java
具体实现
首先须要弄一个破解的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; } }
最后是转化的过程spa
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环境。你学会了吗?.net