blah blah blah is here! blah blah » Close

up1down
link

I thought that C# printing is straightforward.. but when I try it, it does not work. where did I go wrong? Upon click of the button, it is able to feed in the paper in the printer, but out come the paper is blank. When I open the test.txt file to print directly, it has no problem. Thanks.

private void Print_File_button_Click(object sender, EventArgs e)
{
System.IO.StreamReader fileToPrint;
System.Drawing.Font printFont;
fileToPrint = new System.IO.StreamReader(@"D:\test.txt");
printFont = new System.Drawing.Font("Arial", 10);
printDocument1.Print();
fileToPrint.Close();
}

last answered one year ago

2 answers

link

I'm afraid it isn't that simple - you have to actually draw the contents of the file on the page(s) to be printed. This is done by handling the PrintDocument's PrintPage event.

Check out the example on MSDN for how to do this.

Notice that you'll need to declare your StreamReader and Font variables at class level (i.e. as private fields) rather than as local variables so that the PrintPage eventhandler will be able to access them.

up0down
link

hi Vulpes, thank you for linking me to the relevant MSDN. My printing works now. However, on the file receipt.txt, the $ signs of each individual item appears as in the same column as straight vertical line which is what I want, but the printout of $ signs appears as being NOT in the same column, as seen below:

whether I am using each of the below line, the $ signs cannot appear to be in the same straight vertical column, although $ signs in the receipt.txt file appears to be in the same straight vertical column.
foreach (object item in listBox1.Items)
foreach (object item in PurchasedItems)


25/2/2010 10:34:32 AM

California Raisin Bread $0.05
Fruit and Nut Bread $0.10
Corn Bread $0.15

Total Cost = $0.30
Total Items = 3

Is there something which I miss out? Code as in beloiw:

private void Receipt_button_Click(object sender, EventArgs e)
{
SelectedItems_Form SelectedItems = (SelectedItems_Form)Application.OpenForms["SelectedItems_Form"];
ListBox listBox1 = (ListBox)SelectedItems.Controls["listBox1"];
TextBox total_items = (TextBox)SelectedItems.Controls["total_items"];

// save to textfile
StreamWriter sw = new StreamWriter(@"D:\receipt.txt", false); // false=not append
sw.WriteLine(DateTime.Now);
sw.WriteLine();

// Realign those items and prices in the Listbox1
string[] PurchasedItems = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
PurchasedItems[i] = listBox1.Items[i].ToString();
}

int maxLen = 0;
// work out maximum length of the items
foreach (string items in PurchasedItems)
{
if (items.Length > maxLen) maxLen = items.Length;
}
// fill out all items to maximum length by inserting spaces before the $ sign
for (int i = 0; i < PurchasedItems.Length; i++)
{
if (PurchasedItems[i].Length == maxLen) continue;
int index = PurchasedItems[i].IndexOf('$');
string extraSpaces = new string(' ', maxLen - PurchasedItems[i].Length);
PurchasedItems[i] = PurchasedItems[i].Insert(index, extraSpaces);
}
listBox1.Items.Clear();
listBox1.Items.AddRange(PurchasedItems);

// foreach (object item in listBox1.Items)
foreach (object item in PurchasedItems)
sw.WriteLine(item.ToString());
sw.WriteLine();
sw.Write("Total Cost = "); sw.WriteLine(TotalPurchase.Text);
sw.Write("Total Items = "); sw.WriteLine(total_items.Text);
sw.Close();

try
{
streamToPrint = new StreamReader("D:\\receipt.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.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.
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;
string line = null;

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

vulpes
17279

You'll need to use a non-proportional font to get things to line up. I'd try: printFont = new Font("Courier New", 9.75F);

hi Vulpes, it works OK now, thank you very much. I always look forward to receive good answers from you :)

Feedback