本人用poi 把docx转html,对应文字格式都正确,但是图片不回展示出来,
源码:
XWPFDocument document = new XWPFDocument( new FileInputStream(“D:\crm.docx”) );
XHTMLOptions options = XHTMLOptions.create().indent( 4 );
// Extract image
List<XWPFPictureData> pics=document.getAllPictures();
for (XWPFPictureData pic : pics) {
System.out.println(pic.getPictureType() + file.separator + pic.suggestFileExtension()
+file.separator+pic.getFileName());
byte[] bytev = pic.getData();
FileOutputStream fos = new FileOutputStream(“d:\”+pic.getFileName());
fos.write(bytev);
}
File imageFolder = new File( “D:/vfsroot/1000000/ueditor_upload/images” + fileInName );
options.setExtractor( new FileImageExtractor( imageFolder ) );
// URI resolver
options.URIResolver( new FileURIResolver( imageFolder ) );
源码:
XWPFDocument document = new XWPFDocument( new FileInputStream(“D:\crm.docx”) );
XHTMLOptions options = XHTMLOptions.create().indent( 4 );
// Extract image
List<XWPFPictureData> pics=document.getAllPictures();
for (XWPFPictureData pic : pics) {
System.out.println(pic.getPictureType() + file.separator + pic.suggestFileExtension()
+file.separator+pic.getFileName());
byte[] bytev = pic.getData();
FileOutputStream fos = new FileOutputStream(“d:\”+pic.getFileName());
fos.write(bytev);
}
File imageFolder = new File( “D:/vfsroot/1000000/ueditor_upload/images” + fileInName );
options.setExtractor( new FileImageExtractor( imageFolder ) );
// URI resolver
options.URIResolver( new FileURIResolver( imageFolder ) );
File outFile = new File( fileOutName );
outFile.getParentFile().mkdirs();
OutputStream out = new FileOutputStream( outFile );
XHTMLConverter.getInstance().convert( document, out, options );
通过 for (XWPFPictureData pic : pics) {
System.out.println(pic.getPictureType() + file.separator + pic.suggestFileExtension()
+file.separator+pic.getFileName());
byte[] bytev = pic.getData();
FileOutputStream fos = new FileOutputStream(“d:\”+pic.getFileName());
fos.write(bytev);
}本人可以获取到对应图片,但是怎么在html里面正确的位置展示出来?
解决方案
5
这个你要把图片保存到html可访问的地方,把地址填入进去。或使用内嵌资源的方式。
还有个简单方法,就是调用com接口或Openoffice 直接word转换html即可。
本人书写word转html简单的情况可以应付,复杂的布局文档等是比较麻烦的。
还有个简单方法,就是调用com接口或Openoffice 直接word转换html即可。
本人书写word转html简单的情况可以应付,复杂的布局文档等是比较麻烦的。
15
InputStream is = null; try { is = new FileInputStream(src); XWPFDocument document = new XWPFDocument(is); XHTMLOptions options = XHTMLOptions.create().indent(4); //img的src属性 后面会自动添加/word/media //这里就是images/word/media/ + 图片名字 options.URIResolver(new BasicURIResolver("images")); //>> 文件的保存路径 之后自动会添加 word\media子路径 FileImageExtractor extractor = new FileImageExtractor(new File( htmlImagesPath)); options.setExtractor(extractor); XHTMLConverter.getInstance().convert(document, new FileOutputStream(des), options); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
不需要手动添加代码保存图片