開始使用IronWord
IronWord: 用於 .NET 的 Word 文件庫
IronWord 是由 Iron Software 開發的 Word 文件庫。IronWord 擅長為 .NET 應用程序提供處理 Word 文件的強大功能。
- 加載、操作和保存 Word 和 Docx 文檔。
- PageSetup: 配置紙張大小、頁面方向、邊距和背景顏色。
- TextRun: 處理文本內容、樣式、拆分、附加文本和添加圖像。
- TextStyle: 管理字體系列、大小、顏色、粗體、斜體、刪除線、下劃線、上標和下標。
- Paragraph: 添加文本運行、圖像、形狀,設置樣式、對齊方式、項目符號和編號列表。
- Table: 操作表格結構,包括添加行、獲取和設置單元格值、刪除行、合併單元格等。
- Image: 從文件或流加載圖像,設置環繞文本、位置偏移、寬度、高度和其他屬性。
- Shape: 設置環繞文本、位置偏移、寬度、高度、形狀類型和旋轉。
.NET 的 Word 文件 C# 函式庫
- 下載處理 DOCX 文件的 C# 庫
- 創建和修改 Word 和 DOCX 文件
- 新增文件結構,例如段落、章節和表格。
- 添加文檔元素,例如文字串、圖像和形狀
- 輕鬆為文件元素設計樣式
安裝
IronWord 庫
安裝 IronWord 既快速又簡單,請按以下方式安裝套件:
Install-Package IronWord
或者,直接從 官方 IronWord NuGet 網站一旦安裝完畢,您可以通過在您的 C# 程式碼頂部添加 using IronWord;
開始使用。
套用授權金鑰
接下來,通過將有效的授權或試用金鑰分配給 License
類的 LicenseKey
屬性來套用授權金鑰至 IronWord。在使用任何 IronWord 方法之前,請在導入語句後包含以下代碼:
:path=/static-assets/word/content-code-examples/get-started/get-started-license.cs
IronWord.License.LicenseKey = "IRONWORD.MYLICENSE.KEY.1EF01";
IronWord.License.LicenseKey = "IRONWORD.MYLICENSE.KEY.1EF01"
代碼範例
由 IronWord 生成的 DOCX 檔案,在某個版本的 Microsoft Word 中打開時,可能處於相容模式,導致某些樣式不可用。要將 Word 文件轉換出相容模式:
選擇「檔案」>「資訊」,然後點擊「轉換」。
- 系統會提示您,您的文件將升級到最新的檔案格式。點擊「確定」。
創建 Word 和 Docx 文件
通過使用其中一個構造函數實例化 WordDocument
類來創建 Word 文件。之後,使用 SaveAs
方法導出 Word 文件。
:path=/static-assets/word/content-code-examples/get-started/get-started-1.cs
using IronWord;
using IronWord.Models;
// Create textrun
Text textRun = new Text("Sample text");
Paragraph paragraph = new Paragraph();
paragraph.AddText(textRun);
// Create a new Word document
WordDocument doc = new WordDocument(paragraph);
// Export docx
doc.SaveAs("document.docx");
Imports IronWord
Imports IronWord.Models
' Create textrun
Private textRun As New Text("Sample text")
Private paragraph As New Paragraph()
paragraph.AddText(textRun)
' Create a new Word document
Dim doc As New WordDocument(paragraph)
' Export docx
doc.SaveAs("document.docx")
添加圖片
無法單獨添加圖片;相反,應將其添加到文檔結構之一,如 Paragraph
、TableCell
或 Section
。使用 AddImage
方法來添加圖片。
:path=/static-assets/word/content-code-examples/get-started/get-started-2.cs
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("image.jpg");
image.Width = 250; // In unit pixel
image.Height = 200; // In unit pixel
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models
' Load docx
Private doc As New WordDocument("document.docx")
' Configure image
Private image As New IronWord.Models.Image("image.jpg")
image.Width = 250 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim paragraph As New Paragraph()
' Add image
paragraph.AddImage(image)
' Add paragraph
doc.AddParagraph(paragraph)
' Export docx
doc.SaveAs("save_document.docx")
添加表格
添加表格需要更多的工作,因為需要創建表格、行、列和表格單元格。然而,通過這種設置,可以進行大量配置。每個單元格可以有不同的樣式。探索各種邊框樣式,提供多達24種類型的選擇。
:path=/static-assets/word/content-code-examples/get-started/get-started-3.cs
using IronWord;
using IronWord.Models;
// Create table cell
TableCell cell = new TableCell();
Text textRun = new Text();
textRun.Text = "Sample text";
// Add textrun to the cell
cell.AddChild(new Paragraph(textRun));
// Configure border style
BorderStyle borderStyle = new BorderStyle();
borderStyle.BorderColor = Color.Black;
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick;
borderStyle.BorderSize = 5;
// Configure table border
TableBorders tableBorders = new TableBorders() {
TopBorder = borderStyle,
RightBorder = borderStyle,
BottomBorder = borderStyle,
LeftBorder = borderStyle,
};
cell.Borders = tableBorders;
// Create row and add cell
TableRow row = new TableRow();
row.AddCell(cell);
row.AddCell(cell);
// Create table and add row
Table table = new Table();
table.AddRow(row);
// Create new Word document from the table
WordDocument doc = new WordDocument(table);
// Export Word document
doc.SaveAs("Document.docx");
Imports IronWord
Imports IronWord.Models
' Create table cell
Private cell As New TableCell()
Private textRun As New Text()
textRun.Text = "Sample text"
' Add textrun to the cell
cell.AddChild(New Paragraph(textRun))
' Configure border style
Dim borderStyle As New BorderStyle()
borderStyle.BorderColor = Color.Black
borderStyle.BorderValue = IronWord.Models.Enums.BorderValues.Thick
borderStyle.BorderSize = 5
' Configure table border
Dim tableBorders As New TableBorders() With {
.TopBorder = borderStyle,
.RightBorder = borderStyle,
.BottomBorder = borderStyle,
.LeftBorder = borderStyle
}
cell.Borders = tableBorders
' Create row and add cell
Dim row As New TableRow()
row.AddCell(cell)
row.AddCell(cell)
' Create table and add row
Dim table As New Table()
table.AddRow(row)
' Create new Word document from the table
Dim doc As New WordDocument(table)
' Export Word document
doc.SaveAs("Document.docx")
許可證和支援選項
IronWord 是一個付費的庫,但也提供免費試用許可證。 這裡有關 Iron Software 的更多資訊,請訪問我們的網站:https://ironsoftware.com/ 如需更多支持和查詢,請 請詢問我們的團隊.
Iron Software 支援
如有一般支援及技術問題,請發送電子郵件至:support@ironsoftware.com