Java Word文档如何转为PDF文件

最近做的功能要求是Java 调用ftl模板生成Word文件,再将Word文件转为PDF
1、首先是ftl模板生成Word文件。Java 利用ftl模板生成Word用的技术是freemarker,相关技术可以网上自查。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class DocumentHandler {
private Configuration configuration = null;  
     public DocumentHandler() {  
         configuration = new Configuration();  
         configuration.setDefaultEncoding("utf-8");  
     }  
     /**
       * @author 
       * @desc 根据类别和类型导出相应
       * @param dataMap 
       *          type 
       * @return pd
      * @throws IOException
       * @throws Exception
       */
     public File createDoc(Map<String,Object>  dataMap,int lb) throws IOException {
          configuration.setClassForTemplateLoading(this.getClass(),  "/ftl/template/");  
         String fileName =  "temp"+(int)(Math.random()*1000)+".doc";
         Template t=null;
         
         try {  
             //test.ftl为要装载的模板  
                t =  configuration.getTemplate("test.ftl");  
         } catch (IOException e) {  
             e.printStackTrace();  
         }  
         //输出文档路径及名称  
         
         File outFile = new File(fileName);  
         try {  
            Writer w = new OutputStreamWriter(new  FileOutputStream(outFile), "utf-8");
              t.process(dataMap, w);
              w.close();
         } catch (Exception e1) {  
             e1.printStackTrace();  
         }
          return outFile;
     }

2、Word转为PDF。将Word转为PDF 用到的是aspose.works

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
public static void main(String[] args) {
          License aposeLic = new License();
          try {
//            Map<String,Object> map =  setMap();
//            DocumentHandler documentHandler = new  DocumentHandler();
//            File file =  documentHandler.createWorkflowDoc(map,5);
              //破解aspose,以防PDF文件添加水印
              InputStream license =  Test.class.getClassLoader().getResourceAsStream("license.xml");
              aposeLic.setLicense(license);
              String in = "E:\\xxx\\aaa.doc";
              File file = new File(in);
              InputStream fin = new  FileInputStream(file);
              long old = System.currentTimeMillis();
            Document doc = new Document(fin);
            String test = "E:\\2019work\\ceshi.pdf";
            File outputFile = new File(test);
            FileOutputStream fileOS = new  FileOutputStream(outputFile);
            doc.save(fileOS, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) /  1000.0) + "秒\n\n" + "文件保存在:" +  outputFile.getPath());
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值