Convert Word document to Image in Windows Forms
29 Nov 20244 minutes to read
Syncfusion® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert a Word document to image in Windows Forms.
Steps to convert Word document to Image in Windows Forms
Step 1: Create a new Windows Forms application project.
Step 2: Install Syncfusion.DocIO.WinForms NuGet package as a reference to your Windows Forms application from the NuGet.org.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.
Step 3: Include the following namespaces in the Form1.Designer.cs file.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
Step 4: Add a new button in Form1.Designer.cs to create Word file as follows.
private Button btnCreate;
private Label label;
private void InitializeComponent()
{
this.label = new System.Windows.Forms.Label();
this.btnCreate = new System.Windows.Forms.Button();
this.SuspendLayout();
// label
this.label.Location = new System.Drawing.Point(0, 40);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(426, 35);
this.label.TabIndex = 0;
this.label.Text = "Click the button to Convert Word document to image generated by Essential DocIO";
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// btnCreate
this.btnCreate.Location = new System.Drawing.Point(90, 110);
this.btnCreate.Name = "btnCreate";
this.btnCreate.Size = new System.Drawing.Size(223, 36);
this.btnCreate.TabIndex = 1;
this.btnCreate.Text = "Convert Word to Image";
this.btnCreate.Click += new System.EventHandler(this.btnConvert_Click);
// Form1
this.ClientSize = new System.Drawing.Size(450, 150);
this.Controls.Add(this.label);
this.Controls.Add(this.btnCreate);
this.Name = "Form1";
this.Text = "Convert Word document to Image";
this.ResumeLayout(false);
}
Step 5: Add the following code in btnConvert_Click to convert Word document to image with simple text.
//Load the existing Word document
using (WordDocument document = new WordDocument("Input.docx", FormatType.Docx))
{
//Convert the first page of the Word document into an image.
Image image = document.RenderAsImages(0, ImageType.Bitmap);
//Save the image as jpeg.
image.Save("WordToImage.Jpeg", ImageFormat.Jpeg);
}
You can download a complete working sample from GitHub.
By executing the program, you will get the image as follows.
Click here to explore the rich set of Syncfusion® Word library (DocIO) features.
An online sample link to convert Word document to image in ASP.NET Core.