Loaded Image Extraction

功能概述

加载侧图像提取让你在用 LoadFromFile / LoadFromStream 打开已有 PDF 后,枚举其内嵌的图像 XObject 并把它们解码为 TBitmap,便于预览、转码或重新嵌入。HotPDF 扫描 /Subtype /Image 对象并按其 /Filter 选择解码路径,覆盖 v2.229–2.231 引入的九类来源

API

  • function GetLoadedImageCount: Integer:返回当前已加载文档中可枚举的图像对象数量
  • function GetLoadedImageInfo(ImageIndex: Integer; out AInfo: THPDFLoadedImageInfo): Boolean:取得指定图像的元信息(尺寸、色彩、过滤器等),不解码像素;索引越界或无信息时返回 False
  • function ExtractLoadedImage(ImageIndex: Integer): TBitmap:把指定图像解码为新建 TBitmap(调用方负责释放);来源过滤器不受支持或数据损坏时返回 nil

THPDFLoadedImageInfo 字段

  • Width / Height:像素尺寸
  • BitsPerComponent:每分量位深(1 / 2 / 4 / 8)
  • ColorComponents:色彩分量数(灰度 1、RGB 3、CMYK 4)
  • Filter:来源过滤器名(FlateDecode / DCTDecode / JPXDecode / JBIG2Decode / CCITTFaxDecode 等)
  • ColorSpace:色彩空间名(DeviceGray / DeviceRGB / DeviceCMYK / Indexed 等)
  • IsImageMask:是否为 /ImageMask 模板图
  • ObjectNumber:图像的 PDF 间接对象号
  • Decodable:当前构建是否能把该图解码为位图

支持的过滤器

  • 数据过滤器:FlateDecodeLZWDecodeASCIIHexDecodeASCII85DecodeRunLengthDecode,以及多过滤器链(前置数据过滤器 + 末级图像过滤器)
  • 图像过滤器:DCTDecode(JPEG)、JPXDecode(JPEG 2000)、JBIG2DecodeCCITTFaxDecode(G3/G4)
  • 色彩与位深:DeviceGray / DeviceRGB / DeviceCMYK / Indexed 调色板,以及非 8-bit 每分量(1/2/4-bit)栅格
  • JBIG2DecodeJPXDecode 默认走内置高性能解码后端

Usage Example


procedure ExtractAllImages(const FileName: string);
var
  Doc: THotPDF;
  Info: THPDFLoadedImageInfo;
  Bmp: TBitmap;
  I: Integer;
begin
  Doc := THotPDF.Create(nil);
  try
    Doc.LoadFromFile(FileName);
    for I := 0 to Doc.GetLoadedImageCount - 1 do
    begin
      if Doc.GetLoadedImageInfo(I, Info) and Info.Decodable then
      begin
        Bmp := Doc.ExtractLoadedImage(I);
        try
          if Bmp <> nil then
            Bmp.SaveToFile(Format('image_%d.bmp', [I]));
        finally
          Bmp.Free;
        end;
      end;
    end;
  finally
    Doc.Free;
  end;
end;
        

适用场景

  • 预览或导出已有 PDF 内嵌的图像
  • 把 JBIG2 / JPEG 2000 / CCITT 等图像转码为更通用的格式
  • 检查图像元信息(尺寸、色彩空间、过滤器)而不必解码全部像素

另请参阅