#include // Use the main header file of Foxit Reader SDK #include "fpdfview.h" // Write a Foxit Device Independant Bitmap to a PPM file void WritePPM(FPDF_BITMAP pBitmap, char* filename) { FILE* file = fopen(filename, "wb"); if (file == NULL) return; // Write a simple file header int width = FPDFBitmap_GetWidth(pBitmap); int height = FPDFBitmap_GetHeight(pBitmap); fprintf(file, "P6 %d %d 255\n", width, height); // Write pixel color values one by one. for (int row = 0; row < height; row ++) { char* scan_line = (char*)FPDFBitmap_GetBuffer(pBitmap) + row * width * 4; for (int col = 0; col < width; col ++) // NOTE: Foxit DIB uses BGRA byte order, PPM uses RGB, // so we need to swap here. fprintf(file, "%c%c%c", scan_line[col*4+2], scan_line[col*4+1], scan_line[col*4]); } // Finished fclose(file); } int main(int argc, char** argv) { if (argc == 1) { printf("Please specify a file name.\n"); return 0; } // Load the PDF document FPDF_DOCUMENT doc = FPDF_LoadDocument(argv[1], NULL); if (doc == NULL) { printf("error opening file\n"); return 0; } // Load first page FPDF_PAGE page = FPDF_LoadPage(doc, 0); // Create a bitmap device for displaying the page int width = 480, height = 640; // width and height of the bitmap device FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); // we don't use alpha channel here // Fill the whole bitmap with white background FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 255, 255, 255, 255); // Now we can display the page into the bitmap FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); // Write the result bitmap to a simple PPM bitmap file WritePPM(bitmap, "test.ppm"); // Clean up FPDFBitmap_Destroy(bitmap); FPDF_ClosePage(page); FPDF_CloseDocument(doc); return 0; }