IRONSOFTWAREHOME
編輯文字
using IronWord;

// Open existing Word
WordDocument doc = new WordDocument("sample.docx");

// Edit existing text
doc.Paragraphs[0].ReplaceText("old text.", "This is the edited text.");

      // Edit text by retrieving the old text
      doc.Paragraphs[1].ReplaceText(doc.Paragraphs[1].Texts[1].Text, "Updated content for the second paragraph.");

      // Export docx
      doc.SaveAs("document.docx");
Imports IronWord

' Open existing Word
Dim doc As New WordDocument("sample.docx")

' Edit existing text
doc.Paragraphs(0).ReplaceText("old text.", "This is the edited text.")

' Edit text by retrieving the old text
doc.Paragraphs(1).ReplaceText(doc.Paragraphs(1).Texts(1).Text, "Updated content for the second paragraph.")

' Export docx
doc.SaveAs("document.docx")
NuGet Download
PM > Install-Package IronWord
編輯文字

編輯文字

IronWord中的"編輯文字"功能允許開發者修改現有DOCX文件的內容。 開發者可以透過存取ReplaceText方法替換。 這提供了靈活性,適用於需要動態更新內容的情況,例如更新報告的特定部分,替換佔位符,或根據使用者輸入或資料修改文字。

在細粒度水平上編輯文字的能力確保可以在不影響文件其餘部分結構或格式的情況下進行精確更改。 此功能在自動文件生成工作流程中特別有用,當內容頻繁需要更新或修訂時。

編輯現有DOCX中的文字的5個步驟

  • using IronWord;
  • WordDocument doc = new WordDocument("sample.docx");
  • doc.Paragraphs[0].ReplaceText("舊文字。", "這是編輯過的文字。");
  • doc.Paragraphs[1].Texts[1].Text, "更新為第二段的內容。");
  • doc.SaveAs("document.docx");

替換文字

此程式碼展示了使用IronWord在現有Word文件中編輯特定文字的方法。 一個WordDocument物件被初始化以載入"sample.docx"。 然後程式碼從Paragraphs集合並修改文件的第一和第二段。 第一次修改通過調用Text物件的文字。 方法ReplaceText接受兩個參數,第一個是要查找的字串值,第二個是用來替換舊值的新文字。 在此範例中,我們在第一個參數中提供了一個靜態字串,並用新字串更新了第一段。

然而,有時候您需要一個更動態的變數。 為了實現這一點,我們可以更進一步存取第二段中的現有文字。 我們首先存取Paragraphs集合,這與第一次實例相似。 但是,在舊文字欄位中,我們進一步深入存取現有文字,方法是存取Text屬性。 此方法讓您可以完全控制定位和更新現有字串,確保您在文件操作過程中的信心。

探索IronWord教程中的文件元素 ReplaceText ReplaceText ReplaceText

在 GitHub 上查看

準備開始了嗎?

Nuget Downloads 56,401版本:2026.9剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預約您的免費現場演示
Booking Badge

全球數百萬工程師信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
完成以下表單或發郵件至sales@ironsoftware.com
您的詳細資訊將始終保持機密。
全球數百萬工程師信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立