const PdfDefaultStorageMemoryLimit = 64 * 1024 * 1024;
type TPdfStorageBackend = (psbMemory, psbTemporaryFile);
constructor TPdfRandomAccessStore.Create(AMemoryLimit: Int64 = PdfDefaultStorageMemoryLimit; const ATemporaryDirectory: string = '');
destructor Destroy; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Seek(Offset: Int64; Origin: TSeekOrigin): Int64; override;
function Write(const Buffer; Count: Longint): Longint; override;
function ReadAt(Offset: Int64; var Buffer; Count: Longint): Longint;
function WriteAt(Offset: Int64; const Buffer; Count: Longint): Longint;
procedure CopyToStream(Destination: TStream; BufferSize: Integer = 1048576);
procedure Flush;
procedure SpillToDisk;
property Backend: TPdfStorageBackend;
property MemoryBytes: Int64;
property MemoryLimit: Int64;
property PeakMemoryBytes: Int64;
property Spilled: Boolean;
property TemporaryDirectory: string;
property TemporaryFileName: string;
property SaveMemoryLimit: Int64;
property SaveTemporaryDirectory: string;
property LastSaveSpillCount: Integer;
property LastSaveLargestMemoryBuffer: Int64;
| SaveMemoryLimit | Maximum backing-memory capacity of each internal save store before it spills; defaults to 64 MiB |
| SaveTemporaryDirectory | Existing directory used for spill files; an empty value uses the process temporary directory |
| LastSaveSpillCount | Number of internal working stores that spilled during the most recent save pipeline |
| LastSaveLargestMemoryBuffer | Largest single backing-memory capacity observed during the most recent save pipeline |
| Create | Builds the store with a ceiling between zero and High(Integer), defaulting to PdfDefaultStorageMemoryLimit, and an optional spill directory that must already exist when non-empty |
| Destroy | Zeroes and releases the memory buffer, closes the exclusive spill file, and deletes it from disk |
| Read | TStream override that transfers up to Count bytes from the sequential cursor and advances it |
| Write | TStream override that writes Count bytes at the sequential cursor, spilling first when the write would end beyond MemoryLimit |
| Seek | Moves the cursor relative to the beginning, current position, or end and returns the new offset; negative results and Int64 overflow raise EPdfError |
| CopyToStream | Copies the whole logical content from offset zero into Destination in BufferSize chunks; nil or self destinations and non-positive buffer sizes raise EPdfError |
| Flush | Pushes an open spill file’s buffered bytes to the storage device and raises the last OS error when the flush fails; a memory backend makes the call a no-op |
| SpillToDisk | Requests the one-time migration to the temporary file immediately while the store is still memory-resident |
| Backend | Reports psbMemory while content is RAM-resident and psbTemporaryFile after the spill |
| Spilled | True once the store has migrated to its temporary file; the migration never reverses |
| TemporaryDirectory | Directory selected at construction for the spill file; an empty value resolves to the process temporary directory |
TPdfRandomAccessStore derives from TStream and supports normal sequential reads, writes, seeks, and sizing in addition to cursor-independent positioned I/O
Data remains in a dynamically sized memory buffer while its logical end stays at or below MemoryLimit
A store created without an explicit ceiling uses PdfDefaultStorageMemoryLimit, the same 64 MiB default TPdf applies to SaveMemoryLimit
The first operation that crosses the ceiling creates an exclusive temporary file, copies the existing bytes once, scrubs the memory buffer, and keeps the file backend for the remainder of the store lifetime
The temporary handle uses delete-on-close and random-access flags, permits no sharing, and is removed on normal destruction or constructor rollback
Individual operations are serialized by an internal critical section; ReadAt and WriteAt are preferred when concurrent callers must not compete for the sequential cursor
A store can be passed directly to LoadCustomDocument; PDFium block callbacks use positioned reads and do not alter the store cursor
TPdf uses the configured policy for core saves, every saved-PDF post-processing workspace, and intermediate standards conversion, validation, encryption, DSS, and PAdES staging
Native incremental xref-stream repair reads fixed-size tail and dictionary windows, then applies validated edits while copying unchanged ranges in bounded chunks
MemoryBytes reports backing-buffer capacity rather than logical content sizePeakMemoryBytes never exceeds MemoryLimitLastSaveLargestMemoryBuffer is the largest single internal store, not the sum of simultaneously active storesTBytes still created by a specific parser, compressor, validator, or signerMemoryLimit must be between zero and High(Integer)FPDF_FILEACCESS ABI retains its 4 GiB file-length limit even when the backing store itself has a larger Int64 sizeTemporaryFileName is diagnostic only; the live file is exclusively opened and must not be reopened by the caller
var
Store: TPdfRandomAccessStore;
begin
Pdf.SaveMemoryLimit := 16 * 1024 * 1024;
Pdf.SaveTemporaryDirectory := TPath.GetTempPath;
Pdf.SaveAs('output.pdf');
Log(Pdf.LastSaveSpillCount, Pdf.LastSaveLargestMemoryBuffer);
Store := TPdfRandomAccessStore.Create(4 * 1024 * 1024);
try
Store.CopyFrom(SourceStream, SourceStream.Size);
Store.Position := 0;
Pdf.LoadCustomDocument(Store, False);
finally
Pdf.Active := False;
Store.Free;
end;
end;