Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Explanation:
WordDocument
.TextRun
is used to define a piece of text with specified fonts, size, color, and style.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.SaveAs
method is used to save the completed document to the specified path.Further Reading: Document Element Tutorial