Reputation: 85779
I have this requirement where I must print a receipt from a Web Application. I've found the way to get the data in a String, generate a temporary text file, send it to the printer and delete the file. Everything is good except the print part, it generates nothing, a white document! I've checked my temp files before sending them to print and there is data there, maybe I'm sending the file in the wrong way or forgot to add a PrintPageEventHandler
(I'm not so sure about this last one, or maybe I just don't know).
This is s what I've done:
private void printTextfile(String strFileName)
{
//there is this standar that makes programmers declare variables at the
//beginning of the method and this "obj" and "str" things...
PrintDocument objPrintDocument =null;
String strPrinterName = string.Empty;
//getting the printer name from web.config
strPrinterName= ConfigurationManager.AppSettings["NOMBRE_IMPRESORA"];
objPrintDocument = new PrintDocument();
objPrintDocument.PrinterSettings.PrinterName = strPrinterName;
//the temp text file created and with data
objPrintDocument.DocumentName = strFileName;
//I guess don't need this because I've setted up the file name (it is reachable)
//objPrintDocument.PrintPage += new PrintPageEventHandler(this.printTextFileHandler);
//send the file to the printer (this works)
objPrintDocument.Print();
//ok, now I've check my physic file and it has nothing in it!
}
Please tell me if there is another way to do it, I just need to see the data printed. Note: I'm not using white forecolor or something like that, the printer can receive like 1 MB text and just print 1 page with nothing in it.
Upvotes: 2
Views: 8350
Reputation: 11
Try this:
private void button1_Click(object sender, EventArgs e)
{
System.IO.StreamReader filetoprint;
filetoprint = new System.IO.StreamReader(@"D:\\m.txt");
printDocument1.Print();
filetoprint.Close();
}
Upvotes: 1
Reputation: 216293
You need to add a print page event handler that outputs your data to the printer.
If strFileName
is the file name containing the data, then
I think that this example in MSDN fits exactly
If you look at the documentation in MSDN about DocumentName property you will find this statement
The DocumentName property does not specify the file to print.
Rather, you specify the output to print by handling the PrintPage event.
For an example, see the PrintDocument class overview.
Upvotes: 3