Converting an image to PDF is simple using Debenu 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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");

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");