DCTDecode data, reading the file on demand instead of loading it into memoryfunction AddJpegImageFile(const FileName: string; X, Y, Width, Height: Double): Boolean;
| FileName | string. Full path to a JPEG file. The file is opened read-only with write sharing denied, and stays open until the document is unloaded |
| 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 |
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 doesAddJpegImageFile 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 while 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
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;