IRONSOFTWAREHOME
USING IRONWORD

如何在 C# 中將 Word 轉換為 PDF

Curtis Chau
Curtis Chau
Updated: 2026年6月28日

建立、編輯和管理Word文件是許多應用程式的常見需求。 雖然用C#建立和編輯Word文件的方法有很多種,但使用Microsoft Interop服務是其中最強大的方法之一。 使用此工具,您可以輕鬆地程式化處理Word文件。

先決條件

在設置環境並開始程式碼之前,確保您滿足以下先決條件:

  1. **Visual Studio:**確保在您的機器上已安裝Visual Studio。如果沒有,請從Microsoft官方網站下載並安裝它。
  2. **Microsoft Word:**由於我們正在使用Microsoft Interop,您應該在您的電腦上安裝MS Word。 Interop服務與安裝在您機器上的Microsoft Word應用程式介面。
  3. **基本C#知識:**理解基本C#概念是必須的。
  4. **.NET Framework:**確保您的Visual Studio支持.NET Framework,因為我們的應用程式將以此為基礎。

設置環境

首先打開Visual Studio應用程式。 打開後,您將看到歡迎螢幕。

1. 建立新的.NET Framework控制臺應用程式

  1. 點擊"建立新專案"。
  2. 在搜索框中輸入"控制臺應用程式 (.NET Framework)"。
  3. 在結果中,選擇"控制臺應用程式 (.NET Framework)"並點擊"下一步"按鈕。
  4. 為您的專案設置一個名稱,然後點擊"建立"按鈕。

完成這些步驟後,Visual Studio將為您生成一個新的.NET Framework控制臺應用程式。 在Program.cs文件中,您將找到一個帶有Main的方法,這是控制臺應用程式的入口點。

2. 使用NuGet包管理器安裝Microsoft.Office.Interop.Word

NuGet是.NET的包管理器,並整合到Visual Studio中。 以下是如何使用它來安裝Microsoft.Office.Interop.Word包:

  1. 在Visual Studio中,轉到"工具"選單。
  2. 選擇"NuGet包管理器",然後選擇"管理解決方案的NuGet包..."。
  3. 在NuGet窗口中,點擊"瀏覽"標籤。
  4. 在搜索框中輸入Microsoft.Office.Interop.Word並按下回車。
  5. 在搜索結果中,選擇Microsoft.Office.Interop.Word包。
  6. 在右側,確保選中您的控制臺應用程式專案,然後點擊"安裝"按鈕。

C# 編輯Word(程式碼範例開發者教程)圖1

Visual Studio將現在安裝該包並在您的專案中新增對它的參考。 此包包含必需的程式集和工具,用於從您的C#應用程式中與MS Word進行互動。

介紹IronWord:Interop的卓越替代方案

雖然Interop為操作Word和Excel提供了強大的功能,但也存在局限性。 引入IronWord,這是一個為.NET開發者優化的多功能函式庫。 IronWord提供了比Interop更流暢的體驗,尤其是在編輯Word文件任務中。 它不僅確保了相容性和性能,還通過直觀的方法簡化了複雜的任務。 為了便於比較,我將為每個用例在MS Word後提供IronWord程式碼片段,使用IronWord版本2024.1.2

打開現有Word文件

通常,您可能需要編輯現有的Word文件,以下範例顯示如何在C#中執行此操作:

// Create an instance of the Word Application
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Open a Word document with the specified file path
var WordDoc = WordApp.Documents.Open(@"path_to_your_document.docx");

在上述程式碼中,用您的docx文件的路徑替換path_to_your_document.docx

使用IronWord

使用IronWord打開Word文件。

// Open a Word document with the specified file path using IronWord
WordDocument doc = new WordDocument(@"path_to_your_document.docx");

建立新的Word文件

要從零開始建立Word文件:

// Initialize a new instance of the Word Application
var WordApp = new Microsoft.Office.Interop.Word.Application();

// Add a new Word document
var WordDoc = WordApp.Documents.Add();

此程式碼片段建立一個新的Word文件,您可以使用C#編寫和編輯。

使用IronWord

// Create a new, empty Word document using IronWord
WordDocument doc = new WordDocument();

向Word文件新增文字

要新增一個新的文字段落:

// Add a new paragraph to the document
WordDoc.Paragraphs.Add();
// Assign text to the newly added paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph.";

Range.Text屬性為其分配新文字。

使用IronWord

// Add a new text to the document using IronWord
doc.AddText("Add text using IronWord");

編輯現有文字

在本教程中,讓我們更改第一段:

// Edit the text of the first paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph.";

您還可以使用類似的方法向Word文件新增和編輯其他元素。

使用IronWord

// Edit the text of the first paragraph using IronWord
doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph.";

保存和關閉文件

完成您所需的編輯後:

// Save the document to a specified path
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
// Close the document and quit the application
WordDoc.Close();
WordApp.Quit();

path_where_you_want_to_save.docx替換為您所需的路徑。

使用IronWord

// Save the document to the desired path using IronWord
doc.SaveAs(@"path_where_you_want_to_save.docx");

完整程式碼和範例

讓我們將其全部放在一起。 以下是一個完整的程式碼範例,演示如何打開現有的Word文件,編輯它,然後保存更改:

var WordApp = new Microsoft.Office.Interop.Word.Application();

// Create a new Word document
var WordDoc = WordApp.Documents.Add();

// Add new text
WordDoc.Paragraphs.Add();
WordDoc.Paragraphs[1].Range.Text = "This is the first paragraph.";

// Edit the first paragraph
WordDoc.Paragraphs[1].Range.Text = "This is the edited first paragraph.";

// Save and close
WordDoc.SaveAs(@"path_where_you_want_to_save.docx");
WordDoc.Close();
WordApp.Quit();

使用IronWord

使用IronWord的完整程式碼範例簡潔。IronWord使用簡潔的程式碼片段來編輯DOCX文件。

// Create an empty Word document
WordDocument doc = new WordDocument();

// Add new text
doc.AddText("This is the first paragraph.");

// Edit the text
doc.Paragraphs[0].TextRuns[0].Text = "This is the edited first paragraph.";

// Export DOCX
doc.SaveAs(@"path_where_you_want_to_save.docx");

結論

在.NET應用程式中操作Word和Excel文件的領域中,選擇豐富。 雖然Microsoft的Interop服務一直是許多人的首選,但IronWord這樣的解決方案的出現表明向更高效和使用者友好的工具轉移。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

相關文章

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天試用金鑰
無需信用卡或帳戶建立