Discuss this help topic in SecureBlackbox Forum

PDF: Load large PDF documents

If you need to process a large PDF documents and/or if your code fails with OutOfMemory exception, then you need to handle TElPDFDocument.OnCreateTemporaryStream and/or TElPDFDocument.PDFFile.OnCreateTemporaryStream events. The TElPDFDocument.OnCreateTemporaryStream event is fired on loading a document, and TElPDFDocument.PDFFile.OnCreateTemporaryStream event is fired when saving the document in case a document is re-assembled (for example on document encryption). 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.

Create a temporary file stream inside the handler, and then return the corresponding stream object back to the component.


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

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

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

How To articles about common PDF-related tasks

Discuss this help topic in SecureBlackbox Forum