00003-aspose for java 生成水印刻章等,可转为word,pic,pdf

2021-01-21 02:15

阅读:704

标签:director   for   字节数组   pack   art   dir   framework   pat   dateutil   

对应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 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 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 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 dataMap = new HashMap();
        test.getData(dataMap);
        test.createWord(dataMap, basePath+"aspose/","wcsyzm.ftl");*/

        ExportWord exportWord = new ExportWord();
        Map dataMap = new HashMap();
        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文件。

wcsyzm.ftl 文件,是有doc文件另存为ftl得到了。
dataMap的key值,对应的是ftl的参数,类似:${company}

原始word的内容如图:
技术图片

seal.png 是一个要加的水印 或者刻章图片。。

00003-aspose for java 生成水印刻章等,可转为word,pic,pdf

标签:director   for   字节数组   pack   art   dir   framework   pat   dateutil   

原文地址:https://blog.51cto.com/14816966/2495771


评论


亲,登录后才可以留言!