News Category

How to Convert Word to Tiff

2012-03-23 02:30:23 Written by  support iceblue
Rate this item
(0 votes)

TIFF (Tagged Image File Format) is a flexible file format that is used to store images, including photos and art images. It’s popular and widely supported by image-manipulation applications, publishing and page layout applications, and scanning, faxing, word processing applications, etc. It can be a container holding compressed (lossy) JPEG and (lossless) PackBits compressed images. The ability to store image data in a lossless format makes a TIFF file to be a useful image archive. Therefore, sometimes developers need to convert documents in other formats (such as word) to TIFF format.

Spire.Doc, a powerful .NET word component which enables developers to convert files from word to TIFF easily. This article is going to introduce you the solution of converting word to TIFF by using Spire.Doc.

Note: before start, please download the latest version of Spire.Doc, then add the .dll in the bin folder as the reference of Visual Studio.

The sample word file:

How to Convert Word to Tiff

Follow the detail steps below:

Step 1: create a new document instance and load a word document from file.

Document document = new Document(@"E:\Program Files\testing.docx");

Step 2: use document.SaveToImages() method to save the word document as Image array.

private static Image[] SaveAsImage(Document document)
{     
    Image[] images = document.SaveToImages(ImageType.Bitmap);    
    return images;
}

Step 3: use JoinTiffImages() method to save the images from word pages to tiff image type, with the specified encoder and image-encoder parameters.

public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
    //use the save encoder
    System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
    EncoderParameters ep = new EncoderParameters(2);
    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
    ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compressEncoder);
    Image pages = images[0];
    int frame = 0;
    ImageCodecInfo info = GetEncoderInfo("image/tiff");
    foreach (Image img in images)
    {
        if (frame == 0)
        {
            pages = img;
            //save the first frame
            pages.Save(outFile, info, ep);
        }
        else
        {
            //save the intermediate frames
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
            pages.SaveAdd(img, ep);
        }
        if (frame == images.Length - 1)
        {
            //flush and close.
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
            pages.SaveAdd(ep);
        }
        frame++;
    }
}

The result TIFF file:

How to Convert Word to Tiff

Full codes:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace convert_word_to_tiff
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document(@"E:\Program Files\testing.docx");
            JoinTiffImages(SaveAsImage(document),"6056result.tiff",EncoderValue.CompressionLZW);
            System.Diagnostics.Process.Start("6056result.tiff");
        }
        private static Image[] SaveAsImage(Document document)
        {     
            Image[] images = document.SaveToImages(ImageType.Bitmap);    
            return images;
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }

        public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            //use the save encoder
            System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compressEncoder);
            Image pages = images[0];
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;
                    //save the first frame
                    pages.Save(outFile, info, ep);
                }

                else
                {
                    //save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }
                if (frame == images.Length - 1)
                {
                    //flush and close.
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing
Imports System.Drawing.Imaging

Namespace convert_word_to_tiff
	Class Program
		Private Shared Sub Main(args As String())
			Dim document As New Document("E:\Program Files\testing.docx")
			JoinTiffImages(SaveAsImage(document), "6056result.tiff", EncoderValue.CompressionLZW)
			System.Diagnostics.Process.Start("6056result.tiff")
		End Sub
		Private Shared Function SaveAsImage(document As Document) As Image()
			Dim images As Image() = document.SaveToImages(ImageType.Bitmap)
			Return images
		End Function

		Private Shared Function GetEncoderInfo(mimeType As String) As ImageCodecInfo
			Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
			For j As Integer = 0 To encoders.Length - 1
				If encoders(j).MimeType = mimeType Then
					Return encoders(j)
				End If
			Next
			Throw New Exception(mimeType & Convert.ToString(" mime type not found in ImageCodecInfo"))
		End Function

		Public Shared Sub JoinTiffImages(images As Image(), outFile As String, compressEncoder As EncoderValue)
			'use the save encoder
			Dim enc As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.SaveFlag
			Dim ep As New EncoderParameters(2)
			ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame))
			ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.Compression, CLng(compressEncoder))
			Dim pages As Image = images(0)
			Dim frame As Integer = 0
			Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff")
			For Each img As Image In images
				If frame = 0 Then
					pages = img
					'save the first frame
					pages.Save(outFile, info, ep)
				Else

					'save the intermediate frames
					ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.FrameDimensionPage))

					pages.SaveAdd(img, ep)
				End If
				If frame = images.Length - 1 Then
					'flush and close.
					ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush))
					pages.SaveAdd(ep)
				End If
				frame += 1
			Next
		End Sub
	End Class
End Namespace

Spire.Doc can convert Word to most of popular file formats. It can convert Word to PDF, HTML, XML, RTF, Text, ePub, etc. Click to learn more

Additional Info

  • tutorial_title: Convert Word to Tiff
Last modified on Friday, 03 September 2021 03:21