1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > poidoc转换成html乱码 JAVA 利用POI实现DOC转HTML的方法及BUG修改

poidoc转换成html乱码 JAVA 利用POI实现DOC转HTML的方法及BUG修改

时间:2024-01-11 17:30:34

相关推荐

poidoc转换成html乱码 JAVA 利用POI实现DOC转HTML的方法及BUG修改

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

在这部分代码即有

wordToHtmlConverter.setPicturesManager()

也通过

wordDocument.getPicturesTable().getAllPictures()

将Document的Pictures保存到硬盘

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

converter.setPicturesManager(newPicturesManager(){

publicStringsavePicture(byte[]content,PictureTypepictureType,StringsuggestedName,floatwidthInches,floatheightInches){

try{

FileOutputStreamfos=newFileOutputStream(path+suggestedName);

fos.write(content);

}catch(Exceptione){

e.printStackTrace();

}

returnsuggestedName;

}

});

去掉

wordDocument.getPicturesTable().getAllPictures()

这部分代码。

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

通过阅读WordToHtmlConverter源代码发现:

protectedvoidprocessDrawnObject(HWPFDocumentdoc,

CharacterRuncharacterRun,OfficeDrawingofficeDrawing,

Stringpath,Elementblock){

/*302*/Elementimg=htmlDocumentFacade.createImage(path);

/*303*/block.appendChild(img);

}

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

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

protectedvoidprocessDrawnObject(HWPFDocumentdoc,

CharacterRuncharacterRun,OfficeDrawingofficeDrawing,

Stringpath,Elementblock)

{

Elementimg=htmlDocumentFacade.createImage(path);

floatwidth=(float)(officeDrawing.getRectangleRight()-officeDrawing

.getRectangleLeft())/1440F;

floatheight=(float)(officeDrawing.getRectangleBottom()-officeDrawing

.getRectangleTop())/1440F;

img.setAttribute(

"style",

(newStringBuilder()).append("width:").append(width)

.append("in;height:").append(height)

.append("in;vertical-align:text-bottom;")

.toString());

block.appendChild(img);

}

大功告成。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。