How do I use the loadStream function?

Applies to: XpdfInfo, XpdfText, XpdfViewer, XpdfPrint, XpdfRasterizer, XpdfAnalyze, XpdfPS, XpdfImageExtract

The loadStream (and loadStreamWithPassword) functions are available in all of Glyph & Cog's COM components. These functions allow you to read a PDF file from an OLE Stream object instead of reading a file from disk.

Here is sample VB code showing how to read a PDF file into memory, create a Stream object, and then call XpdfViewer's loadStream function. (In real use, you would probably be getting the PDF file data from somewhere other than disk - otherwise, there's not much point in using loadStream instead of loadFile.)

' Declare some functions from the Windows API Private Declare Function GlobalAlloc Lib "kernel32" _ (ByVal wFlags As Long, ByVal dwBytes As Long) As Long Private Declare Function GlobalLock Lib "kernel32" _ (ByVal hMem As Long) As Long Private Declare Function GlobalUnlock Lib "kernel32" _ (ByVal hMem As Long) As Long Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _ (pDest As Any, pSrc As Any, ByVal ByteLen As Long) Private Declare Function CreateStreamOnHGlobal Lib "ole32" _ (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, _ ppstm As Object) As Long ... Dim size As Long Dim byteArray() As Byte Dim f As Integer Dim mem As Long Dim ptr As Long Dim str As IUnknown ' Read the PDF file (fileName) into memory. size = FileLen(fileName) ReDim byteArray(1 To size) f = FreeFile Open fileName For Binary Access Read As #f Get #f, , byteArray() Close #f ' Copy it into global memory. mem = GlobalAlloc(&H2, size) ptr = GlobalLock(mem) MoveMemory ByVal ptr, byteArray(1), size GlobalUnlock mem ' Create an OLE Stream object. CreateStreamOnHGlobal mem, 1, str ' Load the PDF stream into XpdfViewer. viewer.loadStream str

IMPORTANT: The Stream object must be kept valid as long as XpdfViewer (or another G&C component) has the PDF file open. Do not delete the Stream until you are done with that PDF file.