手机浏览器显示word文档

项目有个需求,需要在微信企业号中word文档在线浏览。找了好多资料,没找到什么好办法。无奈只能转换成图片显示。

准备
jacob.jar
office2010
这个只能是word转pdf (找过好多插件可以word直接转图片,但是效果不好)

/**
     * word转换pdf
     * @param sfileName 源文件路径
     * @param toFileName 目标文件路径
     * @param folderPath 目标文件所在文件夹路径
     */
    public static void wordToPDF(String sfileName, String toFileName, String folderPath) {

        System.out.println("启动Word...");
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent(ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_START);
            app.setProperty(ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_01, new Variant(false));
            Dispatch docs = app.getProperty(ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_02).toDispatch();
            // doc = Dispatch.call(docs, "Open" , sourceFile).toDispatch();
            doc = Dispatch.invoke(
                    docs,
                    ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_03,
                    Dispatch.Method,
                    new Object[] { sfileName, new Variant(false),
                            new Variant(true) }, new int[1]).toDispatch();
            System.out.println("打开文档..." + sfileName);
            System.out.println("转换文档到PDF..." + toFileName);
            File tofile = new File(toFileName);
            if (tofile.exists()) {
                tofile.delete();
            }
             FileOperateHelper.newFolder(folderPath);
            // Dispatch.call(doc, "SaveAs", destFile, 17);
            Dispatch.invoke(doc, ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_04, Dispatch.Method, new Object[] {
                    toFileName, new Variant(WDFORMATPDF) }, new int[1]);
        } catch (Exception e) {
            RmProjectHelper.logError("========Error:文档转换失败:", e);
        } finally {
            Dispatch.call(doc, ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_05, false);
            System.out.println("关闭文档");
            if (app != null)
                app.invoke(ISystemConstant.DICTIONARY_FILE_CONVERT_WORD_PROPERTY_06, new Variant[] {});
            // 如果没有这句话,winword.exe进程将不会关闭
            ComThread.Release();
        }
/**
     * 启动word进程
     */
    public static final String DICTIONARY_FILE_CONVERT_WORD_START = "Word.Application";
    /**
     * 调用方法
     */
    public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_01 = "Visible";
    /**
     * 调用方法
     */
    public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_02 = "Documents";
    /**
     * 调用方法
     */
    public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_03 = "Open";
    /**
     * 调用方法 
     */
    public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_04 = "SaveAs";
    /**
     * 调用方法 关闭
     */
    public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_05 = "Close";
    /**
     * 调用方法 退出
     */
    public static final String DICTIONARY_FILE_CONVERT_WORD_PROPERTY_06 = "Quit";

pdf有了 接下来转换图片

public static List<String> pdfToImage(String pdfPath,String imgPath, String rootPath) throws IOException {  
        FileChannel channel = null;
        List<String> list = new ArrayList<String>();
        try {
            File file = new File(  
                    pdfPath);  
            RandomAccessFile raf = new RandomAccessFile(file, "r");  
            channel = raf.getChannel();  
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel  
                    .size()); 
            PDFFile pdffile = new PDFFile(buf);  
            System.out.println("页数: " + pdffile.getNumPages());  

            for (int i = 1; i <= pdffile.getNumPages(); i++) {  
                // draw the first page to an image  
                PDFPage page = pdffile.getPage(i);  

                // get the width and height for the doc at the default zoom  
                Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()  
                        .getWidth(), (int) page.getBBox().getHeight());  
                // generate the image  
                Image img = page.getImage(rect.width, rect.height, // width &  
                                                                    // height  
                        rect, // clip rect  
                        null, // null for the ImageObserver  
                        true, // fill background with white  
                        true // block until drawing is done  
                        );  
                BufferedImage tag = new BufferedImage(rect.width, rect.height,  
                        BufferedImage.TYPE_INT_RGB);  
                tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,  
                        null); 

                list.add(imgPath + i + ISystemConstant.DICTIONARY_IMAGE_SUFFIX);

                FileOutputStream out = new FileOutputStream(  
                        rootPath + imgPath  
                                + i + ISystemConstant.DICTIONARY_IMAGE_SUFFIX); // 输出到文件流  
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
                encoder.encode(tag); // JPEG编码  
                out.close(); 
            }  
            return list;
        }finally{
            channel.close();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值