Table of Contents

Get started with IronWord

Start using IronWord in your project today with a free trial.

First Step:
green arrow pointer


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;

// Create a new Word document
WordDocument doc = new WordDocument();

// Add text to the document
TextContent addText = new TextContent("Add text using IronWord");
// Adds a paragraph with specified text to the document
doc.AddParagraph(new Paragraph(addText));

// Prepare text to be appended
TextContent appendText = new TextContent("The first text.");
// Append additional text to the existing TextContent
appendText.Append(new TextContent(" The second text."));
// Adds the appended text as a new paragraph
doc.AddParagraph(new Paragraph(appendText));

// Prepare text content to be split
TextContent splitText = new TextContent("Use split to split the sentence.");
// Split the text content into an array of words by spaces
TextContent[] splitArray = splitText.Split(" ");

// Create a new paragraph for each piece of split content and add it to the document
foreach (var textPart in splitArray)
{
    doc.AddParagraph(new Paragraph(textPart));
}

// Export the document to a .docx file
doc.SaveAs("textrun.docx"); // Saves the document as "textrun.docx"
Imports IronWord

Imports IronWord.Models



' Create a new Word document

Private doc As New WordDocument()



' Add text to the document

Private addText As New TextContent("Add text using IronWord")

' Adds a paragraph with specified text to the document

doc.AddParagraph(New Paragraph(addText))



' Prepare text to be appended

Dim appendText As New TextContent("The first text.")

' Append additional text to the existing TextContent

appendText.Append(New TextContent(" The second text."))

' Adds the appended text as a new paragraph

doc.AddParagraph(New Paragraph(appendText))



' Prepare text content to be split

Dim splitText As New TextContent("Use split to split the sentence.")

' Split the text content into an array of words by spaces

Dim splitArray() As TextContent = splitText.Split(" ")



' Create a new paragraph for each piece of split content and add it to the document

For Each textPart In splitArray

	doc.AddParagraph(New Paragraph(textPart))

Next textPart



' Export the document to a .docx file

doc.SaveAs("textrun.docx") ' Saves the document as "textrun.docx"
$vbLabelText   $csharpLabel

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
// Import necessary namespaces from the IronWord library.
using IronWord;
using IronWord.Models;
using IronWord.Models.Enums;

// Load a DOCX file into a WordDocument object.
WordDocument doc = new WordDocument("document.docx");

// Create a TextContent object to configure and hold the text properties.
TextContent textRun = new TextContent
{
    // Assign the text to be added.
    Text = "Add text using IronWord",

    // Define the text style.
    Style = new TextStyle
    {
        // Configure font details.
        TextFont = new Font
        {
            FontFamily = "Caveat", // Set the font family.
            FontSize = 72         // Set the font size.
        },
        Color = Color.Red,    // Set the text color.
        IsBold = true,        // Set the text to be bold.
        IsItalic = true,      // Set the text to be italic.
        Underline = new Underline(),        // Set the text to be underlined.
        Strike = StrikeValue.Strike         // Apply a strikethrough style.
    }
};

// Create a new Paragraph object.
Paragraph paragraph = new Paragraph();

// Add the configured text to the paragraph.
paragraph.AddText(textRun);

// Add the paragraph containing the text to the WordDocument.
doc.AddParagraph(paragraph);

// Save the WordDocument to a new file.
doc.SaveAs("save_document.docx");
' Import necessary namespaces from the IronWord library.

Imports IronWord

Imports IronWord.Models

Imports IronWord.Models.Enums



' Load a DOCX file into a WordDocument object.

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



' Create a TextContent object to configure and hold the text properties.

Private textRun As New TextContent With {

	.Text = "Add text using IronWord",

	.Style = New TextStyle With {

		.TextFont = New Font With {

			.FontFamily = "Caveat",

			.FontSize = 72

		},

		.Color = Color.Red,

		.IsBold = True,

		.IsItalic = True,

		.Underline = New Underline(),

		.Strike = StrikeValue.Strike

	}

}



' Create a new Paragraph object.

Private paragraph As New Paragraph()



' Add the configured text to the paragraph.

paragraph.AddText(textRun)



' Add the paragraph containing the text to the WordDocument.

doc.AddParagraph(paragraph)



' Save the WordDocument to a new file.

doc.SaveAs("save_document.docx")
$vbLabelText   $csharpLabel

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;

// Create a new Word document
WordDocument doc = new WordDocument();

// Initialize image configuration
// Ensure the image file exists at the specified path
ImageContent image = new ImageContent("image.jpg");
image.Width = 200; // Set image width in pixels
image.Height = 200; // Set image height in pixels

// Initialize text content for the paragraph
// This appears to initialize an empty text run. If text is needed, it can be set here.
TextContent textRun = new TextContent();

// Create a paragraph and add the image to it
// Ensure the text run is also included when creating the paragraph
Paragraph imageParagraph = new Paragraph(textRun); 
imageParagraph.AddImage(image);

// Add the paragraph with the image to the document
doc.AddParagraph(imageParagraph);

