Loaded Image Extraction
Once LoadFromFile or LoadFromStream has opened an existing PDF, loaded image extraction lets your application enumerate the image XObjects embedded in it, inspect their metadata, and decode the supported ones into TBitmap instances ready for previewing, transcoding or re-embedding
Main APIs
GetLoadedImageCountreturns how many enumerable image objects the loaded document containsGetLoadedImageInfofills aTHPDFLoadedImageInforecord with object number, page index, dimensions, bit depth, colour space, filter list and decodability metadata without decoding any pixels; it returns False for an out-of-range index or when no information is availableExtractLoadedImagedecodes the selected image into a newTBitmapwhich the caller owns and must free; it returns nil when the source filter is unsupported or the data is damagedDecodabletells you whether the current build can decode that image into a bitmap
Supported sources
- Data filters include
FlateDecode,LZWDecode,ASCIIHexDecode,ASCII85Decode,RunLengthDecode, plus multi-filter chains (leading data filters with a trailing image filter) - Image filters include
DCTDecode(JPEG),JPXDecode(JPEG 2000),JBIG2DecodeandCCITTFaxDecode(G3/G4) - Colour and bit depth: DeviceRGB, DeviceGray, DeviceCMYK, Indexed palettes, and non-8-bit-per-component rasters (1/2/4-bit)
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,BitsPerComponentandColumnsare 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;