How can I print a file

A

Allen

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 appreciate 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)
{




}

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 text.

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);
}
 
M

Morten Wennevik [C# MVP]

Hi Allen,

You need to do something similar as you do with the image file. Load it
into memory and draw it onto the PrintPage.

To draw the text you can use

string text = File.ReadAllText("ReadMe.txt");
gr.DrawString(text, new Font("Arial", 10), Brushes.Black, 0, 0);
 
A

Allen

Hi Morten,
I got the error (1) below. Then, I did a little adjustment and then got
error (2) below. Thanks any way:


(1)
1>
c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll : see
declaration of 'System::Windows::Forms::Control'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(248) : warning C4832: token
'.' is illegal after UDT 'System::IO::File'
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see
declaration of 'System::IO::File'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(248) : error C2275:
'System::IO::File' : illegal use of this type as an expression
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see
declaration of 'System::IO::File'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2065: 'gr' :
undeclared identifier
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2228: left of
'.DrawString' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2248:
'System::Windows::Forms::Control::text' : cannot access private member
declared in class 'System::Windows::Forms::Control'
1> c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\AboutBox.h(23) : see declaration of
'System::Windows::Forms::Control::text'
1>
c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll : see
declaration of 'System::Windows::Forms::Control'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2061: syntax
error : identifier 'Font'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : warning C4832: token
'.' is illegal after UDT 'System::Drawing::Brushes'
1> c:\windows\microsoft.net\framework\v2.0.50727\system.drawing.dll :
see declaration of 'System::Drawing::Brushes'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2143: syntax
error : missing ';' before ')'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(249) : error C2143: syntax
error : missing ';' before ')'
1>Build log was saved at "file://c:\Users\Allen\Documents\Visual Studio
2008\Projects\TimeTracking\TimeTracking\Debug\BuildLog.htm"
1>TimeTracking - 10 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

(2)

rted: Project: TimeTracking, Configuration: Debug Win32 ------
1>Compiling...
1>TimeTracking.cpp
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2228: left of
'.DrawString' must have class/struct/union
1> type is 'System::Drawing::Graphics ^'
1> did you intend to use '->' instead?
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2248:
'System::Windows::Forms::Control::text' : cannot access private member
declared in class 'System::Windows::Forms::Control'
1> c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\AboutBox.h(23) : see declaration of
'System::Windows::Forms::Control::text'
1>
c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll : see
declaration of 'System::Windows::Forms::Control'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2061: syntax
error : identifier 'Font'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : warning C4832: token
'.' is illegal after UDT 'System::Drawing::Brushes'
1> c:\windows\microsoft.net\framework\v2.0.50727\system.drawing.dll :
see declaration of 'System::Drawing::Brushes'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2143: syntax
error : missing ';' before ')'
1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Form1.h(246) : error C2143: syntax
error : missing ';' before ')'
1>Build log was saved at "file://c:\Users\Allen\Documents\Visual Studio
2008\Projects\TimeTracking\TimeTracking\Debug\BuildLog.htm"
1>TimeTracking - 5 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


--
Thanks
Allen
Morten Wennevik said:
Hi Allen,

You need to do something similar as you do with the image file. Load it
into memory and draw it onto the PrintPage.

To draw the text you can use

string text = File.ReadAllText("ReadMe.txt");
gr.DrawString(text, new Font("Arial", 10), Brushes.Black, 0, 0);

--
Happy Coding!
Morten Wennevik [C# MVP]


Allen said:
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 appreciate 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)
{




}

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 text.

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top