Foxit Quick PDF Library

Add images to PDF programmatically

Adding images to PDF files is simple using the PDF API available with Foxit 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.

// 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

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

// Select the image

// 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

// 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

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.

This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.

Updated on May 16, 2022

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: