DCTDecode 資料嵌入目前的 PDF 頁面,按需讀取檔案,而不是整份載入記憶體function AddJpegImageFile(const FileName: string; X, Y, Width, Height: Double): Boolean;
| FileName | string。JPEG 檔案的完整路徑。檔案以唯讀開啟、拒絕共用寫入,並保持開啟直到文件被卸載 |
| X | Double。放置矩形的左緣(PDF 使用者單位) |
| Y | Double。放置矩形的下緣(PDF 使用者單位) |
| Width | Double。算繪寬度(PDF 使用者單位) |
| Height | Double。算繪高度(PDF 使用者單位) |
True。檔案不含有效 JPEG 資料或放置失敗時回傳 False,不引發例外。路徑無法存取時引發 EFCreateError,與任何 TFileStream 開啟相同AddJpegImageFile 是架在元件開啟並持有的檔案上的 AddJpegImageDeferred。它逐位元組保留來源 JPEG 的 DCTDecode 串流,所以既不解碼成點陣圖、也不重新編碼 — 不像 AddImage 會經過 TPicture 而失去原始壓縮 — 也不把整份檔案複製進文件,這點與 AddJpegImage 不同
插入時只讀檔案的前 8 KiB;其餘位元組在算繪時、以及文件存檔時再度從磁碟取入。因此插入一整個資料夾的大型掃描檔幾乎不佔記憶體,同一檔案也可以插入多次而不重複讀取
檔案控制代碼會一直持有到文件卸載為止,所以這段期間不得刪除、搬移或就地改寫該檔案。請在丟棄來源影像之前先 SaveAs,並且預期文件開啟期間任何試圖替換這些檔案的工具都會遇到檔案鎖定錯誤
座標位於 PDF 使用者空間,原點在頁面左下角;Width 與 Height 是使用者單位的放置尺寸,不是影像的像素大小
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;