Effortless Image Resize in Docx Files. Simplify image manipulation with this open-source API, enabling easy resizing in just a few lines of code.

How to Resize Image in Word Document in csharp using FileFormat.Words

Overview

Welcome to another informative blog post as we delve into the world of FileFormat.Words for .NET. In this article, we will focus on image resizing within Word documents, leveraging the capabilities of the open-source FileFormat.Words library. With this powerful tool, image manipulation becomes seamless and efficient in your .NET applications. Follow along as we guide you through the straightforward process of programmatically resizing images in Word documents using C#. Let’s explore the possibilities together!

In this article, we will explore the following topics:

Installing the Open-Source Docx Image Manipulator API

Installing this open-source Image Manipulator is effortless, providing two straightforward methods to integrate this .NET library into your application project. Choose to download the NuGet Package or execute a simple command in the NuGet Package Manager for a seamless installation process.

Install-Package FileFormat.Words

How to Resize an Image in a Word Document File using FileFormat.Words

This section illustrates how to effortlessly resize and add images to a Docx file in C# using our powerful open-source .NET library. Follow these steps and utilize the provided code snippet to achieve this functionality:

  • Initialize an instance of the Document class and load an existing Word document.
  • Create an object of the Body class, passing the Document class object as a parameter.
  • Instantiate a Paragraph class object.
  • Initialize a Run class instance, representing a run of characters in a Word document.
  • Instantiate the Image class object using its constructor.
  • Invoke the ResizeImage method of the Image class.
  • Utilize the AppendChild method of the Body class to add a paragraph to the document.
  • Finally, save the Word document onto the disk using the Save method.

using FileFormat.Words;
namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            
            string rootDir = "/Users/fahadadeelqazi/Projects/Aspose/FileFormat.Words-for-.NET/TestDocs/";
            string imagePath = rootDir + "testimage.jpeg";
            // Initialize an instance of the Document class.
            using (Document doc = new Document())
            {
                var image = new FileFormat.Words.Image(doc, imagePath, 100, 100);

                var body = new Body(doc);
                var para = new Paragraph();
                var run = new Run();
                run.AppendChild(image);
                para.AppendChild(run);

                body.AppendChild(para);                

                image.RotateImage(130);

                image.ResizeImage(200, 200);

                doc.Save(rootDir + "newFile1.docx");
            }

        }

    }
}

The provided code snippet demonstrates image rotation at an angle of 130 degrees and image resizing to dimensions of 200 x 200. The resulting output is depicted in the following image.

How to Resize an image in Word Document using Cshap

Conclusion

In this insightful blog post, we have delved into the process of rotating, resizing, and adding images to Word documents using a remarkable open-source Image Manipulator .NET library. By harnessing the capabilities of the FileFormat.Words for .NET enterprise-level API, we have unveiled its extensive range of features that empower us to seamlessly manipulate images programmatically within Word documents. Experience the versatility and efficiency of this powerful tool for enhancing your document workflows.

For comprehensive guidance and support in developing and utilizing this open-source Image Manipulator, refer to the detailed documentation available.

Stay connected with fileformat.com as we continue to publish insightful blog posts covering a wide range of topics. Follow us on social media platforms such as Facebook, LinkedIn, and Twitter for the latest updates and valuable content.

Contribute

As FileFormat.Words for .NET is an open-source project hosted on GitHub, community contributions are highly encouraged and appreciated.

Ask a Question

You can let us know about your questions or queries on our forum.

See Also