Is it possible to generate a Grayscale or Black and White TIFF image from a colour PDF (using Aspose.PDF)?

Hi Team,

I’m trying to convert a colour PDF to TIFF image using Aspose.PDF. Here’s my current working code:

using (MemoryStream ms = new MemoryStream())
                        {
                            properties.GetDocumentStream(doc).CopyTo(ms);
                            //open document
                            Aspose.Pdf.Document newDoc = new Aspose.Pdf.Document(ms);

                            //create Resolution object
                            Resolution resolution = new Resolution(300);

                            //create TiffSettings object
                            TiffSettings tiffSettings = new TiffSettings();
                            tiffSettings.Compression = CompressionType.None;
                            tiffSettings.Depth = ColorDepth.Default;
                            tiffSettings.SkipBlankPages = false;
                            tiffSettings.Shape = ShapeType.Portrait;

                            //create TIFF device
                            TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);

                            //convert a particular page and save the image to stream
                            tiffDevice.Process(newDoc, outputFolderPath.TrimEnd('\\') + "\\" + doc.DisplayName + ".tiff");
                            newDoc.Save();
                        }

The output of this will always be a colour TIFF image, my questions is that is there any chance we can generate a Grayscale or Black and White TIFF image from a colour PDF using Aspose.PDF TiffSettings?

I noticed Aspose.Words.Saving has ImageSaveOptions which I can use to set ImageColorMode to be either None, Grayscale or BlackAndWhite:

namespace Aspose.Words.Saving
{
    //
    // Summary:
    //     Specifies the color mode for the generated images of document pages.
    public enum ImageColorMode
    {
        //
        // Summary:
        //     The pages of the document will be rendered as color images.
        None = 0,
        //
        // Summary:
        //     The pages of the document will be rendered as grayscale images.
        Grayscale = 1,
        //
        // Summary:
        //     The pages of the document will be rendered as black and white images.
        BlackAndWhite = 2
    }
}

But I could not seem to find this option in Aspose.Pdf. Could anyone please confirm whether this is possible? Thanks.

@ixsunnyle

Thanks for contacting support.

You may please use CompressionType.CCITT4 i.e. Black/ White (T6 Compression) while converting PDF to TIFF, in order to generate Black and White TIFF image. Please consider using following code snippet:

Document doc = new Document(dataDir + "Sample.pdf");
// Create Resolution object
Resolution resolution = new Resolution(300, 300);
// Create TiffSettings object
TiffSettings tiffSettings = new TiffSettings()
{
 Compression = CompressionType.CCITT4
};
// Create TIFF device
TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);
tiffDevice.Process(doc, dataDir + "Sample.tiff");

In case of any further assistance, please feel free to let us know.

Hi @asad.ali,

Thanks for your reply.

How about converting PDF to TIFF Grayscale? Is it possible as well?

Cheers,
Sunny

@ixsunnyle

Thanks for posting your inquiry.

Please use following TiffSettings, in order to obtain grayscale TIFF image:

TiffSettings tiffSettings = new TiffSettings()
{
  Compression = CompressionType.LZW,
  Depth = ColorDepth.Format1bpp
}; 

In case of any further assistance, please feel free to let us know.

how about for RGB and CMYK?

@brian.yap

Would you please elaborate your requirements a little more.

I had a requirement to change colorspace to RGB, CMYK and grayscale. I have tested the grayscale conversion and it seems to work. Not familiar with RGB and CMYK though.

@brian.yap

Please remove compression option from above code snippet or assign it as CompressionType.None in order to obtain RGB colorspace. Furthermore, please share your sample PDF document so that we can test the scenario for your second requirement i.e. CMYK and share our feedback with you.