JAVA 利用POI实现DOC转HTML的方法及BUG修改

关于DOC转HTML的初始代码详见:

http://chembo.iteye.com/blog/1510536

在这部分代码即有

wordToHtmlConverter.setPicturesManager()

也通过

wordDocument.getPicturesTable().getAllPictures()

将Document的Pictures保存到硬盘

实际操作过程中会发现这两个方法得到的Pictures数量是不一致的,后部分代码不包含通过Doc绘制的图片,而前面部分代码能获得这部分图片,于是将这部分代码改造

converter.setPicturesManager(new PicturesManager() {
	            public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
	            	try {
						FileOutputStream fos=new FileOutputStream(path + suggestedName);
						fos.write(content);
					} catch (Exception e) {
						e.printStackTrace();
					}
	            	return suggestedName;
	            }
	        });

去掉

wordDocument.getPicturesTable().getAllPictures()

这部分代码。

这样改造能解决图片不一致的问题,却发现另外一个问题,在转成html的时候word中正常的图片能在网页中显示正常的高度和宽度,但是针对通过Doc绘制的图片,img标签是没有高度和宽度的。会发现在html中呈现原始大小与word不保持一致。

通过阅读WordToHtmlConverter源代码发现:

protected void processDrawnObject(HWPFDocument doc,
			CharacterRun characterRun, OfficeDrawing officeDrawing,
			String path, Element block) {
		/* 302*/Element img = htmlDocumentFacade.createImage(path);
		/* 303*/block.appendChild(img);
	}

这段代码只在html中生成了img标签而没有设置它的高度和宽度。

于是将这个方法改造一下:

 protected void processDrawnObject( HWPFDocument doc,
            CharacterRun characterRun, OfficeDrawing officeDrawing,
            String path, Element block )
    {
        Element img = htmlDocumentFacade.createImage( path );
        float width = (float) (officeDrawing.getRectangleRight() - officeDrawing
				.getRectangleLeft()) / 1440F;
		float height = (float) (officeDrawing.getRectangleBottom() - officeDrawing
				.getRectangleTop()) / 1440F;
		img.setAttribute(
				"style",
				(new StringBuilder()).append("width:").append(width)
						.append("in;height:").append(height)
						.append("in;vertical-align:text-bottom;")
						.toString());
        block.appendChild( img );
    }

大功告成。

转载于:https://my.oschina.net/caiw/blog/369058

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值