PDFium Delphi Component Docs

AddJpegImageFile method

Component: TPdf  ·  Unit: PDFium
Embed a JPEG file on the current PDF page as its original DCTDecode data, reading the file on demand rather than loading it into memory

Syntax

function AddJpegImageFile(const FileName: string; X, Y, Width, Height: Double): Boolean;

FileNamestring. Full path to a JPEG file. The file is opened read-only with write sharing denied, and stays open until the document is unloaded
XDouble. Left edge of the placement rectangle in PDF user units
YDouble. Bottom edge of the placement rectangle in PDF user units
WidthDouble. Rendered width in PDF user units
HeightDouble. Rendered height in PDF user units

Return Value

Returns True if the file was opened, its JPEG header accepted and the image object placed on the page. Returns False if the file does not contain valid JPEG data or if the placement fails, without raising an exception. An inaccessible path raises EFCreateError, as any TFileStream open does

Description

AddJpegImageFile is AddJpegImageDeferred over a file the component opens and owns. It keeps the source JPEG's DCTDecode stream byte for byte, so there is no decode to a bitmap and no re-encode — unlike AddImage, which goes through TPicture and loses the original compression — and no full-file copy into the document, unlike AddJpegImage

Only the first 8 KiB of the file are read at insert time; the rest is pulled from disk at render time and again when the document is saved. Inserting a folder of large scans therefore costs almost nothing in memory, and the same file can be inserted several times without repeating the read

Because the file handle is held until the document is unloaded, the file must not be deleted, moved or rewritten in place in the meantime. Call SaveAs before discarding the source images, and expect a locked-file error from any tool that tries to replace them whilst the document is open

Coordinates are in PDF user space, with the origin at the page lower-left; Width and Height are the placement size in user units, not the image's pixel size

Example

var
  Name: string;
  Y: Double;
begin
  Pdf1.AddPage(1, 595, 842);
  Y := 700;
  // stack four scans down the page without loading any of them into memory
  for Name in TDirectory.GetFiles('C:\Scans', '*.jpg') do
  begin
    if not Pdf1.AddJpegImageFile(Name, 72, Y, 451, 160) then
      ShowMessage('Not a valid JPEG: ' + Name);
    Y := Y - 170;
  end;
  Pdf1.SaveAs('C:\Out\scans.pdf');
end;

See Also

AddJpegImageDeferred, AddJpegImage, AddImage, TPdfView.AddJpegImageFile