Document Element Tutorial

IronWord is a powerful Word document library designed to assist .NET C# developers in integrating the capabilities of creating, reading, and editing Word and DOCX documents into their applications. In the context of a Word document, document elements are the building blocks that make up the content.

Table of Contents

C# NuGet Library for

Install with NuGet

Install-Package IronWord

Add TextRuns

Text Content

The Split method is utilized to divide the text run into a list of smaller TextRuns, based on a specified delimiter. This allows for the organization and manipulation of textual information within the document.

:path=/static-assets/word/content-code-examples/tutorials/add-textrun-text-content.cs
using IronWord;
using IronWord.Models;

WordDocument doc = new WordDocument();

// Add text
TextRun addText = new TextRun("Add text using IronWord");
doc.AddParagraph(new Paragraph(addText));

// Append text
TextRun appendText = new TextRun("The first text.");
appendText.Append(new TextRun("The second text."));
doc.AddParagraph(new Paragraph(appendText));

// Split text
TextRun splitText = new TextRun("Use split to split the sentence.");
splitText.Split(" ");
doc.AddParagraph(new Paragraph(splitText));

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

Private doc As New WordDocument()

' Add text
Private addText As New TextRun("Add text using IronWord")
doc.AddParagraph(New Paragraph(addText))

' Append text
Dim appendText As New TextRun("The first text.")
appendText.Append(New TextRun("The second text."))
doc.AddParagraph(New Paragraph(appendText))

' Split text
Dim splitText As New TextRun("Use split to split the sentence.")
splitText.Split(" ")
doc.AddParagraph(New Paragraph(splitText))

' Export docx
doc.SaveAs("textrun.docx")
VB   C#

Set Styling

Setting styling for TextRuns allows you to define the visual presentation of text. This includes specifying attributes such as font size, color, style, strikethrough, underline, superscript, and subscript. Configuring styling enhances the overall appearance of the text in the document.

:path=/static-assets/word/content-code-examples/tutorials/add-textrun-set-styling.cs
using IronSoftware.Drawing;
using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument("document.docx");

// Configure text
TextRun textRun = new TextRun();
textRun.Text = "Add text using IronWord";
textRun.Style = new TextStyle()
{
    FontFamily = "Caveat",
    FontSize = 72,
    TextColor = new IronColor(Color.Red),
    IsBold = true,
    IsItalic = true,
    IsUnderline = true,
    IsSuperscript = false,
    IsStrikethrough = true,
    IsSubscript = false
};

Paragraph paragraph = new Paragraph();

// Add text
paragraph.AddTextRun(textRun);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");
Imports IronSoftware.Drawing
Imports IronWord
Imports IronWord.Models

' Load docx
Private doc As New WordDocument("document.docx")

' Configure text
Private textRun As New TextRun()
textRun.Text = "Add text using IronWord"
textRun.Style = New TextStyle() With {
	.FontFamily = "Caveat",
	.FontSize = 72,
	.TextColor = New IronColor(Color.Red),
	.IsBold = True,
	.IsItalic = True,
	.IsUnderline = True,
	.IsSuperscript = False,
	.IsStrikethrough = True,
	.IsSubscript = False
}

Dim paragraph As New Paragraph()

' Add text
paragraph.AddTextRun(textRun)

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("save_document.docx")
VB   C#

Embed Images

This feature allows you to seamlessly include images within the content, enhancing the overall visual appeal and communicative power of the document.

: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
TextRun textRun = new TextRun();

// Add image
textRun.AddImage(image);

// Add paragraph
doc.AddParagraph(new Paragraph(textRun));

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

' Load docx
Private doc As New WordDocument()

' Configure image
Private image As New IronWord.Models.Image("image.jpg")
image.Width = 200 ' In unit pixel
image.Height = 200 ' In unit pixel
Dim textRun As New TextRun()

' Add image
textRun.AddImage(image)

' Add paragraph
doc.AddParagraph(New Paragraph(textRun))

' Export docx
doc.SaveAs("save_document.docx")
VB   C#

Add Images

Load Image

Loading images is a crucial process. This involves bringing external image files into the document. The ability to load images facilitates the inclusion of relevant visuals, contributing to a more engaging and informative document.

: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");
Imports IronWord
Imports IronWord.Models

' Load docx
Private doc As New WordDocument()

Private paragraph As New Paragraph()

' Add image
paragraph.AddImage("image.jpg")

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("document.docx")
VB   C#

Configure Image

Optimize the images with configurable settings. This includes setting properties such as text wrapping, dimensions, position, and distance from corners. Proper configuration ensures that images are displayed in a visually pleasing and contextually appropriate manner.

: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");
Imports IronWord
Imports IronWord.Models
Imports IronWord.Models.Enums

' Load docx
Private doc As New WordDocument()

' Configure image
Private image As New IronWord.Models.Image("image.jpg")
image.WrapText = WrapText.Square
image.Width = 100
image.Height = 100
image.DistanceFromTop = 50

Dim position = New ElementPosition()
position.X = 50
position.Y = 50
image.Position = position

Dim paragraph As New Paragraph()

' Add image
paragraph.AddImage(image)

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("document.docx")
VB   C#