Print from a File

Problem

You want to print data from a file without loading it into memory first.

Solution

Use the PrintDocument.BeginPrint event to open a stream to the file and the PrintDocument.EndPrint event to close the stream.

Discussion

When printing a large document, you might want to save the memory overhead by printing the data as it’s read from a stream. This way, the entire document doesn’t need to be held in memory at the same time.

The easiest way to use this approach is to adopt the design from recipe Print a Document That Has Multiple Pages and derive a custom PrintDocument class. This class would track the filename of the document and hold a reference to the underlying file stream. This class would also encapsulate the logic for opening ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.