Render all Pages of Word File into a Big Bitmap Graphics Objects & Convert to JPEG Image (C# .NET)

How to save ALL document pages as images?

I use the following code:

Document doc = new Document(@"D:\DEV\tmp\readme_rus.doc");
doc.Save(@"d:\dev\tmp\sign.png", SaveFormat.Png);

But only the first page is saved (Document has 2 pages)

Hi Vitaliy,

Thanks for your inquiry.

In order to save all pages in Microsoft Word document to separate PNG image files, please use the following code snippet:

Document document = new Document(@“C:\test\In.docx”);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);
options.PageCount = 1;
// Save each page of the document as Png.
for (int i = 0; i < document.PageCount; i++)
{
    options.PageIndex = i;
    document.Save(string.Format(@“C:\test\out_{ 0}.png”, i), options);
}

I hope, this helps.

Best regards,

can i save multiple document in one image?

Hi Qin,

Thanks for your inquiry. Sure, you can first join multiple Word documents into a single document and then render the final document to multi-page Tiff format as follows.

Document doc = new Document(MyDir + "Final.doc");
doc.Save(MyDir + "Out.tiff");

Please let me know if I can be of any further assistance.

Best regards,

thank you for your help.

but i want convert a document with three page into one image.

now i join the images on Bitmap,but there is some better idea with aspose?

Hi Qin,

Thanks for your inquiry. I think, you can achieve this using the following code snippet:

Document doc = new Document(@"C:\Temp\input.docx");
List<Image> images = new List<Image>();
int bigImageWidth = 0;
int bigImageHeight = 0;

ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Jpeg);
options.PageCount = 1;

for (int i = 0; i < doc.PageCount; i++)
{
    options.PageIndex = i;

    using (MemoryStream imgStream = new MemoryStream())
    {
        doc.Save(imgStream, options);
        imgStream.Position = 0;

        using (Bitmap bmp = new Bitmap(imgStream))
        {
            images.Add(new Bitmap(imgStream));

            bigImageWidth = (bigImageWidth < bmp.Width) ? bmp.Width : bigImageWidth;
            bigImageHeight += bmp.Height;
        }

        imgStream.Close();
    }
}

using (Bitmap bigImage = new Bitmap(bigImageWidth, bigImageHeight))
{
    using (Graphics gfx = Graphics.FromImage(bigImage))
    {
        int x = 0;
        int y = 0;

        foreach (Image img in images)
        {
            y += img.Height;
            gfx.DrawImage(img, new Point(x, y));
        }

        gfx.Save();
    }

    bigImage.Save(@"C:\Temp\out.jpg", ImageFormat.Jpeg);
}

Best regards,

yes,it works,thank you so much

A post was split to a new topic: Avoid Empty Images during Word to HTML Conversion using C# .NET