We're sorry Aspose doesn't work properply without JavaScript enabled.

# Creating an image for multiple pages in a document

Hello,

I was wondering if it is possible to create images for multiple pages in a document. Say for instance I have a word document with 5 pages and I want to make an image of the first two pages and return it is this possible? the current code I have is this
where I have the startpage = 1 and numberOfPages = 2

public byte[] CreatePreview(byte[] file, int resolution, int startPage, int numberOfPages)
{
var doc = new Document(new MemoryStream(file));

var options = new ImageSaveOptions(SaveFormat.Png)
{
PageCount = numberOfPages,
PageIndex = startPage,
Resolution = resolution
};

using (var ms = new MemoryStream())
{
doc.Save(ms, options);
return ms.GetBuffer();
}
}


Hi

Thanks for your request. You can use code like the following to save each page of the document as an image:

// Open document.
Document doc = new Document(@"Test001\in.docx");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);
options.PageCount = 1;
// Save each page of the document as PNG.
for (int i = 0; i < doc.PageCount; i++)
{
options.PageIndex = i;
doc.Save(string.Format(@"Test001\out_{0}.png", i), options);
}


Hope this helps.

Best regards,

so I can leave my return statement as is, and it will return an array of bytes that are images?

Hi there,

No, at the moment only the first page will be saved to your byte array as PNG does not support multiple pages. There are two different techniques that you can use in your situation, please choose the one which is appropriate for you.

1. Use your current code but change the save format to TIFF. This format will allow you to save multiple pages into one file so your current code will perform correctly (as long as where it’s going knows how to handle it).
2. Iterate through each desired page as shown in the code provided by Alexey and save each image to a MemoryStream then to a byte array. Then return all of these byte arrays together e.g as Byte[][].

Also note GetBuffer is not a reliable method to use - it can often return extra junk data which could possibly corrupt whatever the image is given to. You should use ToArray instead.

If we can help with anything else, please feel free to ask.

Thanks,

Hi,

I cannot seem to save multiple pages as in image. I’ve tried this code with all formats (png, jpeg, tiff). Am I missing something?

MemoryStream templateStream = new MemoryStream();

Document asposeDocument = new Document(documentTemplate.FileStream);
if (!string.IsNullOrEmpty(tokenToHighlight))
asposeDocument.Range.Replace(new Regex(tokenToHighlight), new HighlightDocumentTokenHandler(), true);
ImageSaveOptions options = new ImageSaveOptions(saveFormat);
options.PrettyFormat = true;
options.PageCount = 1;

for (int i = 0; i < asposeDocument.PageCount; i++)
{
options.PageIndex = i;
asposeDocument.Save(templateStream, options);
}

return templateStream;


Hi

// Open document.
Document doc = new Document("C:\\Temp\\in.doc");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Jpeg);
options.PageCount = 1;
for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
{
string outputFileName = string.Format("{0}\\{1}_{2}.Jpeg", "C:\\Temp", "Test", pageIndex + 1);
options.PageIndex = pageIndex;
doc.Save(outputFileName, options);
}


Best regards,

Hi,

doc.Save(string outputFile, Options options) seems to be saving all pages in separate files with following code.

Document doc = new Document(@"C:\RocketMatter\branches\doclibrary\scripts\Payment_Request_Template_Invalid_2.docx");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Jpeg);
options.PageCount = 1;
for (int i = 0; i < doc.PageCount; i++)
{
string outputFile = string.Format(@"C:\RocketMatter\branches\doclibrary\scripts\Payment_Request_Template_Invalid_2_Page{0}.jpeg", i + 1);
options.PageIndex = i;
doc.Save(outputFile, options);
}


We need to ouput in stream.So I used this code and it does seem to loop through the pages, but when I write the output to a file, it only saves the first page.Here is the code I used,

