Java版Word開發工具Aspose.Words功能解析:查找和替換Word文檔中的文本
MS Word提供了一種簡單的方法來查找和替換文檔中的文本。查找和替換文本的一種流行用例之一可能是在文檔之間的敏感信息在各個實體之間共享之前,對其進行刪除或替換。但是,手動過程可能需要您安裝MS Word并分別更新每個文檔。在這種情況下,這將非常方便且省時,尤其是當您在桌面或Web應用程序中集成了查找和替換功能時。
在本文中,我將演示如何使用Java以編程方式在Word(DOC / DOCX)文檔中查找和替換文本(單詞或短語)。分步指南和代碼示例將介紹在Word文檔中查找和替換文本的各種方案。如果你還沒有用過Java版Aspose.Words可以點擊這里下載最新版測試。
因此,來看看如何在各種情況下使用Java查找和替換Word文檔中的文本。
- 使用Java在Word DOC / DOCX中查找和替換文本
- 根據Word DOC / DOCX中的正則表達式模式替換相似的單詞
- 在Word文檔的頁眉/頁腳中查找和替換文本
- 在Word DOC / DOCX中用元字符查找和替換文本
①使用Java查找和替換Word DOC / DOCX文件中的文本
讓我們從解決一個簡單的查找和替換場景開始,在該場景中,我們將在輸入的Word文檔中找到單詞“ Sad”。以下是執行此操作的步驟。
- 創建Document類的實例,然后將Word文檔的路徑傳遞給它。
- 使用Document.getRange.replace(String,String,FindReplaceOptions)方法查找和替換文本。
- 使用Document.save(String)方法保存文檔。
下面的代碼示例演示如何使用Java在Word DOCX中查找和替換文本。
// Load a Word DOCX document Document doc = new Document("document.docx"); // Find and replace text in the document doc.getRange().replace("sad", "[replaced]", new FindReplaceOptions(FindReplaceDirection.FORWARD)); // Save the Word document doc.save("Find-And-Replace-Text.docx");
輸入Word文檔
以下是找到并替換單詞“ sad”后的輸出。
②使用Java查找和替換DOC / DOCX中的相似單詞
還可以自定義API,以根據相似度查找和替換文本。例如,單詞“ sad”,“ mad”和“ bad”遵循類似的模式,以“ ad”結尾。電子郵件ID是此類文本的另一個示例。在這種情況下,您可以定義正則表達式模式來查找和替換具有特定模式的所有文本出現。以下是實現此目的的步驟。
- 創建Document類的實例,然后將Word文檔的路徑傳遞給它。
- 使用Pattern.compile()方法定義一個正則表達式模式,并將其傳遞給Document.getRange()。replace(模式模式,字符串替換,FindReplaceOptions選項)方法。
- 使用Document.save(String)方法保存更新的文檔。
以下代碼示例顯示了如何使用Java根據特定的模式查找和替換相似的單詞。
// Load a Word DOCX document Document doc = new Document("document.docx"); // Find and replace similar words in the document FindReplaceOptions options = new FindReplaceOptions(); doc.getRange().replace(Pattern.compile("[B|S|M]ad"), "[replaced]", options); // Save the Word document doc.save("Find-And-Replace-Text.docx");
以下是更新相似單詞后的Word文檔的屏幕截圖。
③替換Word文檔的頁眉/頁腳中的文本
Aspose.Words還允許您僅在Word文檔的頁眉/頁腳中查找和替換文本。以下是執行此操作的步驟。
- 創建Document類的實例,然后將Word文檔的路徑傳遞給它。
- 使用Document.getFirstSection()。getHeadersFooters()方法獲取文檔的HeaderFooterCollection。
- 在HeaderFooter對象中檢索特定的頁眉/頁腳。
- 使用HeaderFooter.getRange()。replace()方法來查找和替換文本。
- 保存更新的Word文檔。
下面的代碼示例演示如何使用Java查找和替換Word文檔的頁眉/頁腳中的文本。
// Load a Word DOCX document Document doc = new Document("document.docx"); // Access header footer collection HeaderFooterCollection headersFooters = doc.getFirstSection().getHeadersFooters(); HeaderFooter footer = headersFooters.get(HeaderFooterType.FOOTER_PRIMARY); // Set find and replace options FindReplaceOptions options = new FindReplaceOptions(); options.setMatchCase(false); options.setFindWholeWordsOnly(false); footer.getRange().replace("This is footer of the document.", "Copyright (C) 2020 by Aspose Pty Ltd.", options); // Save the Word document doc.save("Find-And-Replace-Text.docx");
以下屏幕快照顯示了Word文檔頁腳中的更新文本。
④使用Java在DOCX中使用元字符查找和替換文本
在某些情況下,需要查找并替換分為多行或多段的短語。在這種情況下,您將必須注意段落,節或換行符。Java的Aspose.Words使您輕松地輕松處理此類情況變得簡單。以下是可用于不同休息時間的元字符:
- &p:段落中斷
- &b:分節符
- &m:分頁符
- &l:換行
下面的代碼示例演示如何在Word文檔中使用段落分隔符查找和替換文本。
// Load a Word DOCX document Document doc = new Document("document.docx"); // Set options FindReplaceOptions options = new FindReplaceOptions(); // Disable matching case and finding whole words only options.setMatchCase(false); options.setFindWholeWordsOnly(false); // Replace text with paragraph break doc.getRange().replace("First paragraph ends.&pSecond paragraph starts.", "[replaced]", options); // Save the Word document doc.save("Find-And-Replace-Text.docx");
以下是輸出Word文檔的屏幕截圖。