I need to print a text file .When i print it ,it is not printed exactly form 0,0 coordiante of the page. I mean the top most left corner.It is leaving some margin from top and from left.The code is an example from MSDN.
I am using c# 2.0 and VS 2005.I have just chaged the X coordinate for the DrawString function.i am using it as 0 and the Y coordinate is calculated.
This is just a test application, as i have to take the printing logic futher to print business check images.With image also i was getting some space on the left and top side.
Can this space be controlled or it by default ?
When i am printing it to Microsoft Office Document Imaging or CutePDF Writer i am not getting any space on the left and top side.When i print it through printer i am getting the space from top and left.
I have given the X coordinate in the DrawString function as 0
and i am varying the Y coordinate.
Thanking you all in advance.
private void btnPrintImage_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader("C:\\work\\error.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
Margins margins = new Margins(0, 0, 0, 0);
RectangleF rect=pd.DefaultPageSettings.PrintableArea;
pd.DefaultPageSettings.Margins = margins;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
if (printDialog.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
leftMargin = 0;
topMargin = 0;
string line = null;
int left = ev.MarginBounds.Left;
// 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()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}