Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

How to export the TX Text Control content as images

This source code snippet requires TX Text Control .NET
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 Download [11.34 KB, ZIP]
Click to enlarge

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.

  1. private void PrintDocumentToBitmaps(PrintDocument document _
  2. , string filenamePrefix)
  3. {
  4. PreviewPrintController controller = new PreviewPrintController();
  5. document.PrintController = controller;
  6. document.Print();
  7.  
  8. PreviewPageInfo[] pages = controller.GetPreviewPageInfo();
  9.  
  10. for(int index = 0; index < pages.Length; index++)
  11. {
  12. string filename = string.Format("{0}-{1}.jpg",filenamePrefix, index);
  13. pages[index].Image.Save(Application.StartupPath + "\\" _
  14. + filename,System.Drawing.Imaging.ImageFormat.Jpeg);
  15. }
  16. }

Additionally, it is important to cancel the real printing process by trapping the EndPrint event of the printDocument.

  1. private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
  2. {
  3. e.Cancel = true;
  4. }

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.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place