Microsoft Word, as one of the most popular file format programs offers powerful and easy solutions for users to create, design and modify any content at will. Sometimes in a Word document, images can give much more clear explanation than words. Images can make the content colorful and easy to understand for audiences. Many Word developers like adding images in Word document for different reasons. And as you know, adding images in Word document can be very easy.
C# Add Images in Word Document
Microsoft Word offers easy solution to add images in Word document. But here we will use C# add images in Word document and it is also very easy by using Spire.Doc, which is a professional and powerful .NET/Silverlight Word component. Download Spire.Doc (or Spire.Offce) , make sure Spire.Doc (or Spire.Office) and Visual Studio are correctly installed and follow the simple steps below to use C# add images.
Step 1 Create Project
Create a project in Visual Studio and add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.
using System;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace InsertingImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Create Word Document
We can use Spire.Doc to create a new Word document with paragraphs for us adding images in.
//Create a blank word document.
Document document = new Document();
//Add a section.
Section section = document.AddSection();
//Append two paragraphs.
section.AddParagraph().AppendText("Paragraph One.");
section.AddParagraph().AppendText("Paragraph Two.");
//Create an empty paragraph.
Paragraph paragraph = new Paragraph(document);
paragraph.AppendText("This is an image.");
Step 3 Add an Image
Put the image which we want to add into the Word document. Use the code below to add image into Word document.
//Add an image.
paragraph.AppendPicture(Image.FromFile("Image.jpg"));
//Insert the paragraph into the section.
section.Paragraphs.Insert(1, paragraph);
Step 4 Save and Preview
After adding image into Word document, the following code can help us save the file and preview.
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
Now, let’s check out the effect:
C# Add Images in Word Document
Microsoft Word offers easy solution to add images in Word document. But here we will use C# add images in Word document and it is also very easy by using Spire.Doc, which is a professional and powerful .NET/Silverlight Word component. Download Spire.Doc (or Spire.Offce) , make sure Spire.Doc (or Spire.Office) and Visual Studio are correctly installed and follow the simple steps below to use C# add images.
Step 1 Create Project
Create a project in Visual Studio and add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.
using System;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace InsertingImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Create Word Document
We can use Spire.Doc to create a new Word document with paragraphs for us adding images in.
//Create a blank word document.
Document document = new Document();
//Add a section.
Section section = document.AddSection();
//Append two paragraphs.
section.AddParagraph().AppendText("Paragraph One.");
section.AddParagraph().AppendText("Paragraph Two.");
//Create an empty paragraph.
Paragraph paragraph = new Paragraph(document);
paragraph.AppendText("This is an image.");
Step 3 Add an Image
Put the image which we want to add into the Word document. Use the code below to add image into Word document.
//Add an image.
paragraph.AppendPicture(Image.FromFile("Image.jpg"));
//Insert the paragraph into the section.
section.Paragraphs.Insert(1, paragraph);
Step 4 Save and Preview
After adding image into Word document, the following code can help us save the file and preview.
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
Now, let’s check out the effect:
C# Add Images at Specific Locations in Word Document
Sometimes we need add images at specific locations so that it can help readers easily understand the content. With Spire.XLS, we can use C# add images at specific locations in Word document.
Step 1 (The same as above)
Step 2 Load Word Document
Load Word document which we need add images in.
//load the word document
Document doc = new Document();
doc.LoadFromFile(@"..\..\iceblue.doc", FileFormat.Doc);
Step 3 Add images at Specific Locations
First, add first image, set specific location and set image alignment and Text WrappingStyle.
//load the first image
Image image = Image.FromFile(@"..\..\images.jpg");
//add the first image into word document at the specific paragraph
DocPicture picture1 = doc.Sections[0].Paragraphs[1].AppendPicture(image);
//set the first image's alignment and TextWrappingStyle
picture1.VerticalAlignment = ShapeVerticalAlignment.Top;
picture1.HorizontalAlignment = ShapeHorizontalAlignment.Right;
picture1.TextWrappingStyle = TextWrappingStyle.Square;
Then, add another two images:
//load the second image
Image image2 = Image.FromFile(@"..\..\daT.jpg");
//add the second image into doc template at the specific paragraph
DocPicture picture2 = doc.Sections[0].Paragraphs[2].AppendPicture(image2);
//set the second image's alignment and TextWrappingStyle
picture2.VerticalAlignment = ShapeVerticalAlignment.Bottom;
picture2.HorizontalAlignment = ShapeHorizontalAlignment.Left;
picture2.TextWrappingStyle = TextWrappingStyle.Behind;
//load the third image
Image image3 = Image.FromFile(@"..\..\daH.jpg");
//add the third image into doc template at the specific paragraph
DocPicture picture3 = doc.Sections[0].Paragraphs[4].AppendPicture(image3);
//set the third image's alignment and TextWrappingStyle
picture3.VerticalAlignment = ShapeVerticalAlignment.Center;
picture3.HorizontalAlignment = ShapeHorizontalAlignment.Outside;
picture3.TextWrappingStyle = TextWrappingStyle.Inline;
Step 4 Save and Preview
Use the following code to save the file as doc format and preview. Spire.Doc enables developers/programmers save the file to Doc, RTF, HTML, PDF or other popular files.
//save the doc
doc.SaveToFile(@"..\..\result.doc", FileFormat.Doc);
System.Diagnostics.Process.Start(@"..\..\result.doc");
Press F5 to start the project and a Word document will be automatically generated.
Sometimes we need add images at specific locations so that it can help readers easily understand the content. With Spire.XLS, we can use C# add images at specific locations in Word document.
Step 1 (The same as above)
Step 2 Load Word Document
Load Word document which we need add images in.
//load the word document
Document doc = new Document();
doc.LoadFromFile(@"..\..\iceblue.doc", FileFormat.Doc);
Step 3 Add images at Specific Locations
First, add first image, set specific location and set image alignment and Text WrappingStyle.
//load the first image
Image image = Image.FromFile(@"..\..\images.jpg");
//add the first image into word document at the specific paragraph
DocPicture picture1 = doc.Sections[0].Paragraphs[1].AppendPicture(image);
//set the first image's alignment and TextWrappingStyle
picture1.VerticalAlignment = ShapeVerticalAlignment.Top;
picture1.HorizontalAlignment = ShapeHorizontalAlignment.Right;
picture1.TextWrappingStyle = TextWrappingStyle.Square;
Then, add another two images:
//load the second image
Image image2 = Image.FromFile(@"..\..\daT.jpg");
//add the second image into doc template at the specific paragraph
DocPicture picture2 = doc.Sections[0].Paragraphs[2].AppendPicture(image2);
//set the second image's alignment and TextWrappingStyle
picture2.VerticalAlignment = ShapeVerticalAlignment.Bottom;
picture2.HorizontalAlignment = ShapeHorizontalAlignment.Left;
picture2.TextWrappingStyle = TextWrappingStyle.Behind;
//load the third image
Image image3 = Image.FromFile(@"..\..\daH.jpg");
//add the third image into doc template at the specific paragraph
DocPicture picture3 = doc.Sections[0].Paragraphs[4].AppendPicture(image3);
//set the third image's alignment and TextWrappingStyle
picture3.VerticalAlignment = ShapeVerticalAlignment.Center;
picture3.HorizontalAlignment = ShapeHorizontalAlignment.Outside;
picture3.TextWrappingStyle = TextWrappingStyle.Inline;
Step 4 Save and Preview
Use the following code to save the file as doc format and preview. Spire.Doc enables developers/programmers save the file to Doc, RTF, HTML, PDF or other popular files.
//save the doc
doc.SaveToFile(@"..\..\result.doc", FileFormat.Doc);
System.Diagnostics.Process.Start(@"..\..\result.doc");
Press F5 to start the project and a Word document will be automatically generated.
More about Spire.Doc
Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more…
Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more…