Windows Develop Bookmark and Share   
 index > Windows Forms General > How can I print a file?
 

How can I print a file?

Hi everybody,

I need your help.

The code below came from a book which teaches how to print a sketch. The book did not show how to print a file. Right now if I run this program (as is)it will show the printer dialog box. If I click OK (to print), the printer will print an empty paper. How can I use this code to print a file (“ReadMe.txt�?

I would apprciate it if you could help.

private: System::Void printToolStripMenuItem_Click(System::Object ^ sender, System::EventArgs ^ e)

{

// The PrintDocument holds the settings

PrintDocument^ pdoc = gcnew PrintDocument();

// Create a dialog and attach it to the document

PrintDialog^ pd = gcnew PrintDialog();

pd->Document = pdoc;

// Show the dialog

if (pd->ShowDialog() == System::Windows::Forms::DialogResult::OK)

{

// Add the page handler

pdoc->PrintPage += gcnew PrintPageEventHandler(this,&Form1::PrintAPage);

// Print the page

pdoc->Print();

}

else

MessageBox::Show("Print cancelled", "Information");

}

void PrintAPage(Object^ pSender, PrintPageEventArgs^ pe)

{

}

Allen_Press  Thursday, October 01, 2009 10:36 PM

Hi Everybody;

I found it.  If someone wants to know how?  It here.

[Code]

//Visual C++ Copy Code
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::IO;
using namespace System::Drawing;
using namespace System::Drawing::Printing;
using namespace System::Windows::Forms;

public ref class PrintingExample: public System::Windows::Forms::Form
{
private:
   System::ComponentModel::Container^ components;
   System::Windows::Forms::Button^ printButton;
   System::Drawing::Font^ printFont;
   StreamReader^ streamToPrint;

public:
   PrintingExample()
      : Form()
   {

      // The Windows Forms Designer requires the following call.
      InitializeComponent();
   }


private:

   // The Click event is raised when the user clicks the Print button.
   void printButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      try
      {
         streamToPrint = gcnew StreamReader( "C:\\My Documents\\MyFile.txt" );
         try
         {
            printFont = gcnew System::Drawing::Font( "Arial",10 );
            PrintDocument^ pd = gcnew PrintDocument;
            pd->PrintPage += gcnew PrintPageEventHandler( this, &PrintingExample::pd_PrintPage );
            pd->Print();
         }
         finally
         {
            streamToPrint->Close();
         }

      }
      catch ( Exception^ ex )
      {
         MessageBox::Show( ex->Message );
      }

   }


   // The PrintPage event is raised for each page to be printed.
   void pd_PrintPage( Object^ /*sender*/, PrintPageEventArgs^ ev )
   {
      float linesPerPage = 0;
      float yPos = 0;
      int count = 0;
      float leftMargin = (float)ev->MarginBounds.Left;
      float topMargin = (float)ev->MarginBounds.Top;
      String^ line = nullptr;

      // Calculate the number of lines per page.
      linesPerPage = ev->MarginBounds.Height / printFont->GetHeight( ev->Graphics );

      // Print each line of the file.
      while ( count < linesPerPage && ((line = streamToPrint->ReadLine()) != nullptr) )
      {
         yPos = topMargin + (count * printFont->GetHeight( ev->Graphics ));
         ev->Graphics->DrawString( line, printFont, Brushes::Black, leftMargin, yPos, gcnew StringFormat );
         count++;
      }


      // If more lines exist, print another page.
      if ( line != nullptr )
            ev->HasMorePages = true;
      else
            ev->HasMorePages = false;
   }


   // The Windows Forms Designer requires the following procedure.
   void InitializeComponent()
   {
      this->components = gcnew System::ComponentModel::Container;
      this->printButton = gcnew System::Windows::Forms::Button;
      this->ClientSize = System::Drawing::Size( 504, 381 );
      this->Text = "Print Example";
      printButton->ImageAlign = System::Drawing::ContentAlignment::MiddleLeft;
      printButton->Location = System::Drawing::Point( 32, 110 );
      printButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
      printButton->TabIndex = 0;
      printButton->Text = "Print the file.";
      printButton->Size = System::Drawing::Size( 136, 40 );
      printButton->Click += gcnew System::EventHandler( this, &PrintingExample::printButton_Click );
      this->Controls->Add( printButton );
   }

};


// This is the main entry point for the application.
int main()
{
   Application::Run( gcnew PrintingExample );
}

[/Code]

  • Marked As Answer byAllen_Press Wednesday, October 07, 2009 2:38 AM
  •  
Allen_Press  Wednesday, October 07, 2009 2:37 AM
Put some code inside of PrintAPage(). There's a fairly heavy Doh! factor to that answer.

