//Include the following namespaces in CustomDocument.cs to enable the class to p erform the printing functionality using System.

Drawing; using System.Drawing.Printing; using System.IO; using System.Windows.Forms; ________________________________________________________________________________ ___________________________________ //Change the class declaration for the CustomDocument as given in the following code class CustomDocument : PrintDocument ________________________________________________________________________________ ____________________________________ //Add the following code in the CustomDocument class: private Font _printFont; private string _fileToPrint; private Single _headerHeight; private TextReader _printStream; // Private bitmap field // String property to set and retrive the file to print. public string FileToPrint { get { return _fileToPrint; } set { if (File.Exists(value)) { _fileToPrint = value; } else throw(new Exception("File not found.")); } } // Font property to get and set the font to be used. public Font PrintFont { get { return _printFont; } set { _printFont = value; } } // Get and Set the height of header section. public Single HeaderHeight {

Font drawFont = new Font("Arial".DocumentName.Bold). Graphics g = e.get { return _headerHeight.Y. } } protected virtual Single printPageHeader(RectangleF bounds.Close().Alignment = StringAlignment. // Initialize the text reader. // Draw the header. b ounds. headerStringFormat). // Create the drawing surface. return localHeaderHeight. bounds.Center. // Return the header height. . bounds. localHeaderHeight = g. StringFormat headerStringFormat = new StringFormat(). _printStream. _printStream = new StreamReader(FileToPrint). string headerText = this. g. Single localHeaderHeight. // To Get the bounds of the header.Width. headerTextLayout.Graphics.OnBeginPrint(e). // To Get the text to be displayed in the header.MeasureString(headerText. // Calculate the height of the header. headerT extLayout. PrintPageEventArgs e ) { // Create drawing surface. drawFont .Height). drawFont. // Header should be center aligned. } set { _headerHeight = value.OnEndPrint(e). 16. headerStringFormat). } protected override void OnPrintPage(PrintPageEventArgs e) { base. } protected override void OnBeginPrint(PrintEventArgs e) { base. RectangleF headerTextLayout = new RectangleF(bounds.OnPrintPage(e).DrawString(headerText. } protected override void OnEndPrint(PrintEventArgs e) { base.Brushes. // Close the text reader.FontStyle. headerStringFormat. // Specify font for header.Size.X.Height.Black.

HeaderHeight.InitialDirectory = "c:\\".MarginBounds. topMargin = e.FileName = "".MarginBounds. // Calculate line height and lines per page.Height. while (lineCount < linesPerPage && ((lineText = _printStream. Single headerSize.GetHeight(gdiPage).Graphics gdiPage = e. Single currentPosition = topMargin. // Get the value of the height property. _printFont. else e.DrawString(lineText. // Specify the header area. // Specify the current position to ensure that line prin ting // begins below the header. height). openFileDialog1. e). Single lineHeight = _printFont.Height / lineHeight .Black. leftMargin = e.MarginBounds. int lineCount = 0.Graphics. } // Check if the end of the file has been reached and set HasMorePages property.Top. // Get Single Single Single Single the bounds of the print area. RectangleF headerBounds = new RectangleF(leftMargin. leftMargin. headerBounds. // Show the dialog. top Margin. // Print the page header and retrieve the height of the header. headerSize = printPageHeader(headerBounds. Single linesPerPage = e. (currentPosition + (lineCount++ * lineHeight ))). // Draw each line of text in turn.MarginBounds.Left. Brushes .Height = this. width = e.HasMorePages = true. .ReadLine()) != null)) { gdiPage. if(lineText != null) e. EventArgs e) { // Set initial properties of the openFileDialog. string lineText = null. currentPosition += (headerSize + 20).MarginBounds. openFileDialog1. } ________________________________________________________________________________ ____________________________________ //Add the following code in the Click event of the button named btnBrowse private void btnBrowse_Click(object sender.HasMorePages = false.Width. height = e. width.

Text = openFileDialog1. Doc. MessageBoxIcon.DialogResult result = openFileDialog1. if (result == DialogResult. printPreviewDialog1. MessageBoxButtons.FileToPrint = txtFileToPrint. display file name and path in text box on for m.OK.ShowDialog(this).ToString().FileName.PrintFont = new Font(cmbFonts.DocumentName = txtHeader.Show("Please select the file to be printed.Exclamation). Doc.Text != "") { // Set properties of the CustomPrintDocument object // Using the values selected on the Printing form. // Specify the print document for the PrintPreviewDialog.ShowDialog(this). } } ________________________________________________________________________________ ____________________________________ //Add the following code in the frmPrint class private CustomDocument Doc = new CustomDocument(). ________________________________________________________________________________ ____________________________________ //Add the following code in the Click event of the button named btnPreview private void btnPreview_Click(object sender. "File n ot selected". } else { MessageBox. txtFileToPrint. Doc.Text. } } . // Open the PrintPreviewDialog.Text. // If user clicked ok.". printPreviewDialog1.Focus().Document = Doc. // Set the headerheight property. 10).HeaderHeight = 100.OK) { txtFileToPrint. Doc.Text. EventArgs e) { // If the FileToPrintTextBox is not empty: if (txtFileToPrint.

Sign up to vote on this title
UsefulNot useful