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();
}
}
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);
}
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.
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).
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.
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 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.
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();
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.
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.