泛微ECOLOGY9流程模块(建模通用)office附件上传自动转为图片并显示在流程指定附件字段中

目录说明

通过泛微ECOLOGY9的流程接口二次开发实现office附件上传自动转为图片并显示在流程指定附件字段中
在有些业务场景中,往往需要将上传的文件统一为PDF格式或者图片,比如一些业务合同,在OA流程中上传的是WORD或者EXCEL格式的文件,但是流程提交后,希望附件中显示的是对应的PDF文件或图片。
实现原理
1、通过OA流程接口开发将上传的文件后台转为PDF文件。
2、给生成的PDF文件添加上指定的水印。
3、将PDF文件打印为图片,并上传到OA文档中,并将文档ID写入流程指定附件字段中。

实现将office文件转为PDF文件及PDF文件转换为图片功能

要实现将OFFICE文件转换为PDF文件再转为图片,需要借助jacob1.19插件及ICEpdf-pro-6.1.1插件实现。
jacob1.19及ICEpdf-pro-6.1.1相关下载

1、首选在服务器中搭建环境(安装office 2010 或者WPS)。

2、下载jacob1.19并将对应的JAR和DLL文件放到指定的位置。

2.1、将系统对应的DLL文件放到 c:\windows\system32 下面。
2.2、将jacob.jar 文件导入项目中。 ecoloey/web-inf/lib/下面。

3、其它问题说明:

3.1、在服务器上安装windows office2010版本,如果有安装过WPS或者其它的office 先卸载,再重新安装office2010 或者 office2016. (这里解决 插件初始化失败问题)
3.2、如果无法执行转换服务, 则是系统权限不足。 (这里解决文件访问权限不足问题。)
打开 运行 -cmd -mmc -32 进入控制台。 文件-添加 组件服务 , DCOM配置, 找到 Microsoft Excel Application ,右键 -属性
点 标识, 选择 交互式用户 ,点 安全,启动与激活权限 选择 自定义, 添加 network service 用户,并给 本地启动 本地激活 权限。
然后在 安全- 访问权限,选择 自定义, 添加 network service 用户,给本地访问 权限。
3.3、在 c:\windows\system32\config\systemprofile\ 下面创建 Desktop 文件夹
在c:\windows\SysWOW64\config\systemprofile\ 下面创建 Desktop 文件夹

4、ICEpdf-pro-6.1.1相关使用

将ICEpdf-pro-6.1.1相关的JAR文件放到 ecoloey/web-inf/lib/下面。

5、实现文件格式转换工具类

传源文件路径和文件后缀,转为PDF文件存放在源文件目录下
FileFormatConvertWps.java

package com.file.common.utils;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import weaver.general.BaseBean;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;

public class FileFormatConvertWps {
   

    public static final String DOC = "doc";
    public static final String DOCX = "docx";
    public static final String PDF = "pdf";
    public static final String XLS = "xls";
    public static final String XLSX = "xlsx";
    public static final String MP4 = "mp4";
    public static final String PPT = "ppt";
    public static final String PPTX = "pptx";

    // 8 代表word保存成html
    public static final int WORD2HTML = 8;
    // 17代表word保存成pdf
    public static final int WD2PDF = 17;
    public static final int PPT2PDF = 32;
    public static final int XLS2PDF = 0;
    /**
     * @param resourceType 资源类型
     * @param
     * @return TODO 文件转换
     * @author shenjianhu:
     * @version 创建时间:2017年4月8日 下午9:07:33
     */
    public static String formatConvertImage(String resourceType, String resourcePath) {
   
        String pages=null;
        String resource = resourcePath.substring(0, resourcePath.lastIndexOf("."));
        resource = resource.trim(); //文件名。前面有空格时,去掉该空格
        if (resourceType.equalsIgnoreCase(DOC) || resourceType.equalsIgnoreCase(DOCX)) {
   
            //word转成pdf和图片
            word2pdf(resourcePath, resource + ".pdf");
            pages = pdf2Image(resource + ".pdf");
        } else if (resourceType.equalsIgnoreCase(PDF)) {
   
            //pdf转成图片
            pages = pdf2Image(resourcePath);
        } else if (resourceType.equalsIgnoreCase(XLS) || resourceType.equalsIgnoreCase(XLSX)) {
   
            //excel文件转成图片
            excel2pdf(resourcePath, resource + ".pdf");
            pages = pdf2Image(resource + ".pdf");
        } else if (resourceType.equalsIgnoreCase(PPT) || resourceType.equalsIgnoreCase(PPTX)) {
   
            ppt2pdf(resourcePath, resource+".pdf");
            pages = pdf2Image(resource+".pdf");
        } else if (resourceType.equalsIgnoreCase(MP4)) {
   
            //视频文件不转换
            pages =null;
        }
        return pages;
    }

