DCTDecode data, pulling the bytes on demand instead of copying them into the document up frontfunction AddJpegImageDeferred(JpegImage: TStream; X, Y, Width, Height: Double; OwnsJpegImage: Boolean = False): Boolean;
| JpegImage | TStream. A stream containing raw JPEG-compressed data, positioned at the start of the JPEG data when this method is called |
| X | Double. Left edge of the placement rectangle in PDF user units |
| Y | Double. Bottom edge of the placement rectangle in PDF user units |
| Width | Double. Rendered width in PDF user units |
| Height | Double. Rendered height in PDF user units |
| OwnsJpegImage | Boolean. 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 |
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 exceptionAddJpegImageDeferred produces the same output as AddJpegImage — a byte-identical /DCTDecode image XObject — but reaches it through 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 that should not be materialised 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 no matter how many images are queued
The trade-off is lifetime. Because PDFium keeps reading through the stream for the whole time 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 that carries 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 while another thread mutates the same document
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;