Add an image to a PDF using C#

PDF

An image can be added to a PDF file in two ways:

  • By drawing an image to a specified PDF page.
  • By adding an image as a separate PDF page.

Drawing an image to a PDF page

To draw an image as a part of a PDF page, follow the steps outlined below.

  1. Load the PDF file.
  2. Add the specified image as a resource by using one of the following methods:
  3. Set the origin of the coordinate system with the SetOrigin method. This method requires the PdfOrigin enumeration, which accepts the following values:
  • PdfOriginTopLeft
  • PdfOriginTopRight
  • PdfOriginBottomRight
  • PdfOriginBottomLeft
  1. Set the dimension units with the SetMeasurementUnit method, which uses the PdfMeasurementUnit enumeration as an argument. It allows the following values:
  • PdfMeasurementUnitCentimeter
  • PdfMeasurementUnitMillimeter
  • PdfMeasurementUnitInch
  • PdfMeasurementUnitPoint
  • PdfMeasurementUnitUndefined
  1. Select the PDF page you want to add the image to with the SelectPage method.
  2. Draw the image to the PDF page with the DrawImage method. This method requires the following parameters:
  • ImageResName — Image resource name.
  • DstX — Horizontal distance of the bottom-left corner of the PDF file, where the image is drawn.
  • DstY — Vertical distance of the bottom-left corner of the PDF file, where the image is drawn.
  • Width — Width of the drawn image resource.
  • Height — Height of the drawn image resource.

To add an image to the first and the last PDF page, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Add an image as a resource.
string image = gdpicturePDF.AddJpegImageFromFile(@"C:\temp\source.jpg");
// Set the origin of the coordinate system to the top-left corner.
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
// Set the measurement units to millimeters.
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);
var width = gdpicturePDF.GetPageWidth();
// Select the first page of the PDF file.
gdpicturePDF.SelectPage(1);
// Draw the image to the top-center of the PDF page.
gdpicturePDF.DrawImage(image, (width - 10) / 2, 15, 10, 10);
// Select the last page of the PDF file.
gdpicturePDF.SelectPage(gdpicturePDF.GetPageCount());
// Change the origin of the coordinate system to the bottom-left corner.
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);
// Draw the image to the bottom-center of the PDF page.
gdpicturePDF.DrawImage(image, (width - 10) / 2, 5, 10, 10);
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");

Adding an image as a separate PDF page

To add an image as a separate PDF page at the end of a file, follow the steps outlined below.

  1. Load the PDF file.

  2. Create a GdPicture image from the specified file using the CreateGdPictureImageFromFile method.

  3. Insert the image as a separate page at the end of the file with one of the following methods:

Both methods, AddImageFromGdPictureImage and AddImageFromBitmap, require the following parameters:

  • Either imageID or bitmap.
  • ImageMask — Indicates whether the inserted image is treated as an image mask (or stencil mask). The recommended default value is false. Applicable only for 1 bit per pixel images.
  • DrawImage — Set this parameter to true, which adds a new page, and the image is drawn on its whole surface.

To add an image as a separate page, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPictureImaging gdpictureImaging = new GdPictureImaging();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Add an image as a resource.
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Add the image to a new page.
gdpicturePDF.AddImageFromGdPictureImage(imageID, false, true);
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
gdpictureImaging.ReleaseGdPictureImage(imageID);