2003高版本word转pdf的功能如何实现?
最近项目要用到一个word转pdf功能,网上找了很久,试了很多方法,
http://my.oschina.net/mingpeng/blog/337198
只有这个还原度比较高,没多少乱码,但是不支持2003版本
说说最原始的需求吧,一个归档功能,原来的是doc或者docx 现在要转换为pdf版本。
在linux上的openoffice转换还原度很低,经理想法是下载在本地,本地office转好再上传到服务器.
第一,下载后用户把文件保存在哪?这个无法确定.
第二,由于安全机制,用户不选择文件的情况下,浏览器的js是无法做到选中文件上传到服务器(转换好文件之后)
上面提到的方法只支持高版本的转换.
哪位牛牛有这方面的经验望指点.
希望是java代码就能做到,有开源Linux上的插件也可以,但是要求还原度高。
 所有回答列表(2)
所有回答列表(2)
				/**OfficeToPdf
     * @param word文件路径
     * @param pdf文件路径
     * @param formatType(Html,Pdf,Docx)
     * @return 
     */
    public boolean officeConvert(String office,String target,String formatType) {
        /*
         * office转换结果参数
         * 0:Doc
         * 1:Dot
         * 2-5:Txt
         * 6:Rtf
         * 7:Txt
         * 8,10:htm
         * 11:xml
         * 12,16:Docx
         * 13:Docm
         * 14:Dotx
         * 15:Dotm
         * 17: 32:  57:  pdf
         */
        int format = 99 ;
        boolean result = false;
        String filetype = office.substring(office.lastIndexOf(".") + 1, office.length()).toLowerCase();
        switch(formatType){
            case "Html":
                if("doc".equals(filetype) || "docx".equals(filetype)){
                    format = 8;// word
                 }else {
                     format = 99;
                 }
                break;            
            case "Pdf":
                if("doc".equals(filetype) || "docx".equals(filetype)){
                    format = 17;// word
                 }else if("ppt".equals(filetype) || "pptx".equals(filetype)){
                     format = 32 ;//ppt
                 }else if("xls".equals(filetype) || "xlsx".equals(filetype)){
                     format = 57;// Excel
                 }
                break;
            case "Docx":
                format = 16;
                break;
            case "txt":
                format = 7;
                break;
            default:
                format = 99;
                break;    
        }
        if(format == 99){
            log.info("Office转换终止:转换类型不明确!");
            return result;
        }
        if("txt".equals(filetype) || "doc".equals(filetype) || "docx".equals(filetype)||"ppt".equals(filetype) || "pptx".equals(filetype)||"xls".equals(filetype) || "xlsx".equals(filetype)||"docm".equals(filetype)){
            log.info("Office转换进度:源文件类型校验通过!(路径为:" + office + ")");
        }else{
            log.info("Office转换终止:源文件类型不正确!");
            return result;
        }
        ActiveXComponent app = jacobPool.getConnection(filetype); // 启动Office
        String officeProperty = "";
        if("txt".equals(filetype) || "doc".equals(filetype) || "docx".equals(filetype) || "docm".equals(filetype)){
            officeProperty = "Documents";// word
         }else if("ppt".equals(filetype) || "pptx".equals(filetype)){
             officeProperty = "Presentations";// PowerPoint
         }else if("xls".equals(filetype) || "xlsx".equals(filetype)){
             officeProperty = "Workbooks";// Excel
         }
        try {
               log.info("Office转换进度:"+filetype+"转" + formatType + "开始!");
                (new File(target.substring(0, target.lastIndexOf("/")))).mkdirs(); // 如果文件夹不存在,则建立新文件夹
                // 设置word不可见
                Dispatch docs = app.getProperty(officeProperty).toDispatch();
                // 打开word文件
                Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { office.replaceAll("/", "\\\\"), new Variant(false), new Variant(true) }, new int[1]).toDispatch();
                // 作为pdf格式保存
                Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { target.replaceAll("/", "\\\\"), new Variant(format) }, new int[1]);
                Variant f = new Variant(false);
            if ("Presentations".equals(officeProperty)) {//ppt文档 需要特殊处理服务器显示最后再杀死进程
                app.invoke("Quit", new Variant[0]);
                app = null;
                ComThread.Release();  
            }    else{
                    Dispatch.call(doc, "Close", f);
            }
                log.info("Office转" + formatType + "成功!(文件生成路径为:" + target + ")");
                result = true;
            //暂时解决,待修改
                Dispatch.call(app, "Quit");
                jacobPool.freeConnection(null);
        } catch (Exception ex) {
            log.info("Office转" + formatType + "异常(Exception)!",ex);
            jacobPool.removeFromfreeConnections(app);
            return result;
        } catch (Error ex) {
            log.info("Office转" + formatType + "异常(Error)!",ex);
            jacobPool.removeFromfreeConnections(app);
            return result;
        } finally {
            log.info("Office转" + formatType + "结束!");
        }
        return result;
    }
    
- 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?
 相关问答
相关问答
				- 等 word文档如何在网页显示出来?
- 等 多文件word、图片、pdf 合并转化成pdf?
- 完 有没有开发过服务器端的word文件显示在客户端的weboffice控件?
- 等 java读取word内容并保持word中的格式如何实现?
- 等 java如何实现复杂word表格导出?
- 等 如何用java实现监控office中诸如word的复制,粘贴,剪切等行为进程,并防止文档中文字被复制,修改,删除 ?
- 完 jacob插件将word转换成htm文件显示为什么乱码?
- 完 如何通过apache POI技术来读取Word文档,并把Word文档的原来格式完整地显示在html网页?
- 等 JAVA POI如何将上标下标写入word文件中?
- 等 iOffice在线编辑word文档的ActiveX控件 有免费可用的吗






 最近浏览
最近浏览
				 
     
                 
                