Loaded Image Extraction
已載入影像擷取讓你在用 LoadFromFile / LoadFromStream 開啟既有 PDF 之後,列舉其內嵌的影像 XObject 並把它們解碼為 TBitmap,便於預覽、轉碼或重新內嵌。HotPDF 會掃描
主要 API
function GetLoadedImageCount: Integer:回傳目前已載入文件中可列舉的影像物件數量function GetLoadedImageInfo(ImageIndex: Integer; out AInfo: THPDFLoadedImageInfo): Boolean:取得指定影像的中繼資訊(尺寸、色彩、過濾器等),不解碼像素;索引越界或無資訊時回傳Falsefunction ExtractLoadedImage(ImageIndex: Integer): TBitmap:把指定影像解碼為新建TBitmap(呼叫端負責釋放);來源過濾器不受支援或資料損毀時回傳Decodable:目前建置能否把該圖解碼為點陣圖
支援的來源
- 資料過濾器:
FlateDecode、LZWDecode、ASCIIHexDecode、ASCII85Decode、RunLengthDecode,以及多過濾器鏈(前置資料過濾器 + 末級影像過濾器) - 影像過濾器:
DCTDecode(JPEG)、JPXDecode(JPEG 2000)、JBIG2Decode、CCITTFaxDecode(G3/G4) - 色彩與位元深度:
解碼參數與安全性
DecodeParms與其縮寫鍵DP都能解析,無論值是字典還是過濾器參數陣列中的最後一個字典DeviceGray/DeviceRGB/DeviceCMYK/Indexed調色盤,以及非 8-bit 每分量(1/2/4-bit)點陣JBIG2Decode與JPXDecode預設走內建高效能解碼後端- TIFF Predictor 2 支援緊密排列的 1、2、4 位元取樣,以及 8 與 16 位元分量
- 預覽或匯出既有 PDF 內嵌的影像
- 把 JBIG2 / JPEG 2000 / CCITT 等影像轉碼為更通用的格式
- 檢查影像中繼資訊(尺寸、色彩空間、過濾器)而不必解碼全部像素
範例
var
Doc: THotPDF;
Info: THPDFLoadedImageInfo;
Bmp: TBitmap;
I: Integer;
begin
Doc := THotPDF.Create(nil);
try
Doc.LoadFromFile('input.pdf');
for I := 0 to Doc.GetLoadedImageCount - 1 do
if Doc.GetLoadedImageInfo(I, Info) and Info.Decodable then
begin
Bmp := Doc.ExtractLoadedImage(I);
try
if Assigned(Bmp) then
Bmp.SaveToFile(Format('image-%d.bmp', [I]));
finally
Bmp.Free;
end;
end;
finally
Doc.Free;
end;
end;