Creating a multi-page Tiff from a document

Hello, I want to create a multi-page tiff from a word (excel, powerpoint etc) document by given start and end page number. My aim is to create thumbnails.


I can create one page thumnail but as i see, sample code does not support multi-page tiff from a document.

              var doc = new Document(thumbnail.OriginalFileContent.ConvertToMemoryStream());
// Render Word document to an image stream
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Tiff);
if (thumbnail.AllPages)
{
options.PageIndex = 0;
options.PageCount = doc.PageCount;
}
else
{
options.PageIndex = thumbnail.StartPage;
options.PageCount = thumbnail.EndPage- thumbnail.StartPage;
}
          options.TiffCompression= <span style="color:#2b91af;">TiffCompression</span>.Ccitt4;<span style="color:green;">// (TiffCompression.CCITT_4);</span>
          options.Resolution=160;<span style="color:green;">// .setResolution(160);</span>
          
          options.ColorMode = thumbnail.IsColored? <span style="color:#2b91af;">ColorMode</span>.Normal : <span style="color:#2b91af;">ColorMode</span>.Grayscale;

          <span style="color:#2b91af;">MemoryStream</span> imgStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>();
          doc.Save(imgStream, options);</pre><pre style="font-family: Consolas; font-size: 13px; background: white;"><br></pre><pre style="font-family: Consolas; font-size: 13px; background: white;">Until here imgstream object holds multipage when i save as a tiff but when i want to resize it</pre><pre style="font-family: Consolas; font-size: 13px; background: white;"><br></pre><pre style="font-family: Consolas; font-size: 13px; background: white;"><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><span style="color:#2b91af;">              Document</span> temp = <span style="color:blue;">new</span> <span style="color:#2b91af;">Document</span>();
          <span style="color:#2b91af;">DocumentBuilder</span> builder = <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentBuilder</span>(temp);
          <span style="color:#2b91af;">Shape</span> img = builder.InsertImage(imgStream);
          
          <span style="color:green;">// Resize the image as per your needs</span>
          img.Width = thumbnail.Width;
          img.Height = thumbnail.Height;
          
          <span style="color:green;">// Save the individual image to disk using ShapeRenderer class</span>
          <span style="color:#2b91af;">ShapeRenderer</span> renderer = img.GetShapeRenderer();
          
          <span style="color:blue;">using</span> (<span style="color:blue;">var</span> outStream = <span style="color:blue;">new</span> <span style="color:#2b91af;">MemoryStream</span>())
          {
              
              renderer.Save(outStream, <span style="color:blue;">new</span> <span style="color:#2b91af;">ImageSaveOptions</span>(<span style="color:#2b91af;">SaveFormat</span>.Tiff));
              result.Thumbnail = outStream.ToArray();
              result.Result = <span style="color:#2b91af;">Result</span>.Successful;
              result.Message = <span style="color:#a31515;">$</span><span style="color:#a31515;">"A thumbnail has been successfully created"</span>;
          }</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><br></pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">img Shape object holds only one page from the stream and it gives me the first page. </pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;">Well how i can solve this problem?</pre><pre style="font-family: Consolas; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><br></pre></pre></div>

As I understand from this reply


Doc to Tiff conversion

my code was supposed to worked. Because if you give pageindex and pagecount, this shape object should create a multi-page tiff but it seems it only takes first page of the pages. Maybe there is a bug or i am missing something…

Maybe there should be an “InsertImages” method in documentbuilder and it returns shape list and shaperenderer must render the shapelist as a one file.

This is very important part for my project and i am stucked here

Hi Ertan,

Thanks for your inquiry. You may be able to resize multi-page TIFF images by using Aspose.Imaging for .NET. But, you need to manipulate TIFF images differently i.e. Frame by Frame. Please refer to the following article:

Manipulating TIFF Images

Best regards,

Thank you for your reply Awais,

I understand your point. But let me ask several thing regarding this.

1- Is it the best way for performance?

2- If you look at my code there is imgStream object that is actually a tiff stream (because when i save it, i can get a multi-page tiff file (without resizing).
Well I dont want to save this stream as a tiff file and read this file for Aspose.Imaging. I mean i want to use this stream directly like

var multiImage = Aspose.Imaging.Image.Load(imgStream);

But i get this exception

- Aspose.Imaging.Image.Load(imgStream); 'Aspose.Imaging.Image.Load(imgStream)' threw an exception of type 'Aspose.Imaging.CoreExceptions.ImageLoadException' Aspose.Imaging.Image {Aspose.Imaging.CoreExceptions.ImageLoadException}

+ InnerException {"Cannot open an image. The image file format may be not supported at the moment."} System.Exception {Aspose.Imaging.CoreExceptions.ImageLoadException}


StackTrace " at Aspose.Imaging.Image.\u0002(StreamContainer \u0002, LoadOptions \u0003)\r\n at Aspose.Imaging.Image.Load(Stream stream)" string


3- Let assume I use my stream object as Tiff image and I reach the frames, what about resizing. Because as I see, height and width properties of the frame object is only readable (no setter). so that how to resize the frame?

Considering my requirements, could you provide me a sample code?

I have to write wrappers for all aspose modules for creating multi-page tiff thumbnail. I've done for pdf and excel.. The word is one of the most important module. I can solve this problem but i do not want to do that way due the performance issue.

Thank you
Hi Ertan,

Thank you for your inquiry.

You can use System.IO.Stream objects to load an existing Image file using Aspose.Imaging. Following is the code demonstration. For details, please visit the links:


CODE:

//Create an instance of FileStream
using(System.IO.FileStream stream = new System.IO.FileStream(@"sample.bmp",System.IO.FileMode.Open))
{
//Create an instance of Image class and load an existing file through FileStream object by calling Load method
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(stream))
{
//do some image processing.
}
}

Please try the above at your end. Feel free to contact us in case of any query or comments.

You wrote me the same answer in my other topic which has the same problem…


Please take a ref from here

How to use Bitmap object in Imaging from GetThumbnail Function without saving - #5 by iwagbe - Free Support Forum - aspose.com

This problem is a bit different then the problem from the link.


PS: UPDATE

In this case, we have a stream object which contains a multipage tiff. But the problem in the link, it is only one image (not contains any frame). I do not know if there is difference to solving the problem. But I wanted to mention about it

Hi Ertan,

Please refer to the post for detailed answer of your inquiry.


The issues you have found earlier (filed as ) have been fixed in this Aspose.Words for JasperReports 18.3 update.