Convert PDF to Different Image Formats in Python
Contents
[
Hide
]
Overview
This article explains how to convert PDF to different image formats using Python. It covers the following topics.
Python Convert PDF to Image
Python Convert PDF to PNG
- Import the AsposePdfPython module, which provides a Python wrapper for the Aspose.PDF library.
- Open a PDF document using the
document_open
function, which takes the file name as an argument and returns a Document object. - Get the pages of the document using the
document_get_pages
function, which takes a Document object as an argument and returns a PageCollection object. - Get the desired page of the document using the
page_collection_get_page
function, which takes a PageCollection object and an index as arguments and returns a Page object. - Create a PngDevice object using the
png_device_create
function, which takes no arguments. This object can convert PDF pages to PNG images with default parameters. - Save the desired page of the document as a PNG image using the
png_device_save_page_to_file
function, which takes a PngDevice object, a Page object and a file name as arguments. - Close the handles of the PngDevice and the Document objects using the close_handle function, which takes an object as an argument and releases its resources.
from AsposePdfPython import *
doc = document_open("blank_pdf_document.pdf")
pages = document_get_pages(doc)
page = page_collection_get_page(pages,1)
pngDevice = png_device_create()
png_device_save_page_to_file(pngDevice,page,"test.png")
Python Convert PDF to JPEG
- Open a PDF document using the
document_open
function, which takes the file name as an argument and returns a Document object. - Get the pages of the document using the
document_get_pages
function, which takes a Document object as an argument and returns a PageCollection object. - Get the desired page of the document using the
page_collection_get_page
function, which takes a PageCollection object and an index as arguments and returns a Page object. - Create a Resolution object using the
resolution_create
function, which takes the resolution value in dots per inch (DPI) as an argument. - Create a JpegDevice object using the
jpeg_device_create_from_width_height_resolution
function, which takes the width, height and resolution values as arguments. This object can convert PDF pages to JPEG images with the specified parameters. - Save the desired page of the document as a JPEG image using the
jpeg_device_save_page_to_file
function, which takes a JpegDevice object, a Page object and a file name as arguments. - Close the handles of the JpegDevice and the Document objects using the
close_handle
function, which takes an object as an argument and releases its resources.
from AsposePdfPython import *
doc = document_open("blank_pdf_document.pdf")
pages = document_get_pages(doc)
page = page_collection_get_page(pages,1)
res = resolution_create(300)
jpegDevice = jpeg_device_create_from_width_height_resolution(1239,1754,res)
jpeg_device_save_page_to_file(jpegDevice,page,"test.jpeg")