The simplest way to convert DOCX to a set of Images in .NET C# app


Hi Developers,

In this topic you will find a simplest solution to convert DOCX to Images(PNG, JPEG, TIFF, any) in C#. To start please download these components:


          using System;
using System.IO;
using System.Collections;

namespace Sample
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Convert DOCX file to set of PNG images");

            string pathToDocx = @"D:\simple text.docx";
						
            // We need this pdf as bridge between docx and images.
            // We remove this pdf file after conversion.
            string pathToPdf = Path.ChangeExtension(pathToDocx, ".pdf");
            string pathToImages = Path.GetDirectoryName(pathToDocx);
            string imagePageName = "Page";
            int result = -1;

            // 1. DOCX to PDF
			
            SautinSoft.UseOffice u = new SautinSoft.UseOffice();
			
            // Prepare UseOffice .Net, loads MS Word in memory.
            if (u.InitWord() == 0)
            {
                result = u.ConvertFile(pathToDocx, pathToPdf, SautinSoft.UseOffice.eDirection.DOCX_to_PDF);
				
                // Release MS Word from memory.
                u.CloseWord();
            }
            if (result != 0)
            {
                Console.WriteLine("Converting failed on stage DOCX to PDF!");
                Console.ReadLine();
                return;
            }

            // 2. PDF to Images
            SautinSoft.PdfFocus p = new SautinSoft.PdfFocus();
            p.OpenPdf(pathToPdf);
            if (p.PageCount > 0)
            {
                p.ImageOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
                p.ImageOptions.Dpi = 200;
                p.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.Rgb24bpp;
                p.ToImage(pathToImages, imagePageName);
                Console.WriteLine("{0} page(s) converted successfully!");
                System.Diagnostics.Process.Start(pathToImages);
            }
            else
            {
                Console.WriteLine("Converting failed on stage PDF to Images!");
                Console.ReadLine();
            }
        }
    }
}
                    

If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.