EldoS | Feel safer!

Software components for data protection, secure storage and transfer

When loading large PDF document, OutOfMemory error happens. How do I deal with it?

In this case you should handle TElPDFDocument.OnCreateTemporaryStream and/or TElPDFDocument.PDFFile.OnCreateTemporaryStream events, create a temporary file on the disk inside the handler, and then return the corresponding FileStream/TFileStream object back to the component. If the event is not handled, a memory stream is internally created and used. This can lead to out-of-memory exceptions on large files.

The TElPDFDocument.OnCreateTemporaryStream event handler is fired when the document is loaded, and TElPDFDocument.PDFFile.OnCreateTemporaryStream event handler is fired on saving the document when the document is reassembled (for example during document encryption).

Sample code:


PDFDocument.OnCreateTemporaryStream := DoCreateTemporaryStream;
PDFDocument.PDFFile.OnCreateTemporaryStream := DoCreateTemporaryStream;

procedure TfrmMain.DoCreateTemporaryStream(Sender: TObject; var Stream: 
TStream; var FreeOnClose : boolean);
   Stream := TFileStream.Create(GenerateTempFilename, fmOpenReadWrite or 
   FreeOnClose := True;


AddHandler PDFDocument.OnCreateTemporaryStream, AddressOf 
HandleCreateTemporaryStream ' on opening document
AddHandler PDFDocument.PDFFile.OnCreateTemporaryStream, AddressOf 
HandleCreateTemporaryStream ' on saving

Private Sub HandleCreateTemporaryStream(sender As Object, ByRef stream 
As IO.Stream, ByRef freeOnClose As Boolean)
   stream = New FileStream(IO.Path.GetTempFileName, FileMode.Create)
   freeOnClose = True
End Sub

Return to the list


Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!