Saving a Document as a Multipage TIFF

When working with documents, you often need to convert your document to a raster image file(s). This is especially relevant if you have to present your document in a readable and printable, but not editable format. For example, you can use a raster image of the first page of your document as a preview. This article describes how to convert a document to a raster image using the example of the TIFF format – one of the more popular image formats.

Converting DOC to Multi-Page TIFF

In Aspose.Words, conversion from DOC to TIFF can be performed with one line of code, by simply passing the “save to” path and the relevant file extension to the save method. The save method automatically derives the SaveFormat from the file name extension specified in the path. The following example demonstrates how to convert a document to the TIFF format:

Specifying Additional Options When Rendering TIFF

You often need to specify additional options, which affect the rendering result. For this purpose, use the ImageSaveOptions class, which contains properties that determine how the document is displayed on the image. You can specify the following:

The following example shows how to convert DOC to TIFF with configured options:

Threshold for TIFF Binarization

A TIFF image can be saved in 1bpp b/w format by setting the pixel_format property to FORMAT1BPP_INDEXED pixel format type, and the tiff_compression property to either CCITT3 or CCITT4.

For image segmentation, Aspose.Words uses the simplest method — thresholding. This method converts a gray‑scale TIFF image into a binary image, using a threshold value. Therefore, when a document needs to be converted to the TIFF file format, it is possible to get or set the threshold for TIFF binarization via the threshold_for_floyd_steinberg_dithering property. The default value for this property is set to 128, and the higher this value, the darker the image.

The following example shows how to perform TIFF binarization with a specified threshold:

Below you can compare images on which TIFF binarization was performed at various threshold values:

saving-a-document-as-a-multipage-tiff-aspose-words-net

FAQ

  1. Q: How can I change the resolution of the saved TIFF image?
    A: Use the horizontal_resolution and vertical_resolution properties of ImageSaveOptions. Set them to the desired DPI before calling document.save. Example:

    from aspose.words import Document, ImageSaveOptions, SaveFormat
    doc = Document("input.docx")
    options = ImageSaveOptions(SaveFormat.TIFF)
    options.horizontal_resolution = 300
    options.vertical_resolution = 300
    doc.save("output.tiff", options)
    
  2. Q: How do I save only specific pages of a document to a multi‑page TIFF?
    A: Configure the page_set property with a PageSet that lists the required page numbers. Example:

    from aspose.words import Document, ImageSaveOptions, SaveFormat, PageSet
    doc = Document("input.docx")
    options = ImageSaveOptions(SaveFormat.TIFF)
    options.page_set = PageSet(1, 3, 5)   # saves pages 1, 3 and 5
    doc.save("selected_pages.tiff", options)
    
  3. Q: Which settings control TIFF compression and how can I choose CCITT4 compression?
    A: Set the tiff_compression property of ImageSaveOptions to TiffCompression.CCITT4. This is suitable for 1‑bit black‑and‑white images. Example:

    from aspose.words import Document, ImageSaveOptions, SaveFormat, TiffCompression
    doc = Document("input.docx")
    options = ImageSaveOptions(SaveFormat.TIFF)
    options.tiff_compression = TiffCompression.CCITT4
    doc.save("compressed.tiff", options)
    
  4. Q: How can I produce a 1‑bit black‑and‑white TIFF image?
    A: Set pixel_format to ImagePixelFormat.FORMAT1BPP_INDEXED and choose an appropriate compression (CCITT3 or CCITT4). Example:

    from aspose.words import Document, ImageSaveOptions, SaveFormat, ImagePixelFormat, TiffCompression
    doc = Document("input.docx")
    options = ImageSaveOptions(SaveFormat.TIFF)
    options.pixel_format = ImagePixelFormat.FORMAT1BPP_INDEXED
    options.tiff_compression = TiffCompression.CCITT3
    doc.save("bw.tiff", options)
    
  5. Q: How do I adjust the threshold used for TIFF binarization?
    A: Use the threshold_for_floyd_steinberg_dithering property. The default is 128; increase the value for a darker image. Example:

    from aspose.words import Document, ImageSaveOptions, SaveFormat, TiffBinarizationMethod
    doc = Document("input.docx")
    options = ImageSaveOptions(SaveFormat.TIFF)
    options.tiff_binarization_method = TiffBinarizationMethod.FLOYD_STEINBERG_DITHERING
    options.threshold_for_floyd_steinberg_dithering = 150
    doc.save("threshold.tiff", options)