Java 处理word转pdf

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QL753123/article/details/83622697

直接上代码,采用aspose word转化,方法类如下 ,文末又相关代码以及工具破解包

package com.myhexin.ifs.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
 
import org.aspectj.weaver.ast.Test;
 
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;


/**
 * @author Administrator
 * @version $Id$
 * @since
 * @see
 */
public class WordPdfUtil {

    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static void doc2pdf(String inPath, String outPath) {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
                                         // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void savedocx(String inPath, String outPath) {
      if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
          return;
      }
      try {
          long old = System.currentTimeMillis();
          File file = new File(outPath); // 新建一个空白pdf文档
          FileOutputStream os = new FileOutputStream(file);
          Document doc = new Document(inPath); // Address是将要被转化的word文档
          doc.save(os, SaveFormat.DOCX);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
                                       // EPUB, XPS, SWF 相互转换
          long now = System.currentTimeMillis();
          System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
    //此插件不需要安装office服务但是此插件加密,如不注册会有水印
    public static void main(String[] args) {
    //    doc2pdf("D:/test.doc");
        System.out.println(111);
        doc2pdf("D:\\test.doc","D:\\test.pdf");
    }
}
 

猜你喜欢

转载自blog.csdn.net/QL753123/article/details/83622697