Load a PDF from a Stream in C# .NET

To load a PDF document to your program from a previously instantiated stream object, use the LoadFromStream method from the GdPicturePDF class. This method requires a stream object as its parameter. The stream object should remain open for subsequent use. To specify that the GdPicturePDF instance using the stream object should dispose of the object when it’s no longer needed, set the optional OwnStream parameter in the LoadFromStream method to true. By default, it’s set to false.

You can also load a PDF document to your program from an IStream object. Use the LoadFromIStream method from the GdPicturePDF class. This method requires an IStream object as its parameter.

To load a PDF document from a stream object, use the following code:

// Create a stream object from a PDF file.
System.IO.Stream streamPDF = new System.IO.FileStream(@"C:\temp\source.pdf", System.IO.FileMode.Open);
// Create a `GdPicturePDF` object.
using GdPicturePDF gdpicturePDF = new GdPicturePDF();

// Load a PDF document from the stream object with `OwnStream` set to `false`.
gdpicturePDF.LoadFromStream(streamPDF);
// Close the PDF document.
gdpicturePDF.CloseDocument();
// Dispose of the stream object.
streamPDF.Dispose();

// Create a stream object from a PDF file.
streamPDF = new System.IO.FileStream(@"C:\temp\source.pdf", System.IO.FileMode.Open);
// Load a PDF document from the stream object with `OwnStream` set to `true`.
gdpicturePDF.LoadFromStream(streamPDF, true);
// Close the PDF document. The stream object is disposed of as well.
gdpicturePDF.CloseDocument();
' Create a stream object from a PDF file.
Dim streamPDF As Stream = New FileStream("C:\temp\source.pdf", FileMode.Open)
' Create a `GdPicturePDF` object.
Dim gdpicturePDF As GdPicturePDF = New GdPicturePDF()

' Load a PDF document from the stream object with `OwnStream` set to `false`.
gdpicturePDF.LoadFromStream(streamPDF)
' Close the PDF document.
gdpicturePDF.CloseDocument()
' Dispose of the stream object.
streamPDF.Dispose()

' Create a stream object from a PDF file.
streamPDF = New FileStream("C:\temp\source.pdf", FileMode.Open)
' Load a PDF document from the stream object with `OwnStream` set to `true`.
gdpicturePDF.LoadFromStream(streamPDF, True)
' Close the PDF document. The stream object is disposed of as well.
gdpicturePDF.CloseDocument()