Adding images to PDF files is simple using the PDF API available with Debenu Quick PDF Library.

The key functions involved when inserting an image into a PDF are the AddImageFromFile and DrawImage functions. 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Load the PDF that you want to add the image to
DPL.LoadFromFile("test.pdf", "");
 
// Call CompressImages prior to AddImageFromFile,
// if image data is uncompressed it will be
// compressed using flate, if already compressed
// no change will occur
DPL.CompressImages(1);
 
// Load the image that you want to add to the PDF
int id = DPL.AddImageFromFile("test_image.bmp", 0);
 
// Select the image
DPL.SelectImage(id);
 
// Get the horizontal and vertical resolution
// of the image if it exists
int dpix = DPL.ImageHorizontalResolution();
int dpiy = DPL.ImageVerticalResolution();
 
// If image does not have a DPI use 72 as default
if (dpix == 0) dpix = 72;
if (dpiy == 0) dpiy = 72;
 
// Calculate image width in points
// taking into account the image size in
// pixels and the DPI value
double ImageWidthInPoints = (double)DPL.ImageWidth() / dpix * 72.0; // assumming dpi units
double ImageHeightInPoints = (double)DPL.ImageHeight() / dpiy * 72.0;
 
// Call SetOrigin to specify that coorindates
// should be drawn from top left of the page
DPL.SetOrigin(1);
 
// Start drawing the top left corner of the image
// 100 points from the left of the page
// and 100 points down from the top of the page
DPL.DrawImage(100, 100, ImageWidthInPoints, ImageHeightInPoints);
 
// Save the updated PDF file to disk
DPL.SaveToFile("test_updated.pdf");

// Call CompressImages prior to AddImageFromFile,
// if image data is uncompressed it will be
// compressed using flate, if already compressed
// no change will occur
DPL.CompressImages(1);

// Load the image that you want to add to the PDF
int id = DPL.AddImageFromFile("test_image.bmp", 0);

// Select the image
DPL.SelectImage(id);

// Get the horizontal and vertical resolution
// of the image if it exists
int dpix = DPL.ImageHorizontalResolution();
int dpiy = DPL.ImageVerticalResolution();

// If image does not have a DPI use 72 as default
if (dpix == 0) dpix = 72;
if (dpiy == 0) dpiy = 72;

// Calculate image width in points
// taking into account the image size in
// pixels and the DPI value
double ImageWidthInPoints = (double)DPL.ImageWidth() / dpix * 72.0; // assumming dpi units
double ImageHeightInPoints = (double)DPL.ImageHeight() / dpiy * 72.0;

// Call SetOrigin to specify that coorindates
// should be drawn from top left of the page
DPL.SetOrigin(1);

// Start drawing the top left corner of the image
// 100 points from the left of the page
// and 100 points down from the top of the page
DPL.DrawImage(100, 100, ImageWidthInPoints, ImageHeightInPoints);

// Save the updated PDF file to disk
DPL.SaveToFile("test_updated.pdf");

The same image can be added to a document multiple times without adding any extra size to the file due to the fact that the one image object can be referenced multiple times within a PDF file.