Connecting Tech Pros Worldwide Help | Site Map

How can I use print handler to pring a file?

Newbie
 
Join Date: Oct 2009
Location: USA
Posts: 7
#1: Oct 4 '09
Hi everybody,

I need your help.

The code below is good if I want to print a sketch. The book did not show how to print a simple text 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?

I would apprciate it if you could help.


Expand|Select|Wrap|Line Numbers
  1. private: System::Void printToolStripMenuItem_Click(System::Object ^  sender, System::EventArgs ^  e)
  2.  {
  3.  // The PrintDocument holds the settings
  4.  PrintDocument^ pdoc = gcnew PrintDocument();
  5.  
  6. // Create a dialog and attach it to the document
  7. PrintDialog^ pd = gcnew PrintDialog();
  8.  
  9. pd->Document = pdoc;
  10.  
  11. // Show the dialog
  12. if (pd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
  13.  
  14. {
  15. // Add the page handler
  16.  
  17. pdoc->PrintPage += gcnew PrintPageEventHandler(this,&Form1::PrintAPage);
  18. // Print the page
  19. pdoc->Print();
  20.  
  21. }
  22. else
  23.  MessageBox::Show("Print cancelled", "Information");
  24. }
  25.  
  26. void PrintAPage(Object^ pSender, PrintPageEventArgs^ pe)
  27. {
  28.  
  29.  
  30.  
  31.  
  32. }
  33.  
  34.  
If I put the following code inside the empty function (PrintAPage)I will be ablble to draw and print what I draw. But what I need is to print an already written and saved simple text File.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Graphics* gr = pe->Graphics;
  3.      Pen* pen1 = new Pen(Color::Black);
  4.  
  5.      // Draw the image
  6.      Bitmap* bmp = new Bitmap(S"ramp1.gif");
  7.      gr->DrawImage(bmp, 10,10);
  8.  
  9.      for(int i=0; i<list->Count; i++)
  10.      {
  11.       Line* pl = dynamic_cast<Line*>(list->get_Item(i));
  12.       gr->DrawLine(pen1, pl->p1.X,pl->p1.Y, pl->p2.X,pl->p2.Y);
  13.      }
  14.  
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#2: Oct 4 '09

re: How can I use print handler to pring a file?


You will need to use something like the File class from the System.Io name space to load the file.

Then you can use gr->DrawString(...) to display the loaded text.
Newbie
 
Join Date: Oct 2009
Location: USA
Posts: 7
#3: Oct 4 '09

re: How can I use print handler to pring a file?


Hi Banfa,

I appreciate your help.

I did the following and Ihave the following error.

FileStream^ fs = gcnew FileStream("ReadMe.txt", FileMode::Open);
Graphics^ gr;
gr->DrawString();

error C2661: 'System::Drawing::Graphics::DrawString' : no overloaded function takes 0 arguments
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#4: Oct 5 '09

re: How can I use print handler to pring a file?


That is because you tried to call DrawString with no parameters for which no function exists. You have to say where and what to draw using what font.

I suggest you read the documentation on DrawString, you will find it at the end of the link in my previous post.
Newbie
 
Join Date: Oct 2009
Location: USA
Posts: 7
#5: Oct 5 '09

re: How can I use print handler to pring a file?


I went there already and the instruction wants me to show something about brush and other things. I do not want to forget to let you know that I do not want to draw anything. The book has a program to print what you draw and I did it very well. But the problem is; I do not want to draw or even print a graphic file. All I want to do is to print a simple text file. The thing that frustrated me is I can do the hard thing by following the book, But cannot modify it to make it simpler enough to just print a simple text file from the already saved file.

I really thank you a lot for trying to help
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#6: Oct 5 '09

re: How can I use print handler to pring a file?


You will need to have a brush and a font to draw your text because you may think you do not want to draw anything, just print next but in fact the way you print text is to draw it using a brush and font.

I am now moving this to the .NET forum to see if anyone there has a better idea.
Newbie
 
Join Date: Oct 2009
Location: USA
Posts: 7
#7: Oct 7 '09

re: How can I use print handler to pring a file?


Thanks everybody. I solved like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Visual C++ Copy Code 
  3. #using <System.dll>
  4. #using <System.Windows.Forms.dll>
  5. #using <System.Drawing.dll>
  6.  
  7. using namespace System;
  8. using namespace System::IO;
  9. using namespace System::Drawing;
  10. using namespace System::Drawing::Printing;
  11. using namespace System::Windows::Forms;
  12.  
  13. public ref class PrintingExample: public System::
  14. Windows::Forms::Form
  15. {
  16. private:
  17.    System::ComponentModel::Container^ components;
  18.    System::Windows::Forms::Button^ printButton;
  19.    System::Drawing::Font^ printFont;
  20.    StreamReader^ streamToPrint;
  21.  
  22. public:
  23.    PrintingExample()
  24.       : Form()
  25.    {
  26.  
  27.       // The Windows Forms Designer requires the following call.
  28.       InitializeComponent();
  29.    }
  30.  
  31.  
  32. private:
  33.  
  34.    // The Click event is raised when the user clicks the Print button.
  35.    void printButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
  36.    {
  37.       try
  38.       {
  39.          streamToPrint = gcnew StreamReader( "C:\\My Documents\\MyFile.txt" );
  40.          try
  41.          {
  42.             printFont = gcnew System::Drawing::Font( "Arial",10 );
  43.             PrintDocument^ pd = gcnew PrintDocument;
  44.             pd->PrintPage += gcnew PrintPageEventHandler( this, &PrintingExample::pd_PrintPage );
  45.             pd->Print();
  46.          }
  47.          finally
  48.          {
  49.             streamToPrint->Close();
  50.          }
  51.  
  52.       }
  53.       catch ( Exception^ ex ) 
  54.       {
  55.          MessageBox::Show( ex->Message );
  56.       }
  57.  
  58.    }
  59.  
  60.  
  61.    // The PrintPage event is raised for each page to be printed.
  62.    void pd_PrintPage( Object^ /*sender*/, PrintPageEventArgs^ ev )
  63.    {
  64.       float linesPerPage = 0;
  65.       float yPos = 0;
  66.       int count = 0;
  67.       float leftMargin = (float)ev->MarginBounds.Left;
  68.       float topMargin = (float)ev->MarginBounds.Top;
  69.       String^ line = nullptr;
  70.  
  71.       // Calculate the number of lines per page.
  72.       linesPerPage = ev->MarginBounds.Height / printFont->GetHeight( ev->Graphics );
  73.  
  74.       // Print each line of the file.
  75.       while ( count < linesPerPage && ((line = streamToPrint->
  76. ReadLine()) != nullptr) )
  77.       {
  78.          yPos = topMargin + (count * printFont->GetHeight
  79. ( ev->Graphics ));
  80.          ev->Graphics->DrawString( line, printFont, Brushes::
  81. Black, leftMargin, yPos, gcnew StringFormat );
  82.          count++;
  83.       }
  84.  
  85.  
  86.       // If more lines exist, print another page.
  87.       if ( line != nullptr )
  88.             ev->HasMorePages = true;
  89.       else
  90.             ev->HasMorePages = false;
  91.    }
  92.  
  93.  
  94.    // The Windows Forms Designer requires the following procedure.
  95.    void InitializeComponent()
  96.    {
  97.       this->components = gcnew System::ComponentModel::Container;
  98.       this->printButton = gcnew System::Windows::Forms::Button;
  99.       this->ClientSize = System::Drawing::Size( 504, 381 );
  100.       this->Text = "Print Example";
  101.       printButton->ImageAlign = System::Drawing::ContentAlignment::
  102. MiddleLeft;
  103.       printButton->Location = System::Drawing::Point( 32, 110 );
  104.       printButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
  105.       printButton->TabIndex = 0;
  106.       printButton->Text = "Print the file.";
  107.       printButton->Size = System::Drawing::Size( 136, 40 );
  108.       printButton->Click += gcnew System::EventHandler( this, &PrintingExample::printButton_Click );
  109.       this->Controls->Add( printButton );
  110.    }
  111.  
  112. };
  113.  
  114.  
  115. // This is the main entry point for the application.
  116. int main()
  117. {
  118.    Application::Run( gcnew PrintingExample );
  119. }
  120.  
  121.  
Newbie
 
Join Date: Oct 2009
Location: USA
Posts: 7
#8: Oct 11 '09

re: How can I use print handler to pring a file?


From the code mentioned; I was thinking the printing problem is solved as soon as the printer started to print. Unfortunately, although it worked well, but I found that the printer will print without writing the empty space. For example if the file to be printed has the following line:

1 2 3

It will print it as:
123.

Can anybody tell me how to fix that?
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#9: Oct 11 '09

re: How can I use print handler to pring a file?


Counts line your variable count is being incremented for blank lines. That being the case you may want to check the documentation for streamToPrint->ReadLine() and see if it returns blank lines to the program.
Newbie
 
Join Date: Oct 2009
Location: USA
Posts: 7
#10: Oct 11 '09

re: How can I use print handler to pring a file?


Hi Banfa,

Thanks for your continual support.
I would appreciate if you could indulge me with a little of your details.
Reply