對應java代碼:java
package com.yoooya.ytp.utils.doc; import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.SaveFormat; import com.yoooya.ytp.utils.DateUtils; import com.yoooya.ytp.utils.IdUtils; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import sun.misc.BASE64Encoder; import java.io.*; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * cjianquan 2020/1/3 */ public class ExportWord { private Configuration configuration = null; private static int i = 9; public ExportWord() { configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); } public File createWordToPic(Map<String, Object> dataMap){ try{ String path = ExportWord.class.getResource("/").getPath()+"aspose/"; File pathFile=new File(path); if(!pathFile.exists()){ pathFile.mkdirs(); } return createWord(dataMap,path,"wcsyzm.ftl"); }catch (Exception e){ e.printStackTrace(); } return null; } public File createWord(Map<String, Object> dataMap, String filePath, String tmpName) { try{ configuration.setDirectoryForTemplateLoading(new File(filePath)); Template t = null; try { t = configuration.getTemplate(tmpName); // 文件名 } catch (IOException e) { e.printStackTrace(); } String fileName = String.valueOf(IdUtils.id()) + ".doc"; String pngFileName = String.valueOf(IdUtils.id()) + ".png"; File outFile = new File(filePath+"temp", fileName); File outPngFile = new File(filePath+"temp", pngFileName); Writer out = null; try { out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outFile), "utf-8")); } catch (Exception e1) { e1.printStackTrace(); } try { t.process(dataMap, out); out.close(); getLicense(filePath); FileOutputStream os = new FileOutputStream(outPngFile); FileInputStream iStream = new FileInputStream(outFile); Document doc = new Document(iStream); // doc.save(os, com.aspose.words.SaveFormat.PDF); doc.save(os, SaveFormat.PNG); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return outPngFile; }catch (Exception e){ e.printStackTrace(); } return null; } /** 獲取註冊文件 */ public static void getLicense(String filePath) { String path = filePath + "/license.xml"; InputStream is; try { is = new FileInputStream(new File(path)); License license = new License(); license.setLicense(is); } catch (FileNotFoundException e) { // logger.error("license.xml file not found"); } catch (Exception e) { // logger.error("license register failed"); } } public static void doc2pdf(String wordPath, String pdfPath) { try { long old = System.currentTimeMillis(); File file = new File(pdfPath); // 新建一個pdf文檔 FileOutputStream os = new FileOutputStream(file); Document doc = new Document(wordPath); // Address是將要被轉化的word文檔 doc.save(os, com.aspose.words.SaveFormat.PDF); long now = System.currentTimeMillis(); os.close(); System.out.println("共耗時:" + ((now - old) / 1000.0) + "秒"); // 轉化用時 } catch (Exception e) { e.printStackTrace(); } } public static String GetImageStr(String imgFilePath) {// 將圖片文件轉化爲字節數組字符串,並對其進行Base64編碼處理 String basePath = ExportWord.class.getResource("/").toString(); basePath = basePath.replaceAll("file:/",""); byte[] data = null; // 讀取圖片字節數組 try { InputStream in = new FileInputStream(basePath+"aspose/"+imgFilePath); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 對字節數組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64編碼過的字節數組字符串 } private void getData(Map<String, Object> dataMap) { dataMap.put("company", "原力肽"); dataMap.put("address", "某某地址"); dataMap.put("csmj", "100"); dataMap.put("userCompany", "陳建泉個體戶"); dataMap.put("dateStart", "2019年12月1日"); dataMap.put("dateEnd", "2020年12月1日"); dataMap.put("nowDate", "2020年1月3日"); dataMap.put("imageData", GetImageStr("seal.png")); // dataMap.put("address", "福建省泉州市鯉城區"); // dataMap.put("phone", "13459260612"); } public static void main(String[] args) throws Exception { /*ExportWord test = new ExportWord(); String basePath = ExportWord.class.getResource("/").toString(); basePath = basePath.replaceAll("file:/",""); System.out.println("basePath="+basePath); Map<String, Object> dataMap = new HashMap<String, Object>(); test.getData(dataMap); test.createWord(dataMap, basePath+"aspose/","wcsyzm.ftl");*/ ExportWord exportWord = new ExportWord(); Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("company", "company11"); dataMap.put("managerName","managerName22"); dataMap.put("address", "address33"); dataMap.put("csmj", "csmj44"); dataMap.put("userCompany", "userCompany55"); dataMap.put("dateStart", "yyyy年MM月dd日 666"); dataMap.put("dateEnd", "yyyy年MM月dd日777"); dataMap.put("nowDate", DateUtils.format(new Date(),"yyyy年MM月dd日")); dataMap.put("imageData", exportWord.GetImageStr("seal.png")); File picFile = exportWord.createWordToPic(dataMap); System.out.println("picFile="+picFile.getAbsolutePath()); } }
其中 license.xml 是aspose for java 的license文件。spring
wcsyzm.ftl 文件,是有doc文件另存爲ftl獲得了。
dataMap的key值,對應的是ftl的參數,相似:${company} 數組
原始word的內容如圖:
編碼
seal.png 是一個要加的水印 或者刻章圖片。。spa