PDFium Delphi Component Docs

Banded rendering methods

Component: TPdf  ·  Unit: PDFium
Render the current page in correctly clipped horizontal bands, feed caller codecs, or write PNG and TIFF output without a full-size bitmap

Syntax

function RenderPageBanded(Width, Height, BandHeight: Integer; BandCallback: TPdfBandCallback; Rotation: TRotation = ro0; Options: TRenderOptions = []; Color: TColor = clWhite): Integer;

Width, HeightInteger. Size of the full page image in pixels, as if it were rendered in one piece
BandHeightInteger. Height of each band in pixels. The last band is shorter when Height is not a multiple of it
BandCallbackTPdfBandCallback. Method called once per band as function(BandIndex, BandTopY: Integer; Bitmap: TBitmap): Boolean of object. Return False to cancel the render early
RotationTRotation. Display rotation applied to the page: ro0, ro90, ro180 or ro270
OptionsTRenderOptions. Rendering flags
ColorTColor. Background fill applied to each band before rendering
ResultInteger. Number of bands actually rendered, or 0 on failure

RenderPageBandedToEncoder

function RenderPageBandedToEncoder(Width, Height, BandHeight: Integer; Encoder: TPdfBandedImageEncoder; Rotation: TRotation = ro0; Options: TRenderOptions = []; Color: TColor = clWhite): TPdfBandedImageReport;

The caller owns Encoder and implements BeginImage, WriteBand and EndImage. Bands arrive from top to bottom with zero-based indices and full-page BandTopY coordinates

WriteBand returns True to continue or False to stop cleanly. The bitmap is valid only during the call. GetBytesWritten may return encoder-specific output telemetry

The result reports rendered and accepted bands in BandsRendered and BandsEncoded, encoded rows in RowsEncoded, peak BGRA band bytes in PeakBandBytes, output bytes and whether every requested row completed

RenderPageBandedToStream

function RenderPageBandedToStream(Stream: TStream; Width, Height: Integer; const StreamOptions: TPdfBandedImageStreamOptions; Rotation: TRotation = ro0; Options: TRenderOptions = []; Color: TColor = clWhite): TPdfBandedImageReport;

StreamTStream. Caller-owned writable output positioned where the image begins
StreamOptions.FormatpbifPng for compressed 8-bit RGB PNG or pbifTiff for uncompressed little-endian classic TIFF strips
StreamOptions.BandHeightPositive band and TIFF strip height; the default is 256 rows
StreamOptions.CompressionLevelPNG zlib level from 0 through 9; the default is 6 and TIFF ignores it
StreamOptions.MaxOutputBytesHard encoded byte ceiling; zero means unlimited

Both built-in encoders write forward only and do not seek the caller stream. PNG compresses each scanline directly into sequential IDAT chunks, whilst TIFF writes one RGB strip per render band and appends its directory after the pixels

Peak page pixel storage is one Width x BandHeight BGRA bitmap rather than Width x Height. The report exposes the measured peak band bytes so exporters can enforce and audit memory policy

Use TPdfBandedImageStreamOptions.Default(pbifPng) or Default(pbifTiff) before overriding individual limits

Description

Posters, maps and engineering drawings can exceed the memory a full-page bitmap would need at print resolution. Banding keeps peak memory at one band instead: each band is rendered into a fresh bitmap of Width x BandHeight and handed to the callback

The callback receives the zero-based band index and BandTopY, the y-offset of the band top in full-page pixels, which is what a writer needs to place the strip in an output file. Each bitmap keeps the complete page device transform and is clipped to that coordinate range. The bitmap belongs to the render loop and is freed after the callback returns, so a callback that needs to keep the pixels has to copy them

Returning False from the callback stops the loop; the return value then counts the bands that were rendered before the cancellation

Example

function TForm1.WriteBand(BandIndex, BandTopY: Integer;
  Bitmap: TBitmap): Boolean;
begin
  Bitmap.SaveToFile(Format('band%d.bmp', [BandIndex]));
  Result := True;
end;

Pdf.RenderPageBanded(4960, 7016, 512, WriteBand);

StreamOptions := TPdfBandedImageStreamOptions.Default(pbifPng);
StreamOptions.BandHeight := 256;
StreamOptions.MaxOutputBytes := 256 * 1024 * 1024;
Report := Pdf.RenderPageBandedToStream(OutputStream, 4960, 7016,
  StreamOptions);

See Also

RenderPage, RenderTile, RenderPageWithMatrix, RenderPageProgressive