Document doc = new Document(@"C:\RocketMatter\branches\doclibrary\scripts\Payment_Request_Template_Invalid_2.docx");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Jpeg);
options.PageCount = 1;
using (FileStream outputStream = File.Create(@"C:\RocketMatter\branches\doclibrary\scripts\Payment_Request_Template_Invalid_2.jpeg"))
{
using (MemoryStream processedStream = new MemoryStream())
{
for (int i = 0; i < doc.PageCount; i++)
{
options.PageIndex = i;
doc.Save(processedStream, options);
}

byte[] fileBytes = processedStream.GetBuffer();
outputStream.Write(fileBytes, 0, fileBytes.Length);
}
}


Thanks,
Gaurav

Hello

Thanks for your request. Could you please clarify, you would like to return an image that contains pictures of all pages of the document? If so, there is no direct way to achieve this. You can only return image that represents only one page. If you need to combine them some how, you will have to write you own logic for this.

Best regards,

Thanks for replying Andrey. Yes we need to save the output of all pages as a single page image. Is that possible?

Thanks,
Gaurav

Basically we need to stream the output to the browser. It looks like when you use Document.Save(Stream stream, SaveOptions saveOptions) overloaded method, it doesn’t respect Options.PageIndex.

Document doc = new Document(@"C:\RocketMatter\branches\doclibrary\scripts\Payment_Request_Template_Invalid_2.docx");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Jpeg);
options.PageCount = 1;
context.Response.CacheControl = "no-cache";
context.Response.Expires = 0;
context.Response.ClearContent();
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
for (int i = 0; i < doc.PageCount; i++)
{
options.PageIndex = i;
doc.Save(context.Response.OutputStream, options);
}
context.Response.End();


Hello

Thank you for additional information. I’m afraid there is no direct way to achieve this using Aspose.Words. In this case you can try using System.Drawing.

Best regards,

I ran the sample code from this article and it doesn’t save all pages in one tiff image.

https://docs.aspose.com/words/net/convert-a-document-to-an-image/

I use 2010.09.10 version of Aspose.Words.dll. Could this be a bug?

Attaching the output files.

Hi

Thank you for additional information. As I can see the output tiff contains two pages, as expected. Could you please also attach your expected output?

Best regards,

Source document is a two page word doc, so shouldn’t the output be a single tiff containing both pages as mentioned in this article?

https://docs.aspose.com/words/net/convert-a-document-to-an-image/

Hi there,

The image in the documentation demonstrates how both frames in the TIFF image look, only one page is shown at a time in the actual TIFF. You can test this out for yourself by using a free image viewer such as Infran Viewer and opening the output TIFF from this sample.

By the sounds it, you actually want to render all pages onto a single image instead? If this is the case you may want to look into the Printing Multiple Pages on a One sheet article here: https://docs.aspose.com/words/net/print-a-document-programmatically-or-using-dialogs/

Thanks,

Thanks for replying, Adam. That article was very helpful. I was able to render the pages into one image using following code.

Document doc = new Document(@"C:\RocketMatter\branches\doclibrary\scripts\Payment_Request_Template_Invalid_2.docx");
using (FileStream outputStream = File.Create(@"C:\RocketMatter\branches\doclibrary\scripts\Payment_Request_Template_Invalid_2.tiff"))
{
PageInfo pageInfo = doc.GetPageInfo(0);
Size pageSize = pageInfo.GetSizeInPixels(1, 96);
float height = pageSize.Height;
float width = pageSize.Width;
float x = 0;
float y = 0;
Bitmap bitmap = new Bitmap((int)width, (int)(height * doc.PageCount));
Graphics graphics = Graphics.FromImage(bitmap);
for (int i = 0; i < doc.PageCount; i++)
{
doc.RenderToScale(i, graphics, x, y, 1);
y += height;
}
bitmap.Save(outputStream, ImageFormat.Tiff);
}


Thanks,
Gaurav

Hi Gaurav,

It’s great you were able to implement what you were looking for. Please feel free to ask us for help anytime you need it.

Thanks,