PDFium Component Docs

AddImage method

Component: TPdf  ·  Unit: PDFium
Chèn một hình ảnh từ tệp vào trang PDF hiện tại. Bên trong sử dụng TPicture.LoadFromFile, nên mọi định dạng ảnh được đăng ký với các đơn vị đồ họa VCL hoặc LCL đều được chấp nhận (BMP, PNG qua Vcl.Imaging.PngImage / LCL TPortableNetworkGraphic, JPG, v.v.) Tọa độ nằm trong không gian người dùng PDF (gốc ở góc dưới bên trái của trang); Width và Height là kích thước đặt trong đơn vị người dùng, không phải kích thước pixel của ảnh nguồn

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