2014-09-22 139 views
1

我想刪除docx中的特定行,如果它有特定的單詞,比如說「殺手」。如何刪除docx4j中特定內容的行

如何使用docx4j編寫程序? 如果我將它替換爲空數據,該行將仍然存在。我想刪除整條線。 我試過這樣的,

private void replacePlaceholders(WordprocessingMLPackage targetDocument, 
      String nameOfTheInvitedGuest) throws JAXBException { 

     List<Object> texts = targetDocument.getMainDocumentPart() 
       .getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true); 

     System.out.println(texts.size()); 
     Iterator<Object> itr = texts.iterator(); 

     while (itr.hasNext()) { 
      Object obj = itr.next(); 

      Text text = (Text) ((JAXBElement) obj).getValue(); 

      // System.out.println(text.getValue()); 

      if (text.getValue().contains("Hulk Hogan")) { 
       itr.remove(); 
      } 

      else { 
       String textValue = replacePlaceholderOfInvitedGuestWithGivenName(
         nameOfTheInvitedGuest, text.getValue()); 

       for (Object key : templateProperties.keySet()) { 
        textValue = textValue.replaceAll("\\$\\{" + key + "\\}", 
          (String) templateProperties.get(key)); 
       } 

       text.setValue(textValue); 
      } 


     } 

     System.out.println(texts.size()); 
    } 

但它仍然顯示在docx文件中。

回答

1

A Text元素在docx文件中有父元素。該文本將位於Run之內,而該文本將依次位於像段落(P節點)或表格單元格之類的塊元素內。如果您想要根據文本內容刪除特定的塊元素,則一旦找到相關的文本元素,就需要向上移動父元素並將其刪除 - 例如,如果最終父元素一個段落節點,刪除。

如果說,一個段落在Word中顯示爲3行,並且您試圖刪除該段落中的第二行,那麼您將遇到不同且更具挑戰性的問題。

0

也許這將幫助即將成爲人:

if(((org.docx4j.wml.Text) o2).getValue().contains("WhatYouWant")) { 
           // if your text contains "WhatYouWant" then... 

           Object o4 =((org.docx4j.wml.Text)o2).getParent(); 
           //gets R 
           Object o5 = ((org.docx4j.wml.R) o4).getParent(); 
           // gets P 
           Object o6 = ((org.docx4j.wml.P) o5).getParent(); 
           // gets SdtElement 
           ((List<List<Object>>) o6).remove(o5); 
           // now you remove your P (paragraph) 

           } 

我有一個內容控制(SdtElement),但我需要把它放在列表<列表<對象>>真的不知道爲什麼,但... 。你可能還有別的東西,所以在複製/粘貼之前檢入你的document.xml。

這是其他人誰也很難,像我一樣瞭解docx4j

0

你可以使用Apache POI,如下圖所示,從DOCX文件中刪除文本。

public static void removeTextFromDocx(FileInputStream inpudocxfile, String stringToBeReplaced, 
     String stringToBeReplacedWith, FileOutputStream outputdocxfile) { 
    XWPFDocument document = null; 
    try { 
     //loading docx file 
     document = new XWPFDocument(inpudocxfile); 
     for (XWPFParagraph paragraph : document.getParagraphs()) { 
      List<XWPFRun> runs = paragraph.getRuns(); 
      for (XWPFRun run : runs) { 
       //reading an entire paragraph. So size of list is 1 and index of first element is 0 
       String text = run.getText(0); 
       if (text != null) { 
        if (text.contains(stringToBeReplaced)) { 
         text = text.replace(stringToBeReplaced, stringToBeReplacedWith); 
         text = text.trim(); 
         run.setText(text, 0); 
        } 
       } 
      } 
     } 
     for (XWPFTable table : document.getTables()) { 
      for (XWPFTableRow row : table.getRows()) { 
       for (XWPFTableCell cell : row.getTableCells()) { 
        for (XWPFParagraph paragraph : cell.getParagraphs()) { 
         for (XWPFRun run : paragraph.getRuns()) { 
          String text = run.getText(0); 
          if (text != null) { 
           if (text.contains(stringToBeReplaced)) { 
            text = text.replace(stringToBeReplaced, stringToBeReplacedWith); 
            text = text.trim(); 
            run.setText(text, 0); 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
     document.write(outputdocxfile); 
    } catch (IOException e) { 
     LOGGER.error("Could not create outputdocxFile --> IOEXception" + e); 
    } 
}