java 实现pdf.js加aspose实现doc转pdf

package com.bangzheng.common.bzcommon;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.jfinal.kit.PathKit;
import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**

  • @author Administrator
    */
    public class Doc2Pdf {

    /**

    • 日志
      */
      private static final Logger logger = LoggerFactory.getLogger(Doc2Pdf.class);
      /public static void main(String[] args) {
      Doc2Pdf.doc2pdf(“D:/国家总体应急预案.docx”, “F:/XYpxyl/”, “asd”);
      }
      /

    public static boolean getLicense() {
    boolean result = false;
    try {
    // license.xml应放在…\WebRoot\WEB-INF\classes路径下
    InputStream is = ResolverUtil.Test.class.getClassLoader().getResourceAsStream(“license.xml”);
    License aposeLic = new License();
    aposeLic.setLicense(is);
    result = true;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return result;
    }

    /**

    • word转pdf
    • @param inPath
    • @param outPath
    • @param fileName
    • @return
      /
      public static String doc2pdf(String inPath, String outPath, String fileName) {
      // 验证License 若不验证则转化出的pdf文档会有水印产生
      if (!getLicense()) {
      return null;
      }
      try {
      long old = System.currentTimeMillis();
      //上传地址路径
      String uploadPath = PathKit.getWebRootPath() + File.separator + outPath;
      File file = new File(uploadPath + File.separator + fileName.substring(0, fileName.lastIndexOf(".")) + “.pdf”);
      FileOutputStream os = new FileOutputStream(file);
      //设置一个字体目录
      /
      FontSettings.setFontsFolder("/fonts/chinese/consola.ttf", false);/
      //设置多个字体目录
      /
      String[] fonts = {"/fonts/chinese/consola.ttf", “/fonts/chinese/simsun.ttf”, “/fonts/chinese/msyh.ttf”, “/fonts/chinese/msyhbd.ttf”, “/fonts/chinese/simfang.ttf”, “/fonts/chinese/simhei.ttf”, “/fonts/chinese/simkai.ttf”, “/fonts/chinese/simsunb.ttf”};
      FontSettings.setFontsFolders(fonts, true);*/
      //Address是将要被转化的word文档
      Document doc = new Document(inPath);
      //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
      doc.save(os, SaveFormat.PDF);
      os.close();
      long now = System.currentTimeMillis();
      //转化用时
      logger.info(fileName + “转pdf共耗时:” + ((now - old) / 1000.0) + “秒”);
      os.close();
      return outPath + File.separator + file.getName();
      } catch (Exception e) {
      e.printStackTrace();
      return null;
      }
      }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值