Convert a Document to an Image

Sometimes it is required to get an image instead of documents in other formats, such as DOCX or PDF. For example, you need to add a preview of any document page to your website or application, or create a “scan” of a document to send an invoice. This is when you may need to convert a document in any supported load format to an image, again, in any supported save format.

Convert to Image Format

As with all conversion examples already described, you need to create a new document or load an existing one in any supported format, make the necessary changes, and save it in any available image format, for example, JPEG, PNG, or BMP.

The following code example shows how to convert DOCX to JPEG:

Specify Save Options when Converting to an Image

Aspose.Words provides you with the ImageSaveOptions class, which gives more control over how documents are saved in various image formats. Some properties of this class inherit or overload properties of base classes such as FixedPageSaveOptions or SaveOptions, but there are also options specific to saving images.

It is possible to specify the pages to be converted to image format using the PageSet property. For example, it can be applied if you only need a preview for the first or for a definite page.

It is also possible to control the output image quality and pixel format using the following properties – HorizontalResolution, VerticalResolution, Resolution, Scale, PixelFormat, as well as set up image color settings, using the following properties – ImageBrightness, ImageColorMode, ImageContrast, PaperColor.

There are also properties that apply to a certain format, for example, JpegQuality or TiffCompression.

The following code example shows how to create a preview of the first document page with applying some additional settings:


FAQ

  1. Q: How do I convert a Word document to a JPEG image using Aspose.Words for C++?
    A: Load the document with System::SharedPtr<Aspose::Words::Document> doc = System::MakeObject<Aspose::Words::Document>(u"input.docx");, create an ImageSaveOptions object, set SaveFormat to SaveFormat::Jpeg, and call doc->Save(u"output.jpg", saveOptions);.

  2. Q: Can I convert only selected pages of a document to images?
    A: Yes. Use the PageSet property of ImageSaveOptions. For example, saveOptions->set_PageSet(System::MakeObject<Aspose::Words::Saving::PageSet>(1, 1)); converts only page 1.

  3. Q: Which options let me control the resolution and quality of the saved image?
    A: HorizontalResolution, VerticalResolution, and Resolution control DPI. JpegQuality (0‑100) adjusts JPEG compression, while TiffCompression sets TIFF compression type. Adjust these on the ImageSaveOptions instance before saving.

  4. Q: How can I change the pixel format or color mode of the output image?
    A: Set PixelFormat (e.g., PixelFormat::Format24bppRgb) and ImageColorMode (e.g., ImageColorMode::Grayscale) on the ImageSaveOptions object to modify the image’s color representation.

  5. Q: How do I generate separate image files for every page in a document?
    A: Loop through the page count, update PageSet for each page, and call Save with a distinct file name, e.g.:

    System::SharedPtr<Aspose::Words::Document> doc = System::MakeObject<Aspose::Words::Document>(u"input.docx");
    System::SharedPtr<Aspose::Words::Saving::ImageSaveOptions> options = System::MakeObject<Aspose::Words::Saving::ImageSaveOptions>(SaveFormat::Png);
    for (int i = 0; i < doc->get_PageCount(); ++i)
    {
        options->set_PageSet(System::MakeObject<Aspose::Words::Saving::PageSet>(i + 1, 1));
        System::String fileName = System::String::Format(u"page_{0}.png", i + 1);
        doc->Save(fileName, options);
    }
    

    This creates one image per page with the specified format and options.