How to Work with Document Elements in C#

In this tutorial, we delve into managing Word documents using Iron Word in C#. We start by initializing a Word document object and creating a text run, a segment of the document with consistent styling. The tutorial explains how to style the text run using the Caveat font, size 40, Indian Red color, and bold formatting for a visually impactful result. We then create a paragraph object to serve as a container for content, including text and images. By using the paragraph.add image method, we seamlessly integrate an image into the document. The constructed paragraph, containing both styled text and an image, is added to the document object. Finally, the document is saved to a desired location using the save as method. The tutorial concludes by demonstrating the successful output, highlighting the prominent styling and image placement. Iron Word simplifies the process of creating professional documents programmatically, making it an ideal tool for developers. The video encourages viewers to subscribe for more tutorials and explore Iron Word through a trial subscription available via the provided link.

Here is a basic example code demonstrating the process outlined in the tutorial:

using IronWord;
using System.Drawing;

class Program
{
    static void Main()
    {
        // Initialize a new Word document
        var document = new WordDocument();

        // Create a text run with specific styles
        var textRun = new TextRun("Hello, World!")
        {
            FontFamily = "Caveat", // Set the font family
            FontSize = 40, // Set the font size
            Color = Color.IndianRed, // Set the font color
            Bold = true // Make the text bold
        };

        // Create a paragraph and add the text run to it
        var paragraph = new Paragraph();
        paragraph.AddTextRun(textRun);

        // Add an image to the paragraph (adjust the path as needed)
        string imagePath = "path/to/your/image.png";
        paragraph.AddImage(imagePath);

        // Add the paragraph to the document
        document.AddParagraph(paragraph);

        // Save the document to a desired location
        string savePath = "path/to/save/document.docx";
        document.SaveAs(savePath);
    }
}
using IronWord;
using System.Drawing;

class Program
{
    static void Main()
    {
        // Initialize a new Word document
        var document = new WordDocument();

        // Create a text run with specific styles
        var textRun = new TextRun("Hello, World!")
        {
            FontFamily = "Caveat", // Set the font family
            FontSize = 40, // Set the font size
            Color = Color.IndianRed, // Set the font color
            Bold = true // Make the text bold
        };

        // Create a paragraph and add the text run to it
        var paragraph = new Paragraph();
        paragraph.AddTextRun(textRun);

        // Add an image to the paragraph (adjust the path as needed)
        string imagePath = "path/to/your/image.png";
        paragraph.AddImage(imagePath);

        // Add the paragraph to the document
        document.AddParagraph(paragraph);

        // Save the document to a desired location
        string savePath = "path/to/save/document.docx";
        document.SaveAs(savePath);
    }
}
Imports IronWord
Imports System.Drawing

Friend Class Program
	Shared Sub Main()
		' Initialize a new Word document
		Dim document = New WordDocument()

		' Create a text run with specific styles
		Dim textRun As New TextRun("Hello, World!") With {
			.FontFamily = "Caveat",
			.FontSize = 40,
			.Color = Color.IndianRed,
			.Bold = True
		}

		' Create a paragraph and add the text run to it
		Dim paragraph As New Paragraph()
		paragraph.AddTextRun(textRun)

		' Add an image to the paragraph (adjust the path as needed)
		Dim imagePath As String = "path/to/your/image.png"
		paragraph.AddImage(imagePath)

		' Add the paragraph to the document
		document.AddParagraph(paragraph)

		' Save the document to a desired location
		Dim savePath As String = "path/to/save/document.docx"
		document.SaveAs(savePath)
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  • We start by importing the necessary namespaces and creating an instance of WordDocument.
  • TextRun is used to define a piece of text with specified fonts, size, color, and style.
  • We create a Paragraph object to hold our content, adding a text run and an image.
  • AddTextRun adds the styled text, and AddImage incorporates an image using its file path.
  • Finally, the SaveAs method is used to save the completed document to the specified path.

Further Reading: Document Element Tutorial

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
NEXT >
How to open Word Document in C#

Ready to get started? Version: 2025.6 just released

View Licenses >