如何使用 C# 從 DOCX 移除文字
IronWord 提供多種在 C# 中從 Word 文件移除文字的方法。您可以移除整個段落或特定文字區段,也可以先依文字內容搜尋再移除,同時維持文件結構及格式的完整性。
如何從 DOCX 檔案中刪除文字
- 下載從 Word 文件移除文字的 C# 函式庫
- 載入現有的 Word 文件
- 確定需要刪除的文字內容
- 使用
Remove方法移除選取的文字 - 儲存變更,並將更新後的文件匯出為 DOCX 檔案
- 透過 NuGet 套件管理員安裝 IronWord
- 使用
WordDocument載入您的 DOCX 檔案 - 依索引存取段落或文字區段
- 對目標元素呼叫
Remove() - 儲存修改後的文件
-
1使用 IronWord 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
using IronWord; // Load document WordDocument doc = new WordDocument("document.docx"); // Remove second paragraph doc.Paragraphs[1].Remove(); // Save changes doc.SaveAs("document_modified.docx");Imports IronWord ' Load document Dim doc As New WordDocument("document.docx") ' Remove second paragraph doc.Paragraphs(1).Remove() ' Save changes doc.SaveAs("document_modified.docx")Imports IronWord ' Load document Dim doc As New WordDocument("document.docx") ' Remove second paragraph doc.Paragraphs(1).Remove() ' Save changes doc.SaveAs("document_modified.docx") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronWord,並使用 免費試用
如何從 Word 文件移除整個段落?
若要從 Word 文件中移除段落,請透過 Paragraphs 集合依索引存取該段落。此範例會移除索引為 1 的段落及其所有內容與格式。移除後,文件結構會自動調整,後續段落會向上遞補。
段落索引從 0 開始,第一段的索引是 0,第二段是 1,其餘依此類推。Remove() 方法會從文件結構中移除整個段落元素,包括其中的所有文字區段、格式及圖片或超連結等嵌入內容。
using IronWord;
// Load a DOCX document
WordDocument doc = new WordDocument("text_document.docx");
// Remove the second paragraph
doc.Paragraphs[1].Remove();
// Export the file
doc.SaveAs("text_document_modified.docx");using IronWord;
// Load a DOCX document
WordDocument doc = new WordDocument("text_document.docx");
// Remove the second paragraph
doc.Paragraphs[1].Remove();
// Export the file
doc.SaveAs("text_document_modified.docx");Imports IronWord
' Load a DOCX document
Dim doc As New WordDocument("text_document.docx")
' Remove the second paragraph
doc.Paragraphs(1).Remove()
' Export the file
doc.SaveAs("text_document_modified.docx")Imports IronWord
' Load a DOCX document
Dim doc As New WordDocument("text_document.docx")
' Remove the second paragraph
doc.Paragraphs(1).Remove()
' Export the file
doc.SaveAs("text_document_modified.docx")移除前的文件是什麼樣子?

移除後文件結構有何改變?

移除作業會維持文件的整體格式與結構,其餘段落也會保留原有的顏色和格式屬性。使用 IronWord 的移除方法可精確移除目標元素,並維持周圍內容的完整性。
如何移除段落中具有特定格式的文字?
一個段落可包含多個文字區段。同一區段中的文字具有相同的格式屬性,例如粗體、斜體或顏色。了解文字區段的運作方式有助於精確處理內容。段落中的格式每變更一次,就會開始新的文字區段。例如,一個句子中若只有一個詞是粗體,通常會有三個文字區段,分別是粗體詞之前的文字、粗體詞本身,以及後面的文字。
若要移除段落中具有特定格式的內容,請存取 Texts 集合,並依索引指定要移除的文字區段。此範例會移除第一個段落中的第三個文字區段(索引為 2),其餘內容維持原樣。這樣就能保留段落結構,只移除指定的格式區段。
using IronWord;
// Load a DOCX document
WordDocument doc = new WordDocument("sample.docx");
// Remove the first paragraph's third textrun
doc.Paragraphs[0].Texts[2].Remove();
// Export the file
doc.SaveAs("sample_after_textrun_removal.docx");using IronWord;
// Load a DOCX document
WordDocument doc = new WordDocument("sample.docx");
// Remove the first paragraph's third textrun
doc.Paragraphs[0].Texts[2].Remove();
// Export the file
doc.SaveAs("sample_after_textrun_removal.docx");Imports IronWord
' Load a DOCX document
Dim doc As New WordDocument("sample.docx")
' Remove the first paragraph's third textrun
doc.Paragraphs(0).Texts(2).Remove()
' Export the file
doc.SaveAs("sample_after_textrun_removal.docx")Imports IronWord
' Load a DOCX document
Dim doc As New WordDocument("sample.docx")
' Remove the first paragraph's third textrun
doc.Paragraphs(0).Texts(2).Remove()
' Export the file
doc.SaveAs("sample_after_textrun_removal.docx")每個文字區段都有獨立的格式屬性,移除其中一個區段不會影響相鄰區段的格式。您可以精確移除具有特定格式的內容,同時維持其餘文字的外觀。
移除前的文字區段是什麼樣子?

