Sometimes it’s necessary to extract images from PDF files and save them to disk. When this happens you’ll most likely want to save the image data back into the image format that it was originally in before it was added to the PDF. This can be tricky at times because some image formats such as PNG are not native to the PDF format so they are manipulated and stored in a modified form in the PDF. Foxit Quick PDF Library does its best to recognize the appropriate image type and save the embedded image as that image type.
Here is some sample C# code which demonstrates how to get a list of all images in a PDF and to save each image to disk using the appropriate image type.
string filename = ""; // Load your doc int nd = DPL.LoadFromFile("images.pdf", ""); // Get page count from doc int pC = DPL.PageCount(); // Iterate through each page for (int i = 1; i <= pC; i++) { // Select current page DPL.SelectPage(i); // Get list of images on the page int il = DPL.GetPageImageList(0); // Count number of images in the list int ic = DPL.GetImageListCount(il); for (int k = 1; k <= ic; k++) { // Iterate through each image and get the // image type and image ID int it = DPL.GetImageListItemIntProperty(il, k, 400); int gid = DPL.GetImageListItemIntProperty(il, k, 405); // Choose the approrpriate file extenion based on // the returned image type switch (it) { case 1: filename = "image-" + Convert.ToString(gid) + "-" + k + ".jpg"; break; case 2: filename = "image-" + Convert.ToString(gid) + "-" + k + ".bmp"; break; case 3: filename = "image-" + Convert.ToString(gid) + "-" + k + ".tif"; break; case 4: filename = "image-" + Convert.ToString(gid) + "-" + k + ".png"; break; } // Save the selected image to disk DPL.SaveImageListItemDataToFile(il, k, 0, filename); } } |