word 转 PDF

     网上有很多 word 转 PDF 的例子,有些是根据 调用安装的服务来实现的,但是如果要实现跨平台或者在没有安装服务的服务器上面转,就很少有例子了。

    我也是经过千辛万苦的调试和在 stackoverflow 网站的才能找到答案, 难解决的问题一般都是老外的网站啊。。没办法。

   不多说了,我直接上代码。挺简单的。但是花了我好长时间啊!

   

   引入jar 

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>fr.opensagres.xdocreport</groupId>
			<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
			<version>1.0.6</version>
		</dependency>

编码:

/**
     *@Description: docx 转 PDF
     *@Author:杨攀
     *@Since: 2016年11月24日下午2:01:58
     *@param docPath
     *@param pdfPath
     */
    public static void convertDocxToPDFByPOI(String docxPath,String pdfPath){
        try {
            InputStream doc = new FileInputStream (new File (docxPath));
            XWPFDocument document = new XWPFDocument (doc);
            PdfOptions options = PdfOptions.create ();
            OutputStream out = new FileOutputStream (new File (pdfPath));
            PdfConverter.getInstance ().convert (document, out, options);
        } catch (Exception ex) {
            ex.printStackTrace ();
        }
    }

   以上的确可以转 PDF ,而且在手机中实现转 PDF ,但是效果太差,没办法,只好转变思路, 在服务器上面转PDF ,手机端下载PDF来实现功能。

  

服务端 选择 Jacob 来实现;

   

   步骤:
   1、下载 Jacob 包, 但是maven 只下载 jar , 没有包括想对于的 dll 文件。 所以需要手动去下载
         地址: https://sourceforge.net/projects/jacob-project/files/jacob-project/
         选择自己版本对应的下载,我选择的是 1.14.3 

    2、下载的Jacob包中有几个dll, 64位的,86位的,拷贝对应的 dll 到 文件放置到C:\Windows\system32目录下,C为你的操作系统盘符,根据自己的系统版本放置不同的dll文件,名称上有写的,或 将dll文件放到当前JDk和jre的lib目录中

    3、编码

    引入 jar

<dependency>
		    <groupId>net.sf.jacob-project</groupId>
		    <artifactId>jacob</artifactId>
		    <version>1.14.3</version>
		</dependency>

 

   

  // PDF格式  值为17
    private static final int wdFormatPDF = 17;


/**
     *@Description: docx 转 PDF
     *@Author:杨攀
     *@Since: 2016年11月24日下午4:14:19
     *@param docxPath
     *@param pdfPath
     *@return
     */
    public static boolean convertDocxToPDFByJacob(String docxPath,String pdfPath){
        
        // 打开word应用程序
        ActiveXComponent app = null;
        try {
            // 打开word应用程序
            app = new ActiveXComponent ("Word.Application");
            // 设置word不可见
            app.setProperty ("Visible", false);
            // 获得word中所有打开的文档,返回Documents对象
            Dispatch docs = app.getProperty ("Documents").toDispatch ();
            // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
            Dispatch doc = Dispatch.call (docs, "Open", docxPath, false, true).toDispatch ();
            // 调用Document对象的SaveAs方法,将文档保存为pdf格式, 为pdf的插件 SaveAsPDFandXPS.exe 安装。(否则无法调用SaveAs方法)
            /*- 
            Dispatch.call(doc,"SaveAs",pdfPath,wdFormatPDF  //word保存为pdf格式宏,值为17); 
             */
            // word保存为pdf格式宏,值为17
            Dispatch.call (doc, "ExportAsFixedFormat", pdfPath, wdFormatPDF);
            // 关闭文档
            Dispatch.call (doc, "Close", false);
            
            return true;
        } catch (Exception e) {
            e.printStackTrace ();
            return false;
        }finally{
            if(app != null){
                // 关闭word应用程序
                app.invoke ("Quit", 0);
            }
        }
    }

经过测试, 转换的 PDF 格式, 非常满意!!! 嘿嘿

猜你喜欢

转载自yangpanwww.iteye.com/blog/2281835
今日推荐