| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| Author: | TX Text Control Support Department |
| Language: | Visual Basic .NET |
| Version: | 1.0 |
| Released: | July 04, 2005 |
| Last modified: | January 11, 2008 |
| Requirements: | TX Text Control .NET with Visual Basic .NET |
| Download code: | tx_sample_print_bitmap.zip |

Sometimes, it is necessary to export every single page of a document to a single image. For example, if you would like to send a fax, some drivers only accept the content as TIFF. This sample shows you how to export images from every page in a document.
As we are using the standard System.Drawing.Image class to realize this, we can easily export images in the formats BMP, WMF, EMF, JPG, GIF, TIF or PNG. It is also possible to create an image of the current document. This can be achieved using the Print method of TX Text Control to print into a System.Drawing.Printing.PrintDocument.
Then we have to create our own PrintController to override the normal controller. After calling the Print method of TX Text Control, we call a function, which implements the PrintController using the default PreviewPrintController. This controller returns the same information such as the default print preview of .NET, including the possibility to save every single page to an image file. We just have to save every page in a loop to an image.
private void PrintDocumentToBitmaps(PrintDocument document _ , string filenamePrefix) { PreviewPrintController controller = new PreviewPrintController(); document.PrintController = controller; document.Print(); PreviewPageInfo[] pages = controller.GetPreviewPageInfo(); for(int index = 0; index < pages.Length; index++) { string filename = string.Format("{0}-{1}.jpg",filenamePrefix, index); pages[index].Image.Save(Application.StartupPath + "\\" _ + filename,System.Drawing.Imaging.ImageFormat.Jpeg); } }
Additionally, it is important to cancel the real printing process by trapping the EndPrint event of the printDocument.
private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e) { e.Cancel = true; }
To change the format of the exported images, we simply have to change the second parameter in the Image.Save method.
At least a TX Text Control .NET trial version and C# .NET 2003 is required to execute the fully functional sample.