We often need to resize images to a desired size after we insert them into a Word document. This article demonstrates how to programmatically resize images in a Word document using Spire.Doc and C#.
Below is the screenshot of the example document we used for demonstration:
Detail steps:
Step 1: Load the Word document.
Document document = new Document("Input.docx");
Step 2: Get the first section and the first paragraph in the section.
Section section = document.Sections[0]; Paragraph paragraph = section.Paragraphs[0];
Step 3: Resize images in the paragraph.
foreach (DocumentObject docObj in paragraph.ChildObjects) { if (docObj is DocPicture) { DocPicture picture = docObj as DocPicture; picture.Width = 50f; picture.Height = 50f; } }
Step 4: Save the document.
document.SaveToFile("ResizeImages.docx");
Output:
Full code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Resize { class Program { static void Main(string[] args) { Document document = new Document("Input.docx"); Section section = document.Sections[0]; Paragraph paragraph = section.Paragraphs[0]; foreach (DocumentObject docObj in paragraph.ChildObjects) { if (docObj is DocPicture) { DocPicture picture = docObj as DocPicture; picture.Width = 50f; picture.Height = 50f; } } document.SaveToFile("ResizeImages.docx"); } } }