How-To: Printing into a PrintDocument

TX Text Control .NET for Windows Forms's Print method introduces many advantages over the Print method of the ActiveX control. It enables you to print into a PrintDocument of the System.Drawing.Printing namespace.

TX Text Control .NET for Windows Forms's Print method uses the following settings of this object:

  • DocumentName
  • PrintController
  • PrinterSettings.FromPage
  • PrinterSettings.ToPage
  • PrinterSettings.Copies
  • PrinterSettings.Collate
  • PrinterSettings.PrinterName
  • PrinterSettings.PrintToFile
  • PrinterSettings.PrintRange
  • DefaultPageSettings.Color

If you would like to print one specific page of the document, not only FromPage and ToPage must be specified, but also the PrintRange property.

The following code sample prints page 2 of TX Text Control and sets the current page size.

int m_curPage = 2;
PrintDocument myPrintDoc = new PrintDocument();
myPrintDoc.DefaultPageSettings.PaperSize = new PaperSize("default",
                                                         textControl1.PageSize.Width,
                                                         textControl1.PageSize.Height);
myPrintDoc.DefaultPageSettings.Margins = new Margins(textControl1.PageMargins.Left,
                                                     textControl1.PageMargins.Right,
                                                     textControl1.PageMargins.Top,
                                                     textControl1.PageMargins.Bottom);
myPrintDoc.PrinterSettings.PrintRange = PrintRange.SomePages;
myPrintDoc.PrinterSettings.FromPage = m_curPage;
myPrintDoc.PrinterSettings.ToPage = m_curPage;
textControl1.Print(myPrintDoc);
Dim CurrentPage As Integer = 2
Dim MyPrintDoc As PrintDocument = New PrintDocument()
MyPrintDoc.DefaultPageSettings.PaperSize = New PaperSize("default",
                                                         TextControl1.PageSize.Width,
                                                         TextControl1.PageSize.Height)
MyPrintDoc.DefaultPageSettings.Margins = New Margins(TextControl1.PageMargins.Left,
                                                     TextControl1.PageMargins.Right,
                                                     TextControl1.PageMargins.Top,
                                                     TextControl1.PageMargins.Bottom)
MyPrintDoc.PrinterSettings.PrintRange = PrintRange.SomePages
MyPrintDoc.PrinterSettings.FromPage = CurrentPage
MyPrintDoc.PrinterSettings.ToPage = CurrentPage
TextControl1.Print(MyPrintDoc)

As you can see, the PrintPage property must be specified with SomePages which indicates that the FromPage and ToPage is considered.