11unit StreamLoadingComparison;
22
33{ *******************************************************************************
4- Demonstrates the three different methods for loading PDFs in DX.Pdfium4D:
4+ Demonstrates the two different methods for loading PDFs in DX.Pdfium4D:
55 1. LoadFromFile - Direct file loading
6- 2. LoadFromStream - Legacy stream loading (loads entire PDF into memory)
7- 3. LoadFromStreamEx - True streaming support (efficient, on-demand loading)
6+ 2. LoadFromStream - Efficient streaming support (on-demand loading)
87*******************************************************************************}
98
109interface
@@ -24,14 +23,9 @@ TPdfLoadingExamples = class
2423 class procedure LoadFromFileExample (APdfViewer: TPdfViewer);
2524
2625 // / <summary>
27- // / Method 2: Load from stream - LEGACY (loads entire PDF into memory)
26+ // / Method 2: Load from stream with efficient streaming support
2827 // / </summary>
29- class procedure LoadFromStreamLegacyExample (APdfViewer: TPdfViewer);
30-
31- // / <summary>
32- // / Method 3: Load from stream - NEW (true streaming, efficient)
33- // / </summary>
34- class procedure LoadFromStreamExExample (APdfViewer: TPdfViewer);
28+ class procedure LoadFromStreamExample (APdfViewer: TPdfViewer);
3529
3630 // / <summary>
3731 // / Comparison: Memory usage for different methods
@@ -50,69 +44,39 @@ class procedure TPdfLoadingExamples.LoadFromFileExample(APdfViewer: TPdfViewer);
5044 // - PDFium handles file access internally
5145 // - Implementation unknown (could be memory-mapped, buffered, or streaming)
5246 // - Best for: Local files that already exist on disk
53-
47+
5448 APdfViewer.LoadFromFile(' C:\Documents\report.pdf' );
55-
56- // That's it! No stream management needed.
57- end ;
5849
59- class procedure TPdfLoadingExamples.LoadFromStreamLegacyExample (APdfViewer: TPdfViewer);
60- var
61- LStream: TMemoryStream;
62- begin
63- // ⚠️ Method 2: LoadFromStream (LEGACY - DEPRECATED)
64- // - Loads ENTIRE stream into memory (TBytes buffer)
65- // - Buffer remains in memory for document lifetime
66- // - Uses FPDF_LoadMemDocument internally
67- // - Best for: Small PDFs only (<10 MB)
68- // - NOT recommended for large files!
69-
70- LStream := TMemoryStream.Create;
71- try
72- // Example: Load from HTTP
73- // LHttpClient.Get('https://example.com/document.pdf', LStream);
74- LStream.LoadFromFile(' C:\Documents\report.pdf' );
75- LStream.Position := 0 ;
76-
77- // ⚠️ WARNING: Entire stream is copied into internal buffer!
78- APdfViewer.LoadFromStream(LStream);
79-
80- // Stream can be freed immediately - data is already copied
81- finally
82- LStream.Free;
83- end ;
84-
85- // Memory impact: PDF size + internal buffer = 2x PDF size (temporarily)
86- // After stream is freed: PDF size remains in TPdfDocument.FMemoryBuffer
50+ // That's it! No stream management needed.
8751end ;
8852
89- class procedure TPdfLoadingExamples.LoadFromStreamExExample (APdfViewer: TPdfViewer);
53+ class procedure TPdfLoadingExamples.LoadFromStreamExample (APdfViewer: TPdfViewer);
9054var
9155 LStream: TMemoryStream;
9256begin
93- // ✅ Method 3: LoadFromStreamEx (RECOMMENDED for streams)
57+ // ✅ Method 2: LoadFromStream (RECOMMENDED for streams)
9458 // - TRUE streaming support via FPDF_LoadCustomDocument
9559 // - PDFium reads blocks on-demand via callback
9660 // - Stream is NOT duplicated in memory
9761 // - Stream must remain valid for document lifetime
9862 // - Best for: Large PDFs, network streams, memory-constrained scenarios
99-
63+
10064 LStream := TMemoryStream.Create;
101-
65+
10266 // Example: Load from HTTP, database, encrypted container, etc.
10367 LStream.LoadFromFile(' C:\Documents\large_report.pdf' );
10468 LStream.Position := 0 ;
105-
69+
10670 // Option A: Keep ownership of stream (you manage lifetime)
107- APdfViewer.LoadFromStreamEx (LStream, False); // AOwnsStream = False
71+ APdfViewer.LoadFromStream (LStream, False); // AOwnsStream = False
10872 // You MUST keep LStream alive until document is closed!
10973 // You MUST free LStream yourself later
110-
74+
11175 // Option B: Transfer ownership to viewer (recommended)
112- // APdfViewer.LoadFromStreamEx (LStream, True); // AOwnsStream = True
76+ // APdfViewer.LoadFromStream (LStream, True); // AOwnsStream = True
11377 // Viewer will free the stream when document is closed
11478 // DO NOT free LStream yourself!
115-
79+
11680 // Memory impact: Only 1x PDF size (the stream itself)
11781 // PDFium reads blocks on-demand - no duplication!
11882end ;
@@ -130,25 +94,21 @@ class procedure TPdfLoadingExamples.CompareMemoryUsage;
13094 // │ LoadFromFile │ Unknown* │ ✅ Good for local │
13195 // │ │ │ files │
13296 // ├─────────────────────────────────────────────────────────────┤
133- // │ LoadFromStream │ ~200 MB │ ⚠️ Only for small │
134- // │ (Legacy) │ (2x during load)│ PDFs (<10 MB) │
135- // │ │ ~100 MB after │ │
136- // ├─────────────────────────────────────────────────────────────┤
137- // │ LoadFromStreamEx │ ~100 MB │ ✅ Best for streams │
138- // │ (Recommended) │ (1x, no copy) │ and large files │
97+ // │ LoadFromStream │ ~100 MB │ ✅ Best for streams │
98+ // │ │ (1x, no copy) │ and large files │
13999 // └─────────────────────────────────────────────────────────────┘
140100 //
141101 // * PDFium's internal implementation is not documented
142102 // Could be memory-mapped, buffered, or streaming
143-
103+
144104 LDocument := TPdfDocument.Create;
145105 LStream := TMemoryStream.Create;
146106 try
147107 LStream.LoadFromFile(' large_file.pdf' );
148-
149- // Use LoadFromStreamEx for efficient streaming
150- LDocument.LoadFromStreamEx (LStream, False);
151-
108+
109+ // Use LoadFromStream for efficient streaming
110+ LDocument.LoadFromStream (LStream, False);
111+
152112 // Stream is NOT duplicated - PDFium reads on-demand
153113 // Memory usage = stream size only
154114 finally
0 commit comments