O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required