java实现word文档转pdf在线浏览

4 篇文章 0 订阅
1 篇文章 0 订阅
用到的jar
 jacob.jar
http://mvnrepository.com/artifact/net.sf.jacob-project/jacob
/**
	 * doc转pdf在线浏览
	 * 
	 */
	public final DataMap queryApplicantDOC2PDF(final DataMap para) throws Exception {

		String sbrid = para.getString("sbrid");

		StringBuffer sqlBF = new StringBuffer();

		sqlBF.setLength(0);
		sqlBF.append(" select sbrdoc, upper(zjhm) zjhm ");
		sqlBF.append("   from vt.applicant ");
		sqlBF.append("  where sbrid = ? ");

		this.sql.setSql(sqlBF.toString());
		sql.setString(1, sbrid);
		DataSet dsApplicant = sql.executeQuery();

		if (dsApplicant.size() <= 0) {
			return null;
		}
		String zjhm = dsApplicant.getString(0, "zjhm");
		String filePath = "temp" + File.separator + GlobalVars.APP_ID;
		String path = filePath + File.separator + zjhm;
		File docFile = new File(path + File.separator + zjhm + ".doc");
      	File docPdfFile = new File(path + File.separator + zjhm + "doc.pdf");
      
     	 //减少数据库负担,直接存在服务器上缓存了
		if (!docPdfFile.exists()) { 
			Blob sbrpdf = dsApplicant.getBlob(0, "sbrdoc");
			int len = (new BigDecimal(sbrpdf.length())).intValue();
			byte[] byteDoc = sbrpdf.getBytes(1, len);
			path = path + File.separator;
            File dir = new File(path);
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    throw new Exception("创建目录失败!");
                }
            }
			FileIOUtil.writeBytesToFile(byteDoc, docFile);
		}
      	try {
          app = new ActiveXComponent("Word.Application");
          Dispatch documents = app.getProperty("Documents").toDispatch();
          Dispatch document = Dispatch.call(documents, "Open", wordFile, false,
                                            true).toDispatch();
          // 判断文件存在
          File target = new File(docPdfFile);  
           if (!docPdfFile.exists()) {  
               Dispatch.call(document, "SaveAs", pdfFile, 17);
               Dispatch.call(document, "Close", false);
           }
         }catch(Exception e) {
          System.out.println("转换失败"+e.getMessage());
         }finally {
          // 关闭office
          app.invoke("Quit", 0);
         }
      
      	String filePath = Path + File.separator + zjhm + "doc.pdf"
        String fileName = zjhm + "doc.pdf"
      	FileInputStream inputstream = null;
        HttpServletResponse response = null;
		try {
			inputstream = new FileInputStream(filePath);
			FileIOUtil.writeStreamToResponse(inputstream, fileName, response);
		} catch (IOException e) {
			throw new Exception("文件读取异常:" + e.getMessage());
		} finally {
			try {
				if (inputstream != null) {
					inputstream.close();
				}
			} catch (Exception e) {
				throw new Exception("文件损坏或不存在:"
						+ e.getMessage());
			}
		}
          return null;
      }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值