使用 IRONWORD 如何使用 C# 在 Word 檔案中加入水印 Jordi Bardia 更新:7月 28, 2025 下載 IronWord NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 在當今的現代企業中,Word 文件是資訊的代名詞,並在部門和公司之間傳遞重要訊息。 但是,使用電子文檔也存在有人篡改或偽造無效或虛假 Word 文件的風險。 因此,對抗這種現象的一種方法是為 Word 文件添加浮水印。 然而,這並不會提高 Word 文件的安全性。 水印能夠有效地區分真正的 Word 文件和偽造的文檔。 此外,以程式設計方式新增浮水印相當具有挑戰性,因為大多數情況下都需要使用者手動使用 Microsoft Word 並在每個文件上新增浮水印,這使得整個過程非常漫長而痛苦。 幸運的是, IronWord可以透過程式設計方式在 Word 中加入圖像作為浮水印。 這樣一來,開發人員就可以以程式設計方式將它們添加到流程中,從而減少重複性工作,提高效率和一致性。 雖然水印有各種類型,例如形狀浮水印和文字浮水印,但本文僅使用圖像浮水印,以 IronWord 庫為例進行討論,並提供向 Word 文件中添加圖像的實用範例。 在 Word 文件中新增浮水印 鐵言 如何在 C# 中為 Word 檔案新增浮水印:圖 1 - IronWord:C# DOCX 函式庫 IronWord 是一個可靠且易於使用的 C# Docx 庫,它允許開發人員使用 C# 建立和編輯 Word 文檔,而無需依賴 Microsoft Office 或 Word Interop 等傳統依賴項。 此外,它還擁有豐富的文檔,並完全支援 .NET 8、7、6、Framework、Core 和 Azure。 這使得它能夠跨平台兼容大多數應用程序,並且無論您正在處理什麼應用程序,它都具有靈活性。 使用影像浮水印 許可證密鑰 請注意,IronWord 需要許可證密鑰才能運作。 您可以點擊此連結以取得免費試用金鑰。 // Replace the license key variable with the trial key you obtained IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"; // Replace the license key variable with the trial key you obtained IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"; ' Replace the license key variable with the trial key you obtained IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY" $vbLabelText $csharpLabel 收到試用金鑰後,請在項目中設定此變數。 在 Word 文件中新增圖像浮水印 讓我們來看一個在 Word 文件中新增影像浮水印的範例。 為了減少重複程式碼,我們將不提及 static void main,而只展示主程式碼。 我們將使用此圖像作為浮水印圖像並將其添加到 Word 文件中。 如何在 C# 中為 Word 檔案新增浮水印:圖 2 - 輸入影像 using IronWord; using IronWord.Models; using IronWord.Models.Enums; // Set the license key IronWord.License.LicenseKey = "YOUR-KEY"; // Create a new Word document WordDocument doc = new WordDocument(); // Load the image to be used as a watermark IronWord.Models.Image image = new IronWord.Models.Image("ironword.png"); // Set the width and height of the image image.Width = 500; // In pixels image.Height = 250; // In pixels // Add the image as a watermark to the document doc.AddImage(image); // Save the document as a .docx file doc.SaveAs("documentImageResized.docx"); using IronWord; using IronWord.Models; using IronWord.Models.Enums; // Set the license key IronWord.License.LicenseKey = "YOUR-KEY"; // Create a new Word document WordDocument doc = new WordDocument(); // Load the image to be used as a watermark IronWord.Models.Image image = new IronWord.Models.Image("ironword.png"); // Set the width and height of the image image.Width = 500; // In pixels image.Height = 250; // In pixels // Add the image as a watermark to the document doc.AddImage(image); // Save the document as a .docx file doc.SaveAs("documentImageResized.docx"); Imports IronWord Imports IronWord.Models Imports IronWord.Models.Enums ' Set the license key IronWord.License.LicenseKey = "YOUR-KEY" ' Create a new Word document Dim doc As New WordDocument() ' Load the image to be used as a watermark Dim image As New IronWord.Models.Image("ironword.png") ' Set the width and height of the image image.Width = 500 ' In pixels image.Height = 250 ' In pixels ' Add the image as a watermark to the document doc.AddImage(image) ' Save the document as a .docx file doc.SaveAs("documentImageResized.docx") $vbLabelText $csharpLabel 我們先建立一個新的WordDocument實例。 這是 IronWord 中的文件類別。 接下來,我們將建立一個名為doc新變量,並將圖片浮水印新增到該變數中。 我們將上面輸入的圖像載入到一個新的Image類別中。 我們會相應地設定圖像的寬度和高度。 此寬度和高度屬性允許使用者實現圖像浮水印縮放。 在我們的範例中,寬度設定為 500,高度設定為 250。 我們使用AddImage將影像浮水印加入文件。 最後,我們將文檔documentImageResized儲存並匯出為 Word 文件。 Word文檔的輸出結果如下。 如何在 C# 中為 Word 文件新增浮水印:圖 3 - 輸出 Word 文檔 請注意,範例中設定的尺寸是為了適應整個 Word 文件並展示 IronWord 的功能。 除了添加圖像之外,我們還可以使用WrapText屬性來確保圖像浮水印保持在背景文字的後面。 // Set the image to wrap behind the text image.WrapText = WrapText.BehindText; // Set the image to wrap behind the text image.WrapText = WrapText.BehindText; ' Set the image to wrap behind the text image.WrapText = WrapText.BehindText $vbLabelText $csharpLabel 透過此屬性,圖像浮水印應環繞在文字後面,僅在背景中顯示,從而使文字不受阻礙。 此外,開發者還可以嘗試對影像浮水印進行更多自訂,使其獨一無二。 IronWord 可讓您調整影像相對於 Word 文件的尺寸,並管理影像浮水印的位置。 using IronWord; using IronWord.Models; // Set the license key IronWord.License.LicenseKey = "YOUR-KEY"; // Create a new Word document WordDocument doc = new WordDocument(); // Load the image to be used as a watermark IronWord.Models.Image image = new IronWord.Models.Image("ironword.png"); // Create an ElementPosition object to set the position ElementPosition elementPosition = new ElementPosition(); elementPosition.SetXPosition(50); elementPosition.SetYPosition(50); // Set the dimensions of the image image.Width = 50; // In pixels image.Height = 50; // In pixels // Set the image position using the ElementPosition object image.Position = elementPosition; // Offset the image from each side by 100 pixels image.DistanceFromTop = 100; image.DistanceFromBottom = 100; image.DistanceFromLeft = 100; image.DistanceFromRight = 100; using IronWord; using IronWord.Models; // Set the license key IronWord.License.LicenseKey = "YOUR-KEY"; // Create a new Word document WordDocument doc = new WordDocument(); // Load the image to be used as a watermark IronWord.Models.Image image = new IronWord.Models.Image("ironword.png"); // Create an ElementPosition object to set the position ElementPosition elementPosition = new ElementPosition(); elementPosition.SetXPosition(50); elementPosition.SetYPosition(50); // Set the dimensions of the image image.Width = 50; // In pixels image.Height = 50; // In pixels // Set the image position using the ElementPosition object image.Position = elementPosition; // Offset the image from each side by 100 pixels image.DistanceFromTop = 100; image.DistanceFromBottom = 100; image.DistanceFromLeft = 100; image.DistanceFromRight = 100; Imports IronWord Imports IronWord.Models ' Set the license key IronWord.License.LicenseKey = "YOUR-KEY" ' Create a new Word document Dim doc As New WordDocument() ' Load the image to be used as a watermark Dim image As New IronWord.Models.Image("ironword.png") ' Create an ElementPosition object to set the position Dim elementPosition As New ElementPosition() elementPosition.SetXPosition(50) elementPosition.SetYPosition(50) ' Set the dimensions of the image image.Width = 50 ' In pixels image.Height = 50 ' In pixels ' Set the image position using the ElementPosition object image.Position = elementPosition ' Offset the image from each side by 100 pixels image.DistanceFromTop = 100 image.DistanceFromBottom = 100 image.DistanceFromLeft = 100 image.DistanceFromRight = 100 $vbLabelText $csharpLabel 與上面的程式碼類似,我們將圖像的 x 和 y 座標尺寸設為 50。我們還將影像的每一邊偏移 100px,以在其周圍建立邊距。 結論 如何在 C# 中向 Word 文件添加浮水印:圖 4 - IronWord 許可信息 在這些範例中,我們示範了使用IronWord庫以程式設計方式在 C# 中操作和讀取 Word 文件是多麼簡單。 該程式庫的靈活性和可擴展性使其成為一個有價值的工具,允許開發人員在實際的、真實的範例中使用 IronWord,例如添加浮水印和文字浮水印。 了解 Word 如何與其他應用程式協同工作至關重要,因為它能為開發人員提供更多應對挑戰的解決方案。 IronWord 提供免費試用授權。 常見問題解答 如何使用 C# 程式化地在 Word 文件中加入水印? 使用 IronWord,開發人員可以在 Word 文件中加入圖片水印,方法是建立 Word 文件實例、載入圖片,並使用 AddImage 方法將其插入為水印。 在 Word 文件中使用水印有什麼好處? 水印有助於區分真偽文件,並透過將檔案標示為草稿、機密或定稿來強化文件管理。 IronWord 如何處理 Word 文件中的影像水印? IronWord 可讓開發人員載入圖片、設定圖片的尺寸和位置,並將圖片加入 Word 文件作為水印。圖片可以使用 WrapText.BehindText 屬性定位在文字之後。 IronWord 兼容哪些版本的 .NET? IronWord 支援 .NET 8、7、6、.NET Framework、.NET Core 和 Azure,因此具有高度的通用性和跨平台相容性。 使用 IronWord 是否需要授權? 是的,IronWord 需要許可金鑰才能使用完整功能。您可以從 IronWord 網站取得試用金鑰,進行初步評估。 如何使用 IronWord 自訂 Word 文件中影像水印的位置? IronWord 允許自訂影像水印的位置,方法是設定 x 與 y 座標,並調整維度,讓影像從兩側各偏移特定的像素值。 IronWord 可以用來在 Word 文件中加入文字水印嗎? 雖然這篇文章著重於影像水印,但 IronWord 也可用來新增文字水印,方法是將文字渲染為影像,並與影像水印相似地套用。 文章提供了哪些使用 IronWord 的實例? 文章提供了建立 Word 文件、載入圖片作為水印、調整圖片尺寸、設定圖片在文字後面的位置等範例,以展示 IronWord 的功能。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新9月 18, 2025 ASP .NET Core 匯入和匯出 Word 檔案 本指南探討如何匯入現有的 Word 文件、顯示其內容,以及使用 IronWord 函式庫從頭建立文件 閱讀更多 更新7月 28, 2025 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多 更新6月 22, 2025 如何使用 C# 在 Word 中對齊文字 讓我們深入瞭解 IronWord NuGet 套件,以及如何使用此套件對齊文字或段落 閱讀更多 如何在 C# 中使用 Word 模板生成 Word 文檔如何在 C# 中從 Word 擷取文字
更新7月 28, 2025 VS 2022 程式化建立新 Word 文件 (教學) 在今天的教程中,我將簡單介紹如何使用 IronWord 程式化地建立 Microsoft Word 文件,並提供簡單的範例。 閱讀更多