1. Home
  2. Foxit Quick PDF Library
  3. Convert an image to PDF programmatically

Convert an image to PDF programmatically

Converting an image to PDF is simple using Foxit Quick PDF Library. The key functions involved are AddImageFromFile and DrawImage. The size of the image drawn onto the PDF is determined by the DPI value of the image (if present). The ImageHorizontalResolution and ImageVerticalResolution functions return the necessary DPI information.

PNG and GIF image files do not have a DPI setting, however, image types such as BMP, JPEG and TIFF all do. A DPI value of 72 is presumed if a DPI setting is not found in the image.

int id = DPL.AddImageFromFile("example_image.jpg", 0);
 
DPL.SelectImage(id);
 
int dpix = DPL.ImageHorizontalResolution();
int dpiy = DPL.ImageVerticalResolution();
 
if (dpix == 0) dpix = 72;
if (dpiy == 0) dpiy = 72;
 
double ImageWidthInPoints = (double)DPL.ImageWidth() / dpix * 72.0; // assumming dpi units
double ImageHeightInPoints = (double)DPL.ImageHeight() / dpiy * 72.0;
 
DPL.SetPageDimensions(ImageWidthInPoints, ImageHeightInPoints);
DPL.SetOrigin(1);
DPL.DrawImage(0, 0, ImageWidthInPoints, ImageHeightInPoints);
 
DPL.SaveToFile("converted_image.pdf");
Updated on April 10, 2017

Was this article helpful?

Related Articles

Ready to try Foxit PDF SDK?
Click the link below to download your trial
Free Trial

Leave a Comment