printer does not print white space

  • Thread starter Thread starter Allen
  • Start date Start date
Allen --
I went to the location below (as Mike advised) to solve the printing
problem"

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx


It worked well, but I found that the printer will print without writing
the empty space. For example if the file to be printed shows has the
following line:

1 2 3

it will print it as:
123.

Can anybody tell me how to fix that?

Kindly show us relevant parts of the code you are using to place the
text on the 'PrintDocument'.
 
Here is the 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 );
}
 
Allen --
Here is the code:
[...]
ev->Graphics->DrawString( line, printFont, Brushes::Black,
leftMargin, yPos, gcnew StringFormat );

It seems that you are printing the lines read from the file directly
without further processing. If the items in the line are separated by
tabs, you may want to adjust the 'StringFormat' you are passing to
'DrawString' as shown in this article:

How to: Set Tab Stops in Drawn Text
<URL:http://msdn.microsoft.com/en-us/library/2c6tzes6.aspx>
 
Hi Mike,

Thanks a lot for your help. Those spaces are made by quotations like that
"1 " "2"
and also like that "3" " " "4"
 
Allen said:
Hi Mike,

Thanks a lot for your help. Those spaces are made by quotations like
that "1 " "2"
and also like that "3" " " "4"

The reason I asked was the same as what Herfried pointed out.

Anyways, when I run your code in VC++ 2008 Express, I get exactly what
is in the file, with the quotes and spaces. Sorry, I don't see how you
would get the spaces missing with that code.
 
Hi Mike,

It looks like when I transferred the code to my program I might have messed
something.
 
Hi Herfried

Thanks for your link. The idea with the Tabs was new for me.
<code>
Dim LTabs As New List(Of Single)


For i As Integer = 0 To 100
LTabs.Add(i * 6)
Next
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
fmt.SetTabStops(0, LTabs.ToArray)

</code>
In my case, I only need a quick solution. This was what I have searched
for!
Thx
 
Back
Top