// Save the document to a file
// Specify the path where the document should be saved
doc.SaveAs("save_document.docx");
Imports IronWord

Imports IronWord.Models



' Create a new Word document

Private doc As New WordDocument()



' Initialize image configuration

' Ensure the image file exists at the specified path

Private image As New ImageContent("image.jpg")

image.Width = 200 ' Set image width in pixels

image.Height = 200 ' Set image height in pixels



' Initialize text content for the paragraph

' This appears to initialize an empty text run. If text is needed, it can be set here.

Dim textRun As New TextContent()



' Create a paragraph and add the image to it

' Ensure the text run is also included when creating the paragraph

Dim imageParagraph As New Paragraph(textRun)

imageParagraph.AddImage(image)



' Add the paragraph with the image to the document

doc.AddParagraph(imageParagraph)



' Save the document to a file

' Specify the path where the document should be saved

doc.SaveAs("save_document.docx")
$vbLabelText   $csharpLabel

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
// Import necessary namespaces for handling Word documents
using IronWord; 
using IronWord.Models;

// Initialize a Word document using the IronWord library
WordDocument doc = new WordDocument();

// Create a new paragraph object that will hold various elements, including images
Paragraph paragraph = new Paragraph();

// Add an image to the paragraph
// Ensure that the image path is correct and that the image exists at the specified path
// The path "image.jpg" is relative to the current working directory of the application
paragraph.AddImage("image.jpg");

// Add the paragraph containing the image to the document
// This integrates the created paragraph with its contents into the Word document structure
doc.AddParagraph(paragraph);

// Save the document as 'document.docx'
// This method saves the current state of the document in the Word format under the specified filename
doc.SaveAs("document.docx");
' Import necessary namespaces for handling Word documents

Imports IronWord

Imports IronWord.Models



' Initialize a Word document using the IronWord library

Private doc As New WordDocument()



' Create a new paragraph object that will hold various elements, including images

Private paragraph As New Paragraph()



' Add an image to the paragraph

' Ensure that the image path is correct and that the image exists at the specified path

' The path "image.jpg" is relative to the current working directory of the application

paragraph.AddImage("image.jpg")



' Add the paragraph containing the image to the document

' This integrates the created paragraph with its contents into the Word document structure

doc.AddParagraph(paragraph)



' Save the document as 'document.docx'

' This method saves the current state of the document in the Word format under the specified filename

doc.SaveAs("document.docx")
$vbLabelText   $csharpLabel

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;

// Create a new Word document.
WordDocument doc = new WordDocument();

// Create and configure an image to be inserted into the document.
ImageContent image = new ImageContent("image.jpg")
{
    // Set text wrapping style to 'Square'.
    WrapText = WrapText.Square,
    // Set image width and height.
    Width = 100,
    Height = 100,
    // Set the distance from the top of the page.
    DistanceFromTop = 50,
    // Set the position of the image within the document.
    Position = new ElementPosition
    {
        X = 50, // Set X position of the image.
        Y = 50  // Set Y position of the image.
    }
};

// Create a new paragraph and add the image to it.
Paragraph paragraph = new Paragraph();
paragraph.AddImage(image);

// Add the paragraph containing the image to the Word document.
doc.AddParagraph(paragraph);

// Save the Word document to file.
doc.SaveAs("document.docx");
Imports IronWord

Imports IronWord.Models

Imports IronWord.Models.Enums



' Create a new Word document.

Private doc As New WordDocument()



' Create and configure an image to be inserted into the document.

Private image As New ImageContent("image.jpg") With {

	.WrapText = WrapText.Square,

	.Width = 100,

	.Height = 100,

	.DistanceFromTop = 50,

	.Position = New ElementPosition With {

		.X = 50,

		.Y = 50

	}

}



' Create a new paragraph and add the image to it.

Private paragraph As New Paragraph()

paragraph.AddImage(image)



' Add the paragraph containing the image to the Word document.

doc.AddParagraph(paragraph)



' Save the Word document to file.

doc.SaveAs("document.docx")
$vbLabelText   $csharpLabel

Frequently Asked Questions

What is IronWord?

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.

How can I add text to a Word document?

You can add a text run to a Word document by creating an instance of TextRun with your desired text and adding it to a document's paragraph using IronWord.

How do I split text in a document?

The 'Split' method is used to divide a text run into smaller TextRuns based on a specified delimiter, allowing for organized manipulation of text using IronWord.

Can I style text in a Word document?

Yes, you can style text by setting attributes such as font size, color, style (bold, italic), and effects like strikethrough, underline, superscript, and subscript using IronWord.

How can images be embedded in a Word document?

Images can be embedded in a Word document by loading an image from a file and adding it to a paragraph as an inline image using IronWord.

What are the steps to load an image into a Word document?

You can load an image from a file or a file stream into a Word document, making it possible to include visual content using IronWord.

How do you configure image properties in a Word document?

Image properties such as text wrapping, dimensions, position offset, and distance from corners can be configured to ensure proper display in the document using IronWord.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?

Nuget Passed