Working with Images in Word document
26 Jun 202424 minutes to read
DocIO provides support for both inline and absolute positioned images.
- Inline images: The position of the image is constrained to the lines of text on the page.
- Absolute positioned images: The images can be positioned anywhere irrespective of the lines of text.
The following code example explains how to add image to the paragraph.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph firstParagraph = section.AddParagraph();
//Adds image to the paragraph
FileStream imageStream = new FileStream(@"Image.png", FileMode.Open, FileAccess.ReadWrite);
IWPicture picture = firstParagraph.AppendPicture(imageStream);
//Sets height and width for the image
picture.Height = 100;
picture.Width = 100;
//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the Word document
document.Close();
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph firstParagraph = section.AddParagraph();
//Adds image to the paragraph
IWPicture picture = firstParagraph.AppendPicture(Image.FromFile("Image.png"));
//Sets height and width for the image
picture.Height = 100;
picture.Width = 100;
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Creates a new Word document
Dim document As New WordDocument()
'Adds new section to the document
Dim section As IWSection = document.AddSection()
'Adds new paragraph to the section
Dim firstParagraph As IWParagraph = section.AddParagraph()
'Adds image to the paragraph
Dim picture As IWPicture = firstParagraph.AppendPicture(Image.FromFile("Image.png"))
'Sets height and width for the image
picture.Height = 100
picture.Width = 100
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()
You can download a complete working sample from GitHub.
Replace image
Image present in the document can be replaced with a new image. This can be achieved by iterating through the paragraph items.
The following code example explains how to replace an existing image.
FileStream fileStream = new FileStream(@"Template.docx", FileMode.Open, FileAccess.ReadWrite);
//Loads the template document
WordDocument document = new WordDocument(fileStream, FormatType.Automatic);
WTextBody textbody = document.Sections[0].Body;
//Iterates through the paragraphs of the textbody
foreach (WParagraph paragraph in textbody.Paragraphs)
{
//Iterates through the child elements of paragraph
foreach (ParagraphItem item in paragraph.ChildEntities)
{
if (item is WPicture)
{
WPicture picture = item as WPicture;
//Replaces the image
if (picture.Title == "Bookmark")
{
FileStream imageStream = new FileStream(@"Image.png", FileMode.Open, FileAccess.ReadWrite);
picture.LoadImage(imageStream);
}
}
}
}
//Saves the Word document to MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the Word document.
document.Close();
//Loads the template document
WordDocument document = new WordDocument("Template.docx");
WTextBody textbody = document.Sections[0].Body;
//Iterates through the paragraphs of the textbody
foreach (WParagraph paragraph in textbody.Paragraphs)
{
//Iterates through the child elements of paragraph
foreach (ParagraphItem item in paragraph.ChildEntities)
{
if (item is WPicture)
{
WPicture picture = item as WPicture;
//Replaces the image
if (picture.Title == "Bookmark")
picture.LoadImage(Image.FromFile("Image.png"));
}
}
}
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Loads the template document
Dim document As New WordDocument("Template.docx")
Dim textbody As WTextBody = document.Sections(0).Body
'Iterates through the paragraphs of the textbody
For Each paragraph As WParagraph In textbody.Paragraphs
'Iterates through the child elements of paragraph
For Each item As ParagraphItem In paragraph.ChildEntities
If TypeOf item Is WPicture Then
Dim picture As WPicture = TryCast(item, WPicture)
'Replaces the image
If picture.Title = "Bookmark" Then
picture.LoadImage(Image.FromFile("Image.png"))
End If
End If
Next
Next
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()
You can download a complete working sample from GitHub.
Remove image
Images can be removed from the document by removing it from the paragraph items.
The following code example explains how to remove the image from the paragraph items.
FileStream fileStream = new FileStream(@"Template.docx", FileMode.Open, FileAccess.ReadWrite);
//Loads the template document
WordDocument document = new WordDocument(fileStream, FormatType.Automatic);
WTextBody textbody = document.Sections[0].Body;
//Iterates through the paragraphs of the textbody
foreach (WParagraph paragraph in textbody.Paragraphs)
{
//Iterates through the child elements of paragraph
for (int i = 0; i < paragraph.ChildEntities.Count; i++)
{
//Removes images from the paragraph
if (paragraph.ChildEntities[i] is WPicture)
{
paragraph.Items.RemoveAt(i);
i--;
}
}
}
//Saves the Word document to MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the Word document.
document.Close();
//Loads the template document
WordDocument document = new WordDocument("Template.docx");
WTextBody textbody = document.Sections[0].Body;
//Iterates through the paragraphs of the textbody
foreach (WParagraph paragraph in textbody.Paragraphs)
{
//Iterates through the child elements of paragraph
for (int i = 0; i < paragraph.ChildEntities.Count; i++)
{
//Removes images from the paragraph
if (paragraph.ChildEntities[i] is WPicture)
{
paragraph.Items.RemoveAt(i);
i--;
}
}
}
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Loads the template document
Dim document As New WordDocument("Template.docx")
Dim textbody As WTextBody = document.Sections(0).Body
'Iterates through the paragraphs of the textbody
For Each paragraph As WParagraph In textbody.Paragraphs
'Iterates through the child elements of paragraph
For i As Integer = 0 To paragraph.ChildEntities.Count - 1
'Removes images from the paragraph
If TypeOf paragraph.ChildEntities(i) Is WPicture Then
paragraph.Items.RemoveAt(i)
i -= 1
End If
Next
Next
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()
You can download a complete working sample from GitHub.
Format and rotate images
Absolute positioned images have properties such as position, wrap formats, and alignments. These properties are not applicable when the text wrapping style is inline. You can also rotate an image and apply flipping (horizontal and vertical) to it.
The following code example explains how various picture formats can be applied to the picture.
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("This paragraph has picture. ");
FileStream imageStream = new FileStream(@"Image.png", FileMode.Open, FileAccess.ReadWrite);
//Appends new picture to the paragraph
WPicture picture = paragraph.AppendPicture(imageStream) as WPicture;
//Sets text wrapping style – When the wrapping style is inline, the images are not absolutely positioned. It is added next to the text range.
picture.TextWrappingStyle = TextWrappingStyle.Square;
//Sets horizontal and vertical origin
picture.HorizontalOrigin = HorizontalOrigin.Page;
picture.VerticalOrigin = VerticalOrigin.Paragraph;
//Sets width and height for the paragraph
picture.Width = 150;
picture.Height = 100;
//Sets horizontal and vertical position for the picture
picture.HorizontalPosition = 200;
picture.VerticalPosition = 150;
//Sets lock aspect ratio for the picture
picture.LockAspectRatio = true;
picture.Name = "PictureName";
//Sets horizontal and vertical alignments
picture.HorizontalAlignment = ShapeHorizontalAlignment.Center;
picture.VerticalAlignment = ShapeVerticalAlignment.Bottom;
//Sets 90 degree rotation
picture.Rotation = 90;
//Sets horizontal flip
picture.FlipHorizontal = true;
//Saves the Word document to MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the Word document.
document.Close();
//Creates a new Word document
WordDocument document = new WordDocument();
//Adds new section to the document
IWSection section = document.AddSection();
//Adds new paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.AppendText("This paragraph has picture. ");
//Appends new picture to the paragraph
WPicture picture = paragraph.AppendPicture(Image.FromFile("Image.png")) as WPicture;
//Sets text wrapping style – When the wrapping style is inline, the images are not absolutely positioned. It is added next to the text range.
picture.TextWrappingStyle = TextWrappingStyle.Square;
//Sets horizontal and vertical origin
picture.HorizontalOrigin = HorizontalOrigin.Page;
picture.VerticalOrigin = VerticalOrigin.Paragraph;
//Sets width and height for the paragraph
picture.Width = 150;
picture.Height = 100;
//Sets horizontal and vertical position for the picture
picture.HorizontalPosition = 200;
picture.VerticalPosition = 150;
//Sets lock aspect ratio for the picture
picture.LockAspectRatio = true;
picture.Name = "PictureName";
//Sets horizontal and vertical alignments
picture.HorizontalAlignment = ShapeHorizontalAlignment.Center;
picture.VerticalAlignment = ShapeVerticalAlignment.Bottom;
//Sets 90 degree rotation
picture.Rotation = 90;
//Sets horizontal flip
picture.FlipHorizontal = true;
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Creates a new Word document
Dim document As New WordDocument()
'Adds new section to the document
Dim section As IWSection = document.AddSection()
'Adds new paragraph to the section
Dim paragraph As IWParagraph = section.AddParagraph()
paragraph.AppendText("This paragraph has picture. ")
'Appends new picture to the paragraph
Dim picture As WPicture = TryCast(paragraph.AppendPicture(Image.FromFile("Image.png")), WPicture)
'Sets text wrapping style – When the wrapping style is inline, the images are not absolutely positioned. It is added next to the text range.
picture.TextWrappingStyle = TextWrappingStyle.Square
'Sets horizontal and vertical origin
picture.HorizontalOrigin = HorizontalOrigin.Page
picture.VerticalOrigin = VerticalOrigin.Paragraph
'Sets width and height for the paragraph
picture.Width = 150
picture.Height = 100
'Sets horizontal and vertical position for the picture
picture.HorizontalPosition = 200
picture.VerticalPosition = 150
'Sets lock aspect ratio for the picture
picture.LockAspectRatio = true
picture.Name = "PictureName"
'Sets horizontal and vertical alignments
picture.HorizontalAlignment = ShapeHorizontalAlignment.Center
picture.VerticalAlignment = ShapeVerticalAlignment.Bottom
'Sets 90 degree rotation
picture.Rotation = 90
'Sets horizontal flip
picture.FlipHorizontal = true
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()
You can download a complete working sample from GitHub.
Find an image by title
An Image with a specific title can be retrieved by iterating the paragraph items that can be used for further manipulations.
The following code example explains how images can be iterated from the document elements.
FileStream fileStream = new FileStream(@"Template.docx", FileMode.Open, FileAccess.ReadWrite);
//Loads an existing Word document into DocIO instance
WordDocument document = new WordDocument(fileStream, FormatType.Docx);
//Gets textbody content
WTextBody textBody = document.Sections[0].Body;
//Iterates through the textbody child entities
foreach (TextBodyItem item in textBody.ChildEntities)
{
if (item is WParagraph)
{
WParagraph paragraph = item as WParagraph;
foreach (ParagraphItem paraItem in paragraph.ChildEntities)
{
//Gets the image from its title and modifies its width and height
if (paraItem is WPicture)
{
WPicture picture = paraItem as WPicture;
if (picture.Title == "Bookmark")
{
picture.Width = 150;
picture.Height = 100;
}
}
}
}
}
//Saves the Word document to MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the Word document.
document.Close();
//Creates a new Word document
WordDocument document = new WordDocument("Template.docx");
//Gets textbody content
WTextBody textBody = document.Sections[0].Body;
//Iterates through the textbody child entities
foreach (TextBodyItem item in textBody.ChildEntities)
{
if (item is WParagraph)
{
WParagraph paragraph = item as WParagraph;
foreach (ParagraphItem paraItem in paragraph.ChildEntities)
{
//Gets the image from its title and modifies its width and height
if (paraItem is WPicture)
{
WPicture picture = paraItem as WPicture;
if (picture.Title == "Bookmark")
{
picture.Width = 150;
picture.Height = 100;
}
}
}
}
}
//Saves the Word document
document.Save("Sample.docx", FormatType.Docx);
//Closes the document
document.Close();
'Creates a new Word document
Dim document As New WordDocument("Template.docx")
'Gets textbody content
Dim textBody As WTextBody = document.Sections(0).Body
'Iterates through the textbody child entities
For Each item As TextBodyItem In textBody.ChildEntities
If TypeOf item Is WParagraph Then
Dim paragraph As WParagraph = TryCast(item, WParagraph)
For Each paraItem As ParagraphItem In paragraph.ChildEntities
'Gets the image from its title and modifies its width and height
If TypeOf paraItem Is WPicture Then
Dim picture As WPicture = TryCast(paraItem, WPicture)
If picture.Title = "Bookmark" Then
picture.Width = 150
picture.Height = 100
End If
End If
Next
End If
Next
'Saves the Word document
document.Save("Sample.docx", FormatType.Docx)
'Closes the document
document.Close()
You can download a complete working sample from GitHub.
Add Image caption
You can add caption to an image and update the caption numbers (Sequence fields) using AddCaption method.
The following code example shows how to add caption to an image.
//Creates a new document
WordDocument document = new WordDocument();
//Adds a new section to the document.
IWSection section = document.AddSection();
//Sets margin of the section
section.PageSetup.Margins.All = 72;
//Adds a paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
//Adds image to the paragraph
FileStream imageStream = new FileStream(@"Google.png", FileMode.Open, FileAccess.ReadWrite);
IWPicture picture = paragraph.AppendPicture(imageStream);
//Adds Image caption
IWParagraph lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage);
//Aligns the caption
lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
//Sets after spacing
lastParagragh.ParagraphFormat.AfterSpacing = 12f;
//Sets before spacing
lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;
//Adds a paragraph to the section
paragraph = section.AddParagraph();
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
//Adds image to the paragraph
imageStream = new FileStream(@"Yahoo.png", FileMode.Open, FileAccess.ReadWrite);
picture = paragraph.AppendPicture(imageStream);
//Adds Image caption
lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage);
//Aligns the caption
lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
//Sets before spacing
lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;
//Updates the fields in Word document
document.UpdateDocumentFields();
//Saves the Word document to MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
//Closes the document.
document.Close();
//Creates a new document
WordDocument document = new WordDocument();
//Adds a new section to the document
IWSection section = document.AddSection();
//Sets margin of the section
section.PageSetup.Margins.All = 72;
//Adds a paragraph to the section
IWParagraph paragraph = section.AddParagraph();
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
//Adds image to the paragraph
IWPicture picture = paragraph.AppendPicture(Image.FromFile("Google.png"));
//Adds Image caption
IWParagraph lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage);
//Aligns the caption
lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
//Sets after spacing
lastParagragh.ParagraphFormat.AfterSpacing = 12f;
//Sets before spacing
lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;
//Adds a paragraph to the section
paragraph = section.AddParagraph();
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
//Adds image to the paragraph
picture = paragraph.AppendPicture(Image.FromFile("Yahoo.png"));
//Adds Image caption
lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage);
//Aligns the caption
lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
//Sets before spacing
lastParagragh.ParagraphFormat.BeforeSpacing = 1.5f;
//Updates the fields in Word document
document.UpdateDocumentFields();
//Saves and closes the document
document.Save("Sample.docx", FormatType.Docx);
document.Close();
'Creates a new document
Dim document As WordDocument = New WordDocument
'Adds a new section to the document
Dim section As IWSection = document.AddSection
'Sets margin of the section
section.PageSetup.Margins.All = 72
'Adds a paragraph to the section
Dim paragraph As IWParagraph = section.AddParagraph
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center
'Adds image to the paragraph
Dim picture As IWPicture = paragraph.AppendPicture(Image.FromFile("Google.png"))
'Adds Image caption
Dim lastParagragh As IWParagraph = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage)
'Aligns the caption
lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center
'Sets after spacing
lastParagragh.ParagraphFormat.AfterSpacing = 12.0F
'Sets before spacing
lastParagragh.ParagraphFormat.BeforeSpacing = 1.5F
'Adds a paragraph to the section
paragraph = section.AddParagraph
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center
'Adds image to the paragraph
picture = paragraph.AppendPicture(Image.FromFile("Yahoo.png"))
'Adds Image caption
lastParagragh = picture.AddCaption("Figure", CaptionNumberingFormat.Roman, CaptionPosition.AfterImage)
'Aligns the caption
lastParagragh.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center
'Sets before spacing
lastParagragh.ParagraphFormat.BeforeSpacing = 1.5F
'Updates the fields in Word document
document.UpdateDocumentFields()
'Saves and closes the document
document.Save("Sample.docx", FormatType.Docx)
document.Close()
You can download a complete working sample from GitHub.
By executing the above code example, it generates output Word document as follows.
Add SVG image
To add an SVG image to a paragraph in a Word document using Syncfusion DocIO, you can use the AppendPicture API.
NOTE
To preserve the SVG image in the Word document, pass both the SVG image data and the equivalent bitmap image bytes to DocIO.
The following code example shows how to add an SVG image in a Word document.
///Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add a new section to the document.
IWSection section = document.AddSection();
//Add a new paragraph to the section.
IWParagraph firstParagraph = section.AddParagraph();
//Get the image as a byte array.
byte[] imageBytes = File.ReadAllBytes(Buyers.png);
//Get the SVG image as a byte array.
byte[] svgData = File.ReadAllBytes(Buyers.svg");
//Add SVG image to the paragraph.
IWPicture picture = firstParagraph.AppendPicture(svgData, imageBytes);
//Set height and width for the image.
picture.Height = 100;
picture.Width = 100;
//Save the Word document to MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
}
using (WordDocument document = new WordDocument())
{
//Add a new section to the document.
IWSection section = document.AddSection();
//Add a new paragraph to the section.
IWParagraph firstParagraph = section.AddParagraph();
//Get the image as a byte array.
byte[] imageBytes = File.ReadAllBytes(Buyers.png);
//Get the SVG image as a byte array.
byte[] svgData = File.ReadAllBytes(Buyers.svg");
//Add SVG image to the paragraph.
IWPicture picture = firstParagraph.AppendPicture(svgData, imageBytes);
//Set height and width for the image.
picture.Height = 100;
picture.Width = 100;
//Save the Word document.
document.Save("Sample.docx", FormatType.Docx);
}
Using document As New WordDocument()
' Add a new section to the document.
Dim section As IWSection = document.AddSection()
' Add a new paragraph to the section.
Dim firstParagraph As IWParagraph = section.AddParagraph()
' Get the PNG image as a byte array.
Dim imageBytes As Byte() = File.ReadAllBytes("Buyers.png")
' Get the SVG image as a byte array.
Dim svgData As Byte() = File.ReadAllBytes("Buyers.svg")
' Add SVG image to the paragraph.
Dim picture As IWPicture = firstParagraph.AppendPicture(svgData, ImageType.Metafile, imageBytes)
' Set height and width for the image.
picture.Height = 100
picture.Width = 100
' Save the Word document.
document.Save("Sample.docx", FormatType.Docx)
End Using
You can download a complete working sample from GitHub.
Online Demo
- Explore how to insert an image into the Word document using the .NET Word Library (DocIO) in a live demo here.