Hans Passant.
nobugz  Thursday, October 01, 2009 11:46 PM
Thanks Hans Passant  for answering.  the following is some code I can put inside the function for creating graphics and print them.  I want to print an already typed text.  I am expect it to be light Doh.  I think that; All I need is a mechanzim to enter a file location. 

 Graphics* gr = pe->Graphics;
     Pen* pen1 = new Pen(Color::Black);
  
     // Draw the image
     Bitmap* bmp = new Bitmap(S"ramp1.gif");
     gr->DrawImage(bmp, 10,10);
  
     for(int i=0; i<list->Count; i++)
     {
      Line* pl = dynamic_cast<Line*>(list->get_Item(i));
      gr->DrawLine(pen1, pl->p1.X,pl->p1.Y, pl->p2.X,pl->p2.Y);
     }
Allen_Press  Saturday, October 03, 2009 1:36 AM
Use an OpenFileDialog and a field in your class to store its FileName.

Hans Passant.
nobugz  Saturday, October 03, 2009 1:48 AM
Can you give Example?
Allen_Press  Saturday, October 03, 2009 11:44 PM

Hi Everybody;

I found it.  If someone wants to know how?  It here.

[Code]

//Visual C++ Copy Code
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::IO;
using namespace System::Drawing;
using namespace System::Drawing::Printing;
using namespace System::Windows::Forms;

public ref class PrintingExample: public System::Windows::Forms::Form
{
private:
   System::ComponentModel::Container^ components;
   System::Windows::Forms::Button^ printButton;
   System::Drawing::Font^ printFont;
   StreamReader^ streamToPrint;

public:
   PrintingExample()
      : Form()
   {

      // The Windows Forms Designer requires the following call.
      InitializeComponent();
   }


private:

   // The Click event is raised when the user clicks the Print button.
   void printButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      try
      {
         streamToPrint = gcnew StreamReader( "C:\\My Documents\\MyFile.txt" );
         try
         {
            printFont = gcnew System::Drawing::Font( "Arial",10 );
            PrintDocument^ pd = gcnew PrintDocument;
            pd->PrintPage += gcnew PrintPageEventHandler( this, &PrintingExample::pd_PrintPage );
            pd->Print();
         }
         finally
         {
            streamToPrint->Close();
         }

      }
      catch ( Exception^ ex )
      {
         MessageBox::Show( ex->Message );
      }

   }


   // The PrintPage event is raised for each page to be printed.
   void pd_PrintPage( Object^ /*sender*/, PrintPageEventArgs^ ev )
   {
      float linesPerPage = 0;
      float yPos = 0;
      int count = 0;
      float leftMargin = (float)ev->MarginBounds.Left;
      float topMargin = (float)ev->MarginBounds.Top;
      String^ line = nullptr;

      // Calculate the number of lines per page.
      linesPerPage = ev->MarginBounds.Height / printFont->GetHeight( ev->Graphics );

      // Print each line of the file.
      while ( count < linesPerPage && ((line = streamToPrint->ReadLine()) != nullptr) )
      {
         yPos = topMargin + (count * printFont->GetHeight( ev->Graphics ));
         ev->Graphics->DrawString( line, printFont, Brushes::Black, leftMargin, yPos, gcnew StringFormat );
         count++;
      }


      // If more lines exist, print another page.
      if ( line != nullptr )
            ev->HasMorePages = true;
      else
            ev->HasMorePages = false;
   }


   // The Windows Forms Designer requires the following procedure.
   void InitializeComponent()
   {
      this->components = gcnew System::ComponentModel::Container;
      this->printButton = gcnew System::Windows::Forms::Button;
      this->ClientSize = System::Drawing::Size( 504, 381 );
      this->Text = "Print Example";
      printButton->ImageAlign = System::Drawing::ContentAlignment::MiddleLeft;
      printButton->Location = System::Drawing::Point( 32, 110 );
      printButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
      printButton->TabIndex = 0;
      printButton->Text = "Print the file.";
      printButton->Size = System::Drawing::Size( 136, 40 );
      printButton->Click += gcnew System::EventHandler( this, &PrintingExample::printButton_Click );
      this->Controls->Add( printButton );
   }

};


// This is the main entry point for the application.
int main()
{
   Application::Run( gcnew PrintingExample );
}

[/Code]

  • Marked As Answer byAllen_Press Wednesday, October 07, 2009 2:38 AM
  •  
Allen_Press  Wednesday, October 07, 2009 2:37 AM

You can use google to search for other answers

Custom Search

More Threads

• Is there a way to REALLY hide a Column in a DataGridView?
• hide window on startup
• Tabular Data
• [SOLVED]Forms and Menory usage
• How to implement a 30 day trial period?
• UserControlTestContainer error
• Text wrap about button
• Dyanmic label size
• Screen Refresh taking Forever
• dataGrid connected with ComboBoxColumn