zoukankan      html  css  js  c++  java
  • java word文档转换PDF文件

    使用jacob 插件 

    链接: https://pan.baidu.com/s/1un6Hb2P4y3OhI3iHOgldrg 密码: j5v8

    这是jacob 插件下载地方 有需要的可以自行下载

    使用:

    首先 将文件中

    jacob-1.19-x86.dll  、jacob-1.19-x64.dll 文件放到jdk bin目录下,用户调用时启动插件功能

    Java代码中如下:

    package com.csnt.hbzpt.Helpper;
    
    import com.csnt.hbzpt.action.ComplaintHistory.ComplainFlowInfoSaveAction;
    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.ComThread;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    import com.smartdp.components.tracer.TracerFactory;
    import com.smartdp.components.tracer.interfaces.Tracer;
    
    import java.io.File;
    
    //import org.xhtmlrenderer.pdf.ITextRenderer;/
    
    
    /**
     * Created by Administrator on 2018/9/12.
     * word文档转换pdf
     */
    public class PdfGenerateHelper {
    
        static final int wdFormatPDF = 17;// PDF 格式
        public String wordToPDF(String docFileName, boolean removeOrgFile){
            String urlPdf="";
            long start = System.currentTimeMillis();
            ActiveXComponent app = null;
            Dispatch doc = null;
            try {
                app = new ActiveXComponent("Word.Application");
                app.setProperty("Visible", new Variant(false));
                Dispatch docs = (Dispatch) app.getProperty("Documents").toDispatch();
                String pdfName = docFileName.substring(docFileName.lastIndexOf("/")+1,docFileName.lastIndexOf(".doc"));
                String path=docFileName.substring(0,docFileName.lastIndexOf("/")+1);
                String toFileName = path+ pdfName + ".pdf";
                urlPdf=toFileName;
                doc = Dispatch.call(docs,  "Open" , docFileName).toDispatch();
                Dispatch.call(doc,
                        "SaveAs",
                        toFileName, // FileName
                        wdFormatPDF);
                long end = System.currentTimeMillis();
                System.out.println("转换完成..用时:" + (end - start) + "ms.");
                tracer.logInfo("转换完成..用时:" + (end - start) + "ms.");
            } catch (Exception e) {
                tracer.logInfo("========Error:文档转换失败:" + e.getMessage());
                System.out.println("========Error:文档转换失败:" + e.getMessage());
            } finally {
    
                Dispatch.call(doc,"Close",false);
                System.out.println("关闭文档");
                tracer.logInfo("关闭文档");
                if (app != null) {
                    app.invoke("Quit", new Variant[] {});
                }
                File orgFile = new File(docFileName);
        //转换完成之后是否要删除word    看自己需求
    //            if (orgFile.exists()) {
    //                orgFile.delete();
    //            }
            }
            //如果没有这句话,winword.exe进程将不会关闭
            ComThread.Release();
            return urlPdf;
        }
    
        public static void main(String[] args) {
        //调用测试
         PdfGenerateHelper pdf = new PdfGenerateHelper();
           String name="C:\Users/dell/Desktop/test/test.doc";
          pdf.wordToPDF(name,false);
        }
    
    }
            
    

      

  • 相关阅读:
    iOS截取http/https流量
    Jenkins拾遗--第五篇-git插件填坑
    Jenkins拾遗--第三篇(用户权限管理)
    Jenkins拾遗--第四篇-适当的让构建失败
    Jenkins拾遗--第二篇(初步配置Jenkins)
    Jenkins拾遗--第一篇(安装Jenkins)
    一个测试人员的2015的回顾和2016年展望
    xcode升级导致git无法使用
    在intellj idea下用sbt的坑
    本人已转战知乎,此处不玩了。
  • 原文地址:https://www.cnblogs.com/wcnwcn/p/9663172.html
Copyright © 2011-2022 走看看