Loaded Image Extraction
Loaded image extraction lets applications enumerate image XObjects after LoadFromFile or LoadFromStream opens an existing PDF, inspect their metadata, and decode supported images into TBitmap instances
Main APIs
GetLoadedImageCountreturns the number of enumerable image objects in the loaded documentGetLoadedImageInfofillsTHPDFLoadedImageInfowith object number, page index, size, bit depth, color space, filter list, and decodability metadata without decoding pixelsExtractLoadedImagedecodes the selected image to a newTBitmap; the caller owns the returned bitmapGetLoadedImageCountPerPagescopes enumeration to one loaded page
Supported sources
- Data filters include
FlateDecode,LZWDecode,ASCIIHexDecode,ASCII85Decode,RunLengthDecode, and multi-filter chains that end in an image filter - Image filters include
DCTDecode,JPXDecode,JBIG2Decode, andCCITTFaxDecode - Raster paths include DeviceRGB, DeviceGray, Indexed, DeviceCMYK, and low-bit-depth grayscale or indexed images
Decode parameters and safety
DecodeParmsand its abbreviatedDPkey are resolved whether the value is a dictionary or the final dictionary in a filter-parameter arrayPredictor,Colors,BitsPerComponent, andColumnsare applied after Flate or LZW decompression- LZW
EarlyChange=0andEarlyChange=1are both honoured - TIFF Predictor 2 supports packed 1, 2, and 4-bit samples as well as 8 and 16-bit components
- PNG predictors require a valid filter tag for every complete row
- Raster dimensions and expected byte counts are checked for overflow before allocation or decoding
- Malformed compressed data is reported as a decode failure and is never treated as uncompressed pixels
Example
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;