PDFium Delphi Component Docs

AddJpegImageDeferred method

Component: TPdf  ·  Unit: PDFium
Embed a JPEG stream on the current PDF page as its original DCTDecode data, pulling the bytes on demand rather than copying them into the document up front

Syntax

function AddJpegImageDeferred(JpegImage: TStream; X, Y, Width, Height: Double; OwnsJpegImage: Boolean = False): Boolean;

JpegImageTStream. A stream containing raw JPEG-compressed data, positioned at the start of the JPEG data when this method is called
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
OwnsJpegImageBoolean. When True the component closes the stream once the document is unloaded; when False (the default) the caller keeps ownership and must keep the stream open and readable for as long as the document stays loaded

Return Value

Returns True if the JPEG header was accepted and the image object was placed on the page. Returns False if the stream does not contain valid JPEG data or if the placement fails, without raising an exception

Description

AddJpegImageDeferred produces the same output as AddJpegImage — a byte-identical /DCTDecode image XObject — but gets there via the non-inline insertion path, so the JPEG is never buffered whole inside the document. Only the first 8 KiB are read at insert time, which is what PDFium needs to parse the JPEG header and build the image dictionary; the remaining bytes are pulled through the stream on demand, at render time and again when the document is saved

This is the variant to prefer when inserting many or large JPEGs, or when the source is a network or database stream you would rather not materialise in memory. A 10 MB scan inserted this way costs an 8 KiB read rather than a 10 MB copy, and the peak working set stays flat however many images are queued

The trade-off is lifetime. Because PDFium keeps reading through the stream for as long as the document is loaded, the stream must stay open, seekable and unmodified until the document is closed. Pass OwnsJpegImage = True to have the component close it for you at unload; pass False only when the caller can guarantee the stream outlives the document, and note that a stream shared with other readers must not be repositioned or truncated underneath the component

If the JPEG is read from a file the component can open itself, use AddJpegImageFile instead: it opens the file, transfers ownership to the component and closes it at unload, which removes the lifetime question entirely

The bridge carrying the bytes is serialised internally, so PDFium can call back into the stream from a render or save on another thread without corrupting the read. Inserting the image is still a document edit and follows the usual TPdf rule: do not insert whilst another thread mutates the same document

Example

var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create('C:\Images\photo.jpg', fmOpenRead or fmShareDenyNone);
  Pdf1.AddPage(1, 595, 842);
  // ownership moves to Pdf1: the stream is closed when the document is unloaded
  if not Pdf1.AddJpegImageDeferred(Stream, 72, 400, 300, 225, True) then
    Stream.Free;
  Pdf1.SaveAs('C:\Out\with-photo.pdf');
end;

See Also

AddJpegImage, AddJpegImageFile, AddImage, TPdfView.AddJpegImageDeferred