Attempting to save word document as image - How to determine color?

I am trying to save a word document out as an image (tiff in my case) and was wondering if there is a way to check the document to see if I need to save it as color or black and white. This is important because of the final size of the image that will be created. Not so much for the single document but that I will be doing this as a server application and will do it quite frequently.

Is there any way to determine the necessary color space when saving? Or will I need to always save as LZW tiff to be safe?

Thanks!

Hi Ben,

Thanks for your inquiry. As per my understanding, you want to save the final Tiff file based on size of output document. There is no way to check the file size before saving the document. In your case, I suggest you please save the document to MemoryStream and check the stream size. Once you know the size of output Tiff file, you can set the TiffCompression according to your requirements. The default value of ImageSaveOptions.TiffCompression is Lzw. Please check the following code example for your kind reference.


Document
doc = new Document(MyDir

  • “in.docx”);

MemoryStream stream = new MemoryStream();

doc.Save(stream, SaveFormat.Tiff);

Console.WriteLine(stream.Length / 1000);

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Tiff);

if (stream.Length / 1000 > 1000)

options.TiffCompression = TiffCompression.Ccitt4;

doc.Save(MyDir + "Out.tiff", options);


Hope this answers your query. Please let us know if you have any more queries.

Sorry but that isn’t exactly my question so let me try to be more clear in my description. This issue I would have is that many times a word document may just be black text on a white background and in this case I could save the file to a group IV tiff. But if the word document has multiple colors for fonts or color images embedded then I would want to save it as LZW tiff to maintain the color. It would be nice if I could know before I try to save if I need to save as LZW or Group IV because if I save a black and white image as a 24 bpp LZW tiff the resultant stream/file will be much larger than necessary which puts an unnecessary resource load on my server application.

Does that make more sense?

Thanks for the quick reply!

Hi Ben,

Thanks for sharing the detail. Aspose.Words does not provide API to check either an image in the document is black and white or not. However, you can use Run.Font.Color property to check the text color. In this case, you need to iterate through all Run node of document. Please use Document.GetChildNodes(NodeType.Run, true) to get the Run node of document. Run class represents a run of characters
with the same font formatting. All text of the document
is stored in runs of text.

Hope this answers your query. Please let us know if you have any more queries.