Apose.words太好用了!支持html,word,pdf互相转换

package com.ulearning.ulms.util;



import cn.hutool.core.io.FileUtil;

import com.aspose.words.*;
import freemarker.template.Configuration;
import freemarker.template.Template;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.util.Map;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * word、pdf处理工具类
 */
public class WordPDFUtil {

    protected static Logger logger = LoggerFactory.getLogger(WordPDFUtil.class);

    /**
     * 读取license.xml的内容
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        Resource resource = new ClassPathResource("static/license.xml");
        try (InputStream is = resource.getInputStream()) {
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过模板导出word格式文件
     *
     * @param dataMap      导出数据
     * @param templateName 模板名称
     * @param path         导出word的路径以及文件名称
     */
    public static void exportHtml(Map dataMap, String templateName, String path) {
        try {
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            if (!getLicense()) {
                return;
            }
            //Configuration 用于读取ftl文件
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("utf-8");
            //指定路径(根据某个类的相对路径指定)
            configuration.setClassForTemplateLoading(WordPDFUtil.class, "/template");
            //输出文档路径及名称
            File outFile = new File(path);
            FileOutputStream os = new FileOutputStream(outFile);
            //以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate(templateName, "utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
            template.process(dataMap, out);
//            //导出成word时,\n换行替换成  标签,不起作用,无法换行,所以用Document保存word
//            Document doc = new Document(path);
//            doc.save(os, SaveFormat.DOC);
            out.close();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * html转pdf文件
     *
     * @param Address    原文件地址
     * @param pdfAddress 保存的pdf文件地址
     */
    public static void htmlConvertPdf(String Address, String pdfAddress) throws IOException {
//        htmlToWord(Address,docAddress);
//        wordConvertPdf(docAddress, pdfAddress);
        byte[] html = FileUtil.readBytes(Address);
        if (!getLicense()) {
            return;
        }
        try {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.insertHtml(new String(html));
            //生成doc文件
            doc.save(pdfAddress, SaveFormat.PDF);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }



    /**
     * 通过模板导出word格式文件
     *
     * @param dataMap      导出数据
     * @param templateName 模板名称
     * @param path         导出word的路径以及文件名称
     */
    public static void exportWord(Map dataMap, String templateName, String path) {
        try {
            // 验证License 若不验证则转化出的pdf文档会有水印产生
            if (!getLicense()) {
                return;
            }
            //Configuration 用于读取ftl文件
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("utf-8");
            //指定路径(根据某个类的相对路径指定)
            configuration.setClassForTemplateLoading(WordPDFUtil.class, "/template");
            //输出文档路径及名称
            File outFile = new File(path);
            FileOutputStream os = new FileOutputStream(outFile);
            //以utf-8的编码读取ftl文件
            Template template = configuration.getTemplate(templateName, "utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
            template.process(dataMap, out);
            //导出成word时,\n换行替换成  标签,不起作用,无法换行,所以用Document保存word
            Document doc = new Document(path);
            doc.save(os, SaveFormat.DOC);
            out.close();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * word转pdf文件
     *
     * @param Address    原文件地址
     * @param pdfAddress 保存的pdf文件地址
     */
    public static void wordConvertPdf(String Address, String pdfAddress) throws IOException {
//         验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }
        FileOutputStream os = null;

        try {
            // 新建一个空白pdf文档
            File file = new File(pdfAddress);
            os = new FileOutputStream(file);
            // Address是将要被转化的word文档
            Document doc = new Document(Address);
            Document document = new Document();//新建一个空白pdf文档
            document.removeAllChildren();
            document.appendDocument(doc, ImportFormatMode.USE_DESTINATION_STYLES);//保留样式
            document.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != os)
                os.close();
        }
    }

    /**
     * html内容转word
     * @param htmlPath  html内容
     * @param wordPath  word保存路径
     * @return
     */
    public static void htmlToWord(String htmlPath,String wordPath) {
        byte[] html = FileUtil.readBytes(htmlPath);
        if (!getLicense()) {
            return;
        }
        try {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.insertHtml(new String(html));
            //生成doc文件
            doc.save(wordPath, SaveFormat.DOC);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }



}

你可能感兴趣的:(c#,开发语言)