移除文字區段會如何影響段落格式?

如圖所示,移除指定的文字區段後,其餘內容的格式仍完整保留。文字底線與其他格式屬性維持原樣,顯示可精確地操作個別文字區段。
如何依內容搜尋並移除文字?
依據文字內容比對時,FindText 方法會動態定位並移除內容,無須指定位置。這適合移除位置不明的無效文字、預留文字或特定內容。此方法會傳回相符的文字元素,若找不到則傳回 null,因此可以在呼叫 Remove 前先檢查是否為 null。
這種方式適合移除範本中的預留文字,或清理文件中符合已知錯誤模式的內容。FindText 方法預設會區分大小寫,以精確比對目標內容。
using IronWord;
// Load the document
WordDocument doc = new WordDocument("sample.docx");
// Find and remove erroneous text
var textToRemove = doc.FindText("ERRONEOUS TEXT IS HERE.");
textToRemove?.Remove();
// Save the cleaned document
doc.SaveAs("sample_cleaned.docx");using IronWord;
// Load the document
WordDocument doc = new WordDocument("sample.docx");
// Find and remove erroneous text
var textToRemove = doc.FindText("ERRONEOUS TEXT IS HERE.");
textToRemove?.Remove();
// Save the cleaned document
doc.SaveAs("sample_cleaned.docx");Imports IronWord
' Load the document
Dim doc As New WordDocument("sample.docx")
' Find and remove erroneous text
Dim textToRemove = doc.FindText("ERRONEOUS TEXT IS HERE.")
textToRemove?.Remove()
' Save the cleaned document
doc.SaveAs("sample_cleaned.docx")Imports IronWord
' Load the document
Dim doc As New WordDocument("sample.docx")
' Find and remove erroneous text
Dim textToRemove = doc.FindText("ERRONEOUS TEXT IS HERE.")
textToRemove?.Remove()
' Save the cleaned document
doc.SaveAs("sample_cleaned.docx")上方程式碼使用 Null 條件運算子(?.),在找不到文字且 FindText 傳回 null 時,可避免發生 NullReferenceException。若目標文字不一定存在,這種寫法能讓程式碼更可靠。
FindText 如何處理多次出現的情況?

FindText 如何處理多次出現的情況?

使用 FindText 時,請留意它只會傳回第一個相符的文字區段。若要移除整份文件中所有相符的文字,請用迴圈反覆搜尋並移除,直到找不到相符內容為止。
搜尋並移除文字時,仍可保留文件其餘部分的格式和結構。段落分隔、周圍文字的格式與文件樣式都會保留,因此移除不需要的內容時,仍可維持文件的一致性。
常見問題
如何使用IronWord在C#中從Word DOCX檔案中刪除文本?
IronWord提供多種方法來在C#中從Word DOCX檔案中刪除文本,例如刪除整個段落、特定文本運行,並在刪除前根據內容找到文本,同時保持文件的結構和格式。
刪除Word文件中的整個段落的過程是什麼?
使用IronWord,您可以透過Paragraphs集合的索引來存取並刪除整個段落。在目標段落上使用Remove()方法,文件結構會自動調整以填補空隙。
IronWord能否刪除段落中的特定格式化文本?
是的,IronWord允許您透過定位文本運行來刪除段落中的特定格式化文本。存取Texts集合並在目標文本運行上呼叫Remove()。
IronWord中的`FindText`方法如何運作?
IronWord中的FindText方法允許您根據匹配的內容而非位置來定位並刪除文本。它執行區分大小寫的搜索並返回匹配的文本元素以進行刪除。
在刪除文本時IronWord是否保留文件格式?
是的,IronWord在刪除文本時保留文件的格式和結構的完整性,確保周圍的內容保持不變。
如何使用IronWord實現對多個出現的文本刪除?
對於多個出現次數,使用帶有FindText方法的循環來逐步搜索並刪除所需文本的每次出現,直到沒有剩餘,從而確保全面的文本刪除。
是否可以根據內容而不知其在文件中的位置來刪除文本?
是的,使用IronWord的FindText方法,您可以根據內容動態地刪除文本,而不需要知道其在文件中的確切位置。
當使用IronWord刪除段落時,文件的結構會發生什麼變化?
當使用IronWord刪除段落時,文件結構會進行調整,以便後續的段落上移填補空隙,從而維持文件佈局的完整性。
IronWord能否選擇性地刪除段落中的格式化片段?
是的,IronWord提供精細的控制,允許您定位並刪除段落中特定的格式化文本運行,而不破壞整體段落格式。
為什麼在DOCX文件中使用IronWord進行文本刪除具有優勢?
IronWord在DOCX文件中的文本刪除具有優勢,因為它提供對文本內容的精確控制,保持文件格式,並允許動態內容操作,確保專業性和一致性。
Ahmad 是一位全端開發人員,精通 C#、Python 和 Web 技術。他對建立可擴展的軟體解決方案有著濃厚的興趣,並樂於探索如何在實際應用中實現設計與功能的完美結合。