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 PDF 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 page_set 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 – horizontal_resolution, vertical_resolution, scale, pixel_format, as well as set up image color settings, using the following properties – image_brightness, image_color_mode, image_contrast, paper_color.

There are also properties that apply to a certain format, for example, jpeg_quality or tiff_compression.

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


FAQ

  1. Q: How can I convert only selected pages of a document to images?
    A: Use the ImageSaveOptions.page_set property to specify a collection of page numbers. For example, to convert pages 1 and 3 only:

    from aspose.words import Document, ImageSaveOptions, SaveFormat, PageSet
    
    doc = Document("input.docx")
    options = ImageSaveOptions(SaveFormat.Jpeg)
    options.page_set = PageSet(page=1)   # pages are zero-based index
    doc.save("output.jpeg", options)
    
  2. Q: How do I change the resolution of the generated image?
    A: Set horizontal_resolution and vertical_resolution (in DPI) on ImageSaveOptions. Higher DPI yields a larger, higher‑quality image.

    options = ImageSaveOptions(SaveFormat.Png)
    options.horizontal_resolution = 300
    options.vertical_resolution = 300
    doc.save("high_res.png", options)
    
  3. Q: How can I control JPEG quality when saving as JPEG?
    A: Adjust the jpeg_quality property (0‑100). A higher value gives better quality but larger file size.

    options = ImageSaveOptions(SaveFormat.Jpeg)
    options.jpeg_quality = 85   # typical high‑quality setting
    doc.save("photo.jpeg", options)
    
  4. Q: Is it possible to change the pixel format of the output image?
    A: Yes. Use the pixel_format property with values from the ImagePixelFormat enumeration (e.g., FORMAT_24BPP_RGB, FORMAT_32BPP_RGB).

    from aspose.words.saving import ImagePixelFormat
    
    options = ImageSaveOptions(SaveFormat.Tiff)
    options.pixel_format = ImagePixelFormat.FORMAT_24BPP_RGB
    doc.save("output.tiff", options)
    
  5. Q: Can I convert a PDF document directly to an image using Aspose.Words for Python via .NET?
    A: Absolutely. Load the PDF with Document, configure ImageSaveOptions, and save it in the desired image format.

    doc = Document("source.pdf")
    options = ImageSaveOptions(SaveFormat.Jpeg)
    options.page_set = PageSet(1)   # second page only
    doc.save("pdf_page2.jpeg", options)