PDFiumVCL Docs

AddImage method

Component: TPdf  ·  Unit: PDFium
Insert an image from a file into the current PDF page. Internally uses TPicture.LoadFromFile, so any image format registered with the VCL or LCL graphics units (BMP, PNG via Vcl.Imaging.PngImage / LCL TPortableNetworkGraphic, JPG, etc.) is accepted. Coordinates are in PDF user space (origin at the page lower-left); Width and Height are the placement size in user units, not the source image pixel size.

Syntax

procedure AddImage(const FileName: string; X, Y, Width, Height: Double);

FileNamestring. Full path to the image file. Any format registered with the VCL/LCL graphics system (BMP, PNG, JPG, etc.) is accepted.

 

procedure AddImage(Bitmap: TBitmap; X, Y, Width, Height: Double);

BitmapTBitmap. A pre-loaded bitmap with PixelFormat = pf32bit. Must not be nil; raises EPdfError otherwise.

Description

AddImage places an image onto the current PDF page at the given position and size. Two overloads are available: one loads the image directly from a file path, and the other accepts a pre-built TBitmap object.

The file-based overload uses TPicture.LoadFromFile internally, so any image format registered with the VCL or LCL graphics system is supported — including BMP, PNG (via Vcl.Imaging.PngImage or the LCL equivalent), JPEG, and others. Simply ensure the relevant units are included in your project's uses clause before calling this method.

The bitmap overload accepts a TBitmap with PixelFormat = pf32bit. This is useful when the bitmap is already in memory (for example, from an offscreen rendering step) and avoids a round-trip through disk or TPicture. Passing a nil bitmap raises an EPdfError exception.

The X and Y parameters specify the lower-left corner of the image placement rectangle in PDF user units (origin at page lower-left, Y increases upward). Width and Height control the rendered size on the page; the source image is scaled to fit these dimensions regardless of its pixel resolution.

Example

// Place a PNG file at (72, 600), rendered 200 x 150 pt on the page
Pdf1.NewPage(595, 842);
Pdf1.AddImage('C:\Images\logo.png', 72, 600, 200, 150);

// Add a pre-built 32-bit bitmap
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.PixelFormat := pf32bit;
    Bmp.LoadFromFile('C:\Images\photo.bmp');
    Pdf1.AddImage(Bmp, 72, 400, 300, 200);
  finally
    Bmp.Free;
  end;
end;

See Also

AddPicture, AddJpegImage, AddText