How do I use the loadStream function with .NET?

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

The loadStream function allows you to read a PDF file from an OLE Stream object instead of reading a file from disk. Note that OLE Stream is not the same thing as the .NET Stream class.

To use loadStream from a .NET application (VB.NET, C#, etc.), you'll need to create an OLE Stream object. The easiest way to do that is with a wrapper class. We recommend the ManagedIStream class described in Adam Nathan's book, .NET and COM: The Complete Interoperability Guide. You can download the source code from the book's web site (under the "Downloads" tab).

As an alternative, you can use the CreateStreamOnHGlobal function:

Imports System.Runtime.InteropServices ... Declare Function CreateStreamOnHGlobal Lib "ole32" _ (ByVal hGlobal As IntPtr, _ ByVal fDeleteOnRelease As Boolean, _ ByRef ppstm As UCOMIStream) As Long ... ' Create the Stream ("UCOMIStream" in .NET) object. Dim str As UCOMIStream = Nothing CreateStreamOnHGlobal(Nothing, True, str) ' Copy buf (a Byte array containing a PDF file) into the stream. str.Write(buf, buf.Length, Nothing) ' 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.