A table in Microsoft Word can contain a variety of elements, including text, images, and many more. Sometimes, you may want to insert images into a table to present some information or extract images from a table for use in other documents. This article will teach you how to insert or extract images from tables in Word documents in C# and VB.NET using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Insert Images into a Table in a Word Document in C# and VB.NET
Spire.Doc provides the TableCell.Paragraphs[int].AppendPicture() method which enables you to add an image to a specific table cell. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get a specific section in the document by its index through Document.Sections[int] property.
- Get a specific table in the section by its index through Section.Tables[int] property.
- Access a specific cell to which you want to add an image through Table.Row[int].Cells[int] property.
- Add an image to a specific paragraph of the cell using TableCell.Paragraphs[int].AppendPicture() method.
- Set image width and height through DocPicture.Width and DocPicture.Height properties.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Fields; using System.Drawing; namespace InsertImagesIntoTable { class Program { static void Main(string[] args) { //Initialize an instance of the Document class Document doc = new Document(); //Load a Word document doc.LoadFromFile("Table.docx"); //Get the first section of the document Section section = doc.Sections[0]; //Get the first table from the first section Table table = (Table)section.Tables[0]; //Add an image to the 3rd cell of the second row in the table TableCell cell = table.Rows[1].Cells[2]; DocPicture picture = cell.Paragraphs[0].AppendPicture(Image.FromFile("doc.png")); //Set image width and height picture.Width = 100; picture.Height = 100; //Add an image to the 3rd cell of the 3rd row in the table cell = table.Rows[2].Cells[2]; picture = cell.Paragraphs[0].AppendPicture(Image.FromFile("xls.png")); //Set image width and height picture.Width = 100; picture.Height = 100; //Save the result document doc.SaveToFile("AddImageToTable.docx", FileFormat.Docx2013); } } }
Extract Images from a Table in a Word Document in C# and VB.NET
To extract images from a table, you need to iterate through all rows in the tale, all cells in each row, all paragraphs in each cell and all child objects in each paragraph, then find the objects that are of DocPicture type and call DocPicture.Image.Save() method to save them to a specified file path. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get a specific section in the document by its index through Document.Sections[int] property.
- Get a specific table in the section by its index through Section.Tables[int] property.
- Iterate through all rows in the table.
- Iterate through all cells in each row.
- Iterate through all paragraphs in each cell.
- Iterate through all child objects in each paragraph.
- Check if the current child object is of DocPicture type.
- If the result is true, typecast the object as DocPicture and call DocPicture.Image.Save() method to save the image to a specified file path.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Drawing.Imaging; namespace ExtractImagesFromTable { class Program { static void Main(string[] args) { //Initialize an instance of the Document class Document doc = new Document(); //Load a Word document doc.LoadFromFile("AddImageToTable.docx"); //Get the first section of the document Section section = doc.Sections[0]; //Get the first table from the first section Table table = (Table)section.Tables[0]; int index = 0; string imageName = null; //Iterate through all rows in the table for (int i = 0; i < table.Rows.Count; i++) { //Iterate through all cells in each row for (int j = 0; j < table.Rows[i].Cells.Count; j++) { //Iterate through all paragraphs in each cell foreach (Paragraph para in table[i, j].Paragraphs) { //Iterate through all child objects in each paragraph foreach (DocumentObject obj in para.ChildObjects) { //Check if the current child object is of DocPicture type if (obj is DocPicture) { //Save the images to a specified file path imageName = string.Format(@"images\TableImage-{0}.png", index); (obj as DocPicture).Image.Save(imageName, ImageFormat.Png); index++; } } } } } } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.