C#: Convert Word to TIFF

Converting Word to TIFF can be useful in various scenarios. TIFF files have high quality and wide support, making them versatile for sharing documents. The conversion also "flattens" the Word document, preserving the layout so it appears exactly as the original. This can be helpful when the document needs to be incorporated into another application or workflow that requires image-based files.

In this article, you will learn how to convert Word to TIFF using C# and the Spire.Doc for .NET library.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word to TIFF in C#

Spire.Doc for .NET provides the Document.SaveToImages() method, which enables developers to convert an entire document into an array of images. Subsequently, these individual images can be combined into a single TIFF image using the built-in .NET library.

The steps to convert Word to TIFF using C# are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFile() method.
  • Convert the document into an array of images using Document.SaveToImages() method.
  • Combine these images into a single TIFF file using the custom method ConvertImagesToTiff().
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using System.Drawing.Imaging;

namespace WordToTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document doc = new Document();

            // Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx");

            // Convert the whole document to images
            Image[] images = doc.SaveToImages(ImageType.Bitmap);

            // Convert multiple images into a TIFF file
            ConvertImagesToTiff(images, "ToTiff.tiff", EncoderValue.CompressionLZW);

            // Dispose resource
            doc.Dispose();
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            // Get the image encoders
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                // Find the encoder that matches the specified MIME type
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }

        public static void ConvertImagesToTiff(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            // Set the encoder parameters 
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);

            // Get the first image
            Image pages = images[0];

            // Create a variable
            int frame = 0;

            // Get an ImageCodecInfo object for processing TIFF image codec information
            ImageCodecInfo info = GetEncoderInfo("image/tiff");

            // Iterate through each Image
            foreach (Image img in images)
            {
                // If it's the first frame, save it to the output file with specified encoder parameters
                if (frame == 0)
                {
                    pages = img;
                    pages.Save(outFile, info, ep);
                }
                else
                {
                    // Save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }

                // If it's the last frame, flush the encoder parameters and close the file
                if (frame == images.Length - 1)
                {
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}

C#: Convert Word to TIFF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.