    public static String formatConvertPdf(String resourceType, String resourcePath) {
   
        String pages=null;
        String resource = resourcePath.substring(0, resourcePath.lastIndexOf("."));
        resource = resource.trim(); //文件名。前面有空格时,去掉该空格
        if (resourceType.equalsIgnoreCase(DOC) || resourceType.equalsIgnoreCase(DOCX)) {
   
            //word转成pdf和图片
            word2pdf(resourcePath, resource + ".pdf");
            pages = resource + ".pdf";
        } else if (resourceType.equalsIgnoreCase(PDF)) {
   
            //pdf转成图片
            pdf2Image(resourcePath);
            pages = resource + ".pdf";
        } else if (resourceType.equalsIgnoreCase(XLS) || resourceType.equalsIgnoreCase(XLSX)) {
   
            //excel文件转成图片
            excel2pdf(resourcePath, resource + ".pdf");
            pages = resource + ".pdf";
        } else if (resourceType.equalsIgnoreCase(PPT) || resourceType.equalsIgnoreCase(PPTX)) {
   
            ppt2pdf(resourcePath, resource+".pdf");
            pages = resource + ".pdf";
        } else if (resourceType.equalsIgnoreCase(MP4)) {
   
            //视频文件不转换
            pages =null;
        }

        return pages;
    }
    /**
     *
     * @param resourcePath 传入源文件的地址,或者转为PDF后的文件地址
     * @throws IOException
     */
    public static void DelPdfFile(String resourcePath) throws IOException {
   
        String resource = resourcePath.substring(0, resourcePath.lastIndexOf("."));
        resource = resource.trim(); //文件名。前面有空格时,去掉该空格
        Path path2 = Paths.get(resource + ".pdf");
        Files.delete(path2);
        new BaseBean().writeLog("PDF文件被删除 :=>", resource + ".pdf");
    }

    /**
     *
     * @param resourcePath 传入源文件的地址,或者转为PDF后的文件地址
     * @throws IOException
     */
    public static void DelImages(String resourcePath) throws IOException {
   
        File basefile = new File(resourcePath.substring(0, resourcePath.lastIndexOf(".")));
        remove(basefile);
        basefile.delete();
        new BaseBean().writeLog("图片文件夹被删除 : =>", basefile );
    }

    /**
     * 删除指定文件夹下的全部内容
     * @param file
     */
    public static void remove(File file) {
   
        File[] files = file.listFiles();//将file子目录及子文件放进文件数组
        if (files != null) {
   //如果包含文件进行删除操作
            for (int i = 0; i < files.length; i++) {
   
                if (files[i].isFile()) {
   //删除子文件
                    files[i].delete();
                } else if (files[i].isDirectory()) {
   //通过递归方法删除子目录的文件
                    remove(files[i]);
                }
                files[i].delete();//删除子目录
            }
        }
    }

    /**
     * @param pptfile
     * @param imgfile TODO  ppt转换成图片
     * @author shenjianhu:
     * @version 创建时间:2017年4月18日 下午3:08:11
     */
    public static  String ppt2Image(String pptfile, String imgfile) {
   
        String imageDir = pptfile.substring(0, pptfile.lastIndexOf("."));
        String files = "";
        File dir = new File(imageDir);
        if (!dir.exists()) {
   
            dir.mkdirs();
        }
        ActiveXComponent app = null;
        try {
   
            ComThread.InitSTA();
            app = new ActiveXComponent("KWPP.Application");
            app.setProperty("Visible", true);
            Dispatch ppts = app.getProperty("Presentations").toDispatch();
            Dispatch ppt = Dispatch.call(ppts, "Open", pptfile, true, true, true).toDispatch();
            Dispatch.call(ppt, "SaveCopyAs", imgfile, 17);
            Dispatch.call(ppt, "Close");
        } catch (Exception e) {
   
            ComThread.Release();
            new BaseBean().writeLog("ppt转换图片异常==>" + e.getMessage());
            e.printStackTrace();
        } finally {
   
            String[] filenamelist = dir.list();
            for (int i = 0; i < filenamelist.length; i++) {
   
                String tempfile = dir+"/"+filenamelist[i];
                files = files +  tempfile + ',';

            }
            files = files.substring(0,files.length() -1); //去掉最后一个‘,’
            app.invoke("Quit");
            ComThread.Release();
        }
        return files;
    }

    /**
     * WORD转HTML
     *
     * @param docfile  WORD文件全路�?
     * @param htmlfile 转换后HTML存放路径
     */
    public static void wordToHtml(String docfile, String htmlfile) {
   
        // 启动word应用程序(Microsoft Office Word 2003)
        ActiveXComponent app = null;
        System.out.println("*****正在转换...*****");
        try {
   
            ComThread.InitSTA();
            app = new ActiveXComponent("KWPS.Application");
            // 设置word应用程序不可�?
            app.setProperty("Visible", new Variant(false));
            // documents表示word程序的所有文档窗口,(word是多文档应用程序�?
            Dispatch docs = app.getProperty("Documents").toDispatch();
            // 打开要转换的word文件
            Dispatch doc = Dispatch.invoke(
                    docs,
                    "Open",
                    Dispatch.Method,
                    new Object[]{
   docfile, new Variant(false),
                            new Variant(true)}, new int[1]).toDispatch();
            // 作为html格式保存到临时文�?
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{
   
                    htmlfile, new Variant(WORD2HTML)}, new int[1]);
            // 关闭word文件


            Dispatch.call(doc, "Close", new Variant(false));
        } catch (Exception e) {
   
            ComThread.Release();
            new BaseBean().writeLog("word2html==>" + e.getMessage())
  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东枫落定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值