Aspose.Words 破解 操作Word模板 转PDF

1.下载aspose-words-18.8-jdk16-crack.jar   由于Aspose.Words是收费的  无法直接注入到pom.xml文件中下载  所以只用手动在cmd中操作命令键注入   操作命令如下:

mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=18.8 -Dpackaging=jar -Dfile=E:/aa/aspose-words-18.8-jdk16-crack.jar

2.添加成功之后  在pom.xml文件中加相应包配置

<dependency>
   <groupId>com.aspose</groupId>
   <artifactId>aspose-words</artifactId>
   <version>18.8</version>
</dependency>

3.将破解文件license.xml放在项目的根目录下  我的路径如下:

4.写一个AsposeWordsUtils工具类   首先对Word转PDF进行操作  

public class AsposeWordsUtils {


   /**
       * 判断是否有授权文件 如果没有则会认为是试用版,转换的文件会有水印
    *@return
    */
   public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = AsposeWordsUtils.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
   /**
    * Word转PDF操作
    *@param sourcerFile 源文件
    *@param targetFile 目标文件
    */
    public static void doc2pdf(String sourcerFile,String targetFile) {
        if (!getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
             long old = System.currentTimeMillis();
            File file = new File(targetFile);  //新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(sourcerFile);                    //sourcerFile是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //测试Word转PDF
    public static void main(String[] args) {
       doc2pdf("C:/Users/hezhipeng/Desktop/胡凌强(18860472900).docx","C:/Users/hezhipeng/Desktop/胡凌强(18860472900).pdf");
   }

}

5.对Word模板内容进行替换填充操作   首先新建一个Word文档  内容如下:

6.对Word模板内容进行填充代码如下:
 

扫描二维码关注公众号,回复: 10369803 查看本文章
public class AsposeWordsUtils {


   /**
       * 判断是否有授权文件 如果没有则会认为是试用版,转换的文件会有水印
    *@return
    */
   public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = AsposeWordsUtils.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
   /**
    * Word转PDF操作
    *@param sourcerFile 源文件
    *@param targetFile 目标文件
    */
    public static void doc2pdf(String sourcerFile,String targetFile) {
        if (!getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
             long old = System.currentTimeMillis();
            File file = new File(targetFile);  //新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(sourcerFile);                    //sourcerFile是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {
        // 验证License
        if (!getLicense()) {
            return;
        }
        //模板word
        String template = "C:/Users/hezhipeng/Desktop/新建文件夹 (2)/aa.docx";
        //目标word
        String destdoc = "F:/edit.docx";
        //定义文档接口
        Document doc = new Document(template);
        Range range = doc.getRange();
        range.replace("何志鹏","1111111111",true,false);
        range.replace("涨哈哈","22222222222",true,false);
        range.replace("静安嘉","22222222222",true,false);
        range.replace("就假按揭啊","222222222222222",true,false);
        doc.save(destdoc);
        System.out.println("完成");
    }

}

7.运行以上代码结果如下    操作完成

发布了99 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_39643007/article/details/93341886
今日推荐