文件元素教程
IronWord是一款強大的Word文件庫,旨在幫助.NET C#開發者將創建、閱讀和編輯Word和DOCX文件的功能整合到他們的應用程序中。 在 Word 文件的上下文中,文件元素是構成內容的基本組成部分。
目錄
- 新增文字
- 文字內容 (添加、追加和拆分)
- 設置樣式 (字體系列和大小、顏色、粗體和斜體、刪除線、底線、上標和下標)
- 嵌入圖像
- 添加圖片
- 載入圖片 (檔案與檔案流)
- 設置自動換行
- 設置尺寸 (寬度和高度)
- 設置位置偏移
- 設置與角落的距離
開始使用IronWord
立即在您的專案中使用IronWord,並享受免費試用。
添加文本運行
文字內容
Split
方法用於根據指定的分隔符將文本運行分割成一系列較小的 TextRuns。 這允許對文件中的文字信息進行組織和操作。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-text-content.cs
using IronWord;
using IronWord.Models;
WordDocument doc = new WordDocument();
// Add text
Text addText = new Text("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));
// Append text
Text appendText = new Text("The first text.");
appendText.Append(new Text("The second text."));
doc.AddParagraph(new Paragraph(appendText));
// Split text
Text splitText = new Text("Use split to split the sentence.");
splitText.Split(" ");
doc.AddParagraph(new Paragraph(splitText));
// Export docx
doc.SaveAs("textrun.docx");
設置樣式
設置TextRuns的樣式可以讓您定義文本的視覺呈現。 這包括指定字體大小、顏色、樣式、刪除線、下劃線、上標和下標等屬性。 配置樣式可以增強文件中文本的整體外觀。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-set-styling.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument("document.docx");
// Configure text
Text textRun = new Text();
textRun.Text = "Add text using IronWord";
textRun.Style = new TextStyle()
{
TextFont = new Font()
{
FontFamily = "Caveat",
FontSize = 72,
},
Color = Color.Red,
IsBold = true,
IsItalic = true,
Underline = new Underline(),
Strike = StrikeValue.Strike,
};
Paragraph paragraph = new Paragraph();
// Add text
paragraph.AddText(textRun);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("save_document.docx");
嵌入圖像
此功能讓您可以無縫地在內容中包含圖像,增強文件的整體視覺吸引力和溝通力。
:path=/static-assets/word/content-code-examples/tutorials/add-textrun-embed-images.cs
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("image.jpg");
image.Width = 200; // In unit pixel
image.Height = 200; // In unit pixel
Text textRun = new Text();
// Add image
Paragraph para = new Paragraph(textRun);
para.AddImage(image);
// Add paragraph
doc.AddParagraph(new Paragraph(textRun));
// Export docx
doc.SaveAs("save_document.docx");
添加圖片
載入圖片
載入圖像是一個關鍵的過程。 這涉及將外部影像檔案引入文件中。 能夠加載圖像有助於包含相關視覺內容,使文件更具吸引力和信息豐富。
:path=/static-assets/word/content-code-examples/tutorials/add-image-load-image.cs
using IronWord;
using IronWord.Models;
// Load docx
WordDocument doc = new WordDocument();
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage("image.jpg");
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");
配置圖像
使用可配置的設置優化圖像。 這包括設置屬性,如文字換行、尺寸、位置和距離角落的距離。 適當的配置可確保圖片以視覺上令人滿意及上下文適宜的方式顯示。
:path=/static-assets/word/content-code-examples/tutorials/add-image-configure-image.cs
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;
// Load docx
WordDocument doc = new WordDocument();
// Configure image
IronWord.Models.Image image = new IronWord.Models.Image("image.jpg");
image.WrapText = WrapText.Square;
image.Width = 100;
image.Height = 100;
image.DistanceFromTop = 50;
var position = new ElementPosition();
position.X = 50;
position.Y = 50;
image.Position = position;
Paragraph paragraph = new Paragraph();
// Add image
paragraph.AddImage(image);
// Add paragraph
doc.AddParagraph(paragraph);
// Export docx
doc.SaveAs("document.docx");