利用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環境。你學會了嗎?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM