This section illustrates how to convert a PDF to a raster image.
Refer to the PDF to Image Sample for complete sample code that illustrates how to use this capability.
- Load all pages from a PDF document from disk to a multi-page vector document HMIGEAR:
C and C++ |
Copy Code
|
LPSTR inputPath = "MyFilePath.pdf";
HMIGEAR document = 0;
IG_mpi_create(&document, 0);
IG_mpi_file_open(inputPath, document, IG_FORMAT_PDF, IG_MP_OPENMODE_READONLY);
|
- Rasterize the first page of the loaded PDF document:
C and C++ |
Copy Code
|
const UINT firstPage = 0;
HIGEAR page = 0;
HIGEAR rasterPage = 0;
// Get a handle to the first page.
IG_mpi_page_get(document, firstPage, &page);
// Rasterize the loaded page.
IG_vector_data_to_dib(page, &rasterPage);
|
- To (optionally) control the resolution that the PDF rasterizes to, use the filters that control resolution. The following code snippet adds in this capability to the previous code snippet:
C and C++ |
Copy Code
|
const UINT firstPage = 0;
HIGEAR page = 0;
HIGEAR rasterPage = 0;
AT_UINT resolution = 100;
// Get a handle to the first page.
IG_mpi_page_get(document, firstPage, &page);
// Set the resolution of the PDF filter to 100 dots per inch.
// This specifies the resolution used in the conversion to a raster image.
IG_fltr_ctrl_set(IG_FORMAT_PDF, "RESOLUTION_X", (LPVOID)resolution, sizeof(resolution));
IG_fltr_ctrl_set(IG_FORMAT_PDF, "RESOLUTION_Y", (LPVOID)resolution, sizeof(resolution));
// Rasterize the loaded page.
IG_vector_data_to_dib(page, &rasterPage);
|
There are additional filters that you can use to control the rasterization process, such as the bit depth and smoothing. (Refer to PDF in the File Formats Reference for more details).
- Once the page has been rasterized, it's a simple matter of saving the file to a BMP, PNG, JPEG or other raster file format. The file format can be explicitly set with the FormatType parameter, or the file format can be implicitly determined by ImageGear from the file's extension. The following code snippet saves the page to a BMP, then a PNG, and finally a JPEG:
C and C++ |
Copy Code
|
// Save the loaded page as a raster image.
IG_fltr_save_file(rasterPage, "PdfToRaster.bmp", IG_SAVE_BMP_UNCOMP, firstPage, TRUE);
// If the FormatType is set to IG_SAVE_UNKNOWN, ImageGear will check the file's
// extension and save the image accordingly.
IG_fltr_save_file(rasterPage, "PdfToRaster.png", IG_SAVE_UNKNOWN, firstPage, TRUE);
IG_fltr_save_file(rasterPage, "PdfToRaster.jpg", IG_SAVE_UNKNOWN, firstPage, TRUE);
|