2016-09-24 130 views
1

我需要使用Apache POI或任何其他可能執行此工作的庫將圖像替換爲Word中的其他圖像,以獲得幫助。我知道如何使用Apache POI替換一個單詞,但我無法找到替代圖像的方法。用Apache POI替換圖像

public static void main(String[] args) throws FileNotFoundException { 

    String c22 = "OTHER WORD"; 

    try { 
     XWPFDocument doc = new XWPFDocument(OPCPackage.open("imagine.docx")); 
     for (XWPFParagraph p : doc.getParagraphs()) { 
      List<XWPFRun> runs = p.getRuns(); 
      if (runs != null) { 
       for (XWPFRun r : runs) { 
        String text = r.getText(0); 
        if (text != null) { 
         String imgFile = "imaginedeschis.jpg"; 
         try (FileInputStream is = new FileInputStream(imgFile)) { 
          r.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, 
              Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels 
          text = text.replace("1ST WORD", c22); 
         } // 200x200 pixels 
         r.setText(text, 0); 
        } 
       } 
      } 
     }  
     doc.write(new FileOutputStream("output.docx")); 
    } catch (InvalidFormatException | IOException m){ } 
} 

回答

1

我正在使用下面的Java代碼來替換Word文檔(* .docx)中的一個圖像。如果有人有更好的方法,請分享。

public XWPFDocument replaceImage(XWPFDocument document, String imageOldName, String imagePathNew, int newImageWidth, int newImageHeight) throws Exception { 
    try { 
     LOG.info("replaceImage: old=" + imageOldName + ", new=" + imagePathNew); 

     int imageParagraphPos = -1; 
     XWPFParagraph imageParagraph = null; 

     List<IBodyElement> documentElements = document.getBodyElements(); 
     for(IBodyElement documentElement : documentElements){ 
      imageParagraphPos ++; 
      if(documentElement instanceof XWPFParagraph){ 
       imageParagraph = (XWPFParagraph) documentElement; 
       if(imageParagraph != null && imageParagraph.getCTP() != null && imageParagraph.getCTP().toString().trim().indexOf(imageOldName) != -1) { 
        break; 
       } 
      } 
     } 

     if (imageParagraph == null) { 
      throw new Exception("Unable to replace image data due to the exception:\n" 
        + "'" + imageOldName + "' not found in in document."); 
     } 
     ParagraphAlignment oldImageAlignment = imageParagraph.getAlignment(); 

     // remove old image 
     document.removeBodyElement(imageParagraphPos); 

     // now add new image 

     // BELOW LINE WILL CREATE AN IMAGE 
     // PARAGRAPH AT THE END OF THE DOCUMENT. 
     // REMOVE THIS IMAGE PARAGRAPH AFTER 
     // SETTING THE NEW IMAGE AT THE OLD IMAGE POSITION 
     XWPFParagraph newImageParagraph = document.createParagraph();  
     XWPFRun newImageRun = newImageParagraph.createRun(); 
     //newImageRun.setText(newImageText); 
     newImageParagraph.setAlignment(oldImageAlignment); 
     try (FileInputStream is = new FileInputStream(imagePathNew)) { 
      newImageRun.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imagePathNew, 
         Units.toEMU(newImageWidth), Units.toEMU(newImageHeight)); 
     } 

     // set new image at the old image position 
     document.setParagraph(newImageParagraph, imageParagraphPos); 

     // NOW REMOVE REDUNDANT IMAGE FORM THE END OF DOCUMENT 
     document.removeBodyElement(document.getBodyElements().size() - 1); 

     return document; 
    } catch (Exception e) { 
     throw new Exception("Unable to replace image '" + imageOldName + "' due to the exception:\n" + e); 
    } finally { 
     // cleanup code 
    } 
} 

請訪問https://bitbucket.org/wishcoder/java-poi-word-document/wiki/Home像更多的例子:在Word文檔

  • 打開現有的Microsoft Word文檔(* .DOCX)
  • 克隆表和新數據添加到克隆表
  • 更新現有的表格 - >文檔中的單元格數據
  • 更新文檔中現有的超鏈接
  • R現有文檔中的圖像
  • 保存更新Microsoft Word文檔(* .docx)