我使用Apache POI从MS word文件生成Html(.doc)

时间:2014-04-29 17:54:47

标签: java apache-poi

我正在使用Apache POI从MS word文件(.doc)生成Html。我想将.doc中的图像添加到html但我无法做到这一点。我得到了.docx的解决方案,给出的方法如下敌人.docx。

private void processImage(Element wrap, List<XWPFPicture> pics)
               throws IOException {

        int pos = output.lastIndexOf(".");
        String path = output.substring(0, pos).concat(File.separator);
        File folder = new File(path);
                System.out.println("path="+path);
        if(!folder.canRead())
            folder.mkdirs();
        folder = null;

        for(XWPFPicture pic : pics)
                {    

            XWPFPictureData data = pic.getPictureData();
            System.out.println("image extension="+data.suggestFileExtension());
                        {
                        System.out.println("Data name="+data.getFileName());
                        ByteArrayInputStream is = new ByteArrayInputStream(data.getData());
                     ImageConverter.convertFormat(path,"c:/hello.jpg","jpg");

                        try
                        {
                         BufferedImage image = ImageIO.read(is);

            // TODO image type convert   like .tif etc.
            String imgFullPath = path.concat(data.getFileName());
            {// extract picture
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(new File(imgFullPath));
                    fos.write(data.getData());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }finally{
                    if(fos != null) fos.close();
                }
            }
            {// add picture to html page
                //TODO get img relative path for showing html page when on server &&  get the picture style, scaling in the docx file (with description style?)
              System.out.println("img full Path="+imgFullPath);
                          int index=imgFullPath.indexOf('/');
                          imgFullPath=imgFullPath.substring(index+1);
                          index=imgFullPath.lastIndexOf('\\');
                          String cu_path=imgFullPath.substring(index+1);
                          String imgFolder=imgFullPath.substring(0, index);
                          index=imgFolder.lastIndexOf('\\');
                          imgFolder=imgFolder.substring(index+1);
                          System.out.println("imgFolder="+imgFolder);
                           System.out.println("cur_Path="+cu_path);
                           imgFullPath="./"+imgFolder+"/"+cu_path;
                          System.out.println(" After remove img full Path="+imgFullPath);
                            Element img = htmlDocumentFacade.createImage(imgFullPath);
                if(!StringUtil.isEmpty(pic.getDescription())){
                    img.setAttribute("Title", pic.getDescription());
                }
                if(image != null && image.getWidth() > 600){
                    img.setAttribute("width", "600px");
                }
                img.setAttribute("align", "center");
                wrap.appendChild(img);

            }  
                        if("gif".equals(data.suggestFileExtension()))
                        {
                           System.out.println("File name="+data.getFileName());
                        }
        }
                 catch(Exception ex)
                        {
                            continue;
                        }

    }
                } }

没有太多可用于此的文档或教程。 Javadoc也没有太多有用的信息。 根据上面的代码,我试图添加图像,但它不起作用。 :/

1 个答案:

答案 0 :(得分:0)

根据Apache POI Documentation HWPF适用于&#34; .doc&#34;文件,而XWPF用于&#34; .docx&#34;文件。

根据Java DocsgetPicturesTable()方法可以帮助您提取所需的图片。

希望这有帮助。