Effortless Image Rotation in Docx Files programmatically. Image manipulation is a matter of a few lines of source code with this open-source API.

How to Rotate an Image File and add it to Docx File using C#

Overview

Welcome to another blog post as we continue exploring FileFormat.Words for .NET. In our previous article, we delved into the process of adding images to Word documents using the open-source FileFormat.Words library in a .NET application. Building upon that knowledge, we will now explore how to programmatically rotate an image within an existing Word document. By installing FileFormat.Words into our .NET application project, we can take advantage of its powerful features for image manipulation. In this blog post, we will guide you through the entire process, which is remarkably straightforward and easy to follow. So, let’s dive in and discover how to rotate an image in a Word document using C#

We will cover the following points in this article:

Open-Source Docx Image Manipulator – API Installation

Installing this open-source Image Manipulator is a breeze, offering you two straight forward methods to incorporate this .NET library into your application project. You can either download its NuGet Package or quickly execute the following command within the NuGet Package Manager.

Install-Package FileFormat.Words

How to Rotate an Image in a Docx File using FileFormat.Words

This section demonstrates how to rotate and image and add it to Docx file in C# using this open-source .NET library.

Please follow the following steps and the code snippet to achieve the functionality:

  • Initialize an instance of the Document class and load an existing Word document.
  • Instantiate the constructor of the Body class with the Document class object.
  • Create an object of the Paragraph class.
  • Instantiate an instance of the Run class that represents a run of characters in a Word document.
  • Instantiate the constructor of the Image class object.
  • Call the RotateImage method of the Image class.
  • Invoke the AppendChild method of the body class to add a paragraph to the document.
  • The Save method will save the Word document onto the disk.
using FileFormat.Words;
namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            <br />            string rootDir = "/Users/fahadadeelqazi/Projects/Aspose/FileFormat.Words-for-.NET/TestDocs/";<br />            string imagePath = rootDir + "testimage.jpeg";<br />            // Initialize an instance of the Document class.
            using (Document doc = new Document())
            {
                var image = new FileFormat.Words.Image(doc, imagePath, 100, 100);<br />                var body = new Body(doc);<br />                var para = new Paragraph();<br />                var run = new Run();<br />                run.AppendChild(image);<br />                para.AppendChild(run);<br /><br />                body.AppendChild(para); <br /><br />                image.RotateImage(60);<br /><br />                doc.Save(rootDir + "newFile1.docx");
            }

        }

    }
}

The output of the above code snippet is shown in the image below:

How to Rotate an Image File to Word Document in Csharp

Conclusion

In this blog post, we have explored the process of rotating and adding images to a Word document using an open-source Image Manipulator .NET library. Leveraging the power of FileFormat.Words for .NET, an enterprise-level API, we have discovered its rich array of features that enable us to programmatically add, extract, and rotate images within Word documents

Comprehensive documentation is available to guide you in the development and utilization of this open-source Image Manipulator.

Finally, fileformat.com continues to write blog posts on other topics. So, please stay in touch for updates. Moreover, you can follow us on our social media platforms, including FacebookLinkedIn, and Twitter.

Contribute

Since FileFormat.Words for .NET is an open-source project and is available on GitHub. So, the contribution from the community is much appreciated.

Ask a Question

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

See Also