You can add images to a PDF document using the PDF editing layer, which is implemented via "PDE" objects (for example, HIG_PDE_CONTENT). This topic walks you through how to...
Prepare the Image
In order to add an image to a PDF page, a PDE version of the image has to be created first, using IG_PDE_image_create. The code snippet below demonstrates how to do this for a 24-bit RGB image, preparing the pixel data, color space, compression, image attributes and transformation matrix.
The following steps summarize the preparation of the input parameters, and then creating the PDE image accordingly.
- Get the pixel depth, assuming 24 bit per pixel.
- Get the image dimensions.
- Set the pixel access mode to new, so we get RGB values instead of BGR.
- Copy the image pixel data into a new buffer, using IG_DIB_raster_size_get to get the memory needed to hold one line of pixels of the original image, and IG_DIB_raster_get to get the actual pixel data line by line.
- Create the color space for the PDE image, using IG_PDE_colorspace_create.
- Set the compression parameters by means of a filter (AT_PDE_FILTERSPEC) and specifying the values using a PDF dictionary.
- Initialize the image attributes (AT_PDE_IMAGEATTRS) for type, width, height and depth.
- Initialize the transformation matrix (AT_PDF_FIXEDMATRIX), which can be used to position, scale, rotate, or skew the image on the page (see the PDF Reference for details).
- Create the PDE image by calling IG_PDE_image_create with the parameters prepared above.
The following code snippet demonstrates this:
If the image has an alpha channel, a separate 8-bit grayscale image has to be created for it using IG_PDE_image_create, and linked to the PDE image using IG_PDE_image_set_soft_mask.
Add the Image
Now that we have a way to create a PDE image, we can add it to the PDF. The following steps summarize how to gain access to the content of a PDF page and add the newly created PDE image.
- Create a multi-page document interface using IG_mpi_create.
- Create or open a PDF document interface using IG_PDF_doc_create or IG_mpi_file_open.
- Create a new page using IG_PDF_doc_create_new_page, if necessary.
- Get a handle to the PDF page using IG_mpi_page_get and IG_vector_data_get.
- Get a handle to the content using IG_PDF_page_get_content.
- Add the image to the content using IG_PDE_content_add_element.
- Set the content back into the page using IG_PDF_page_set_content.
- Save the changes using IG_mpi_file_save.
The following code snippet demonstrates this:
See also the section Watermarks and Annotations in a PDF.