我有这个要求,我必须从Web应用程序打印收据。我找到了在String中获取数据,生成临时文本文件,将其发送到打印机并删除文件的方法。一切都很好,除了打印部分,它什么都不产生,一个白色的文件!我已经检查了我的临时文件,然后将它们发送到打印并且那里有数据,也许我正在以错误的方式发送文件或忘记添加PrintPageEventHandler
(我不太确定这个最后一个,或者我可能只是不知道。)
这就是我所做的:
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!
}
请告诉我,如果还有其他办法,我只需要查看打印的数据。注意:我没有使用白色的前色或类似的东西,打印机可以接收1 MB的文本,只打印1页,里面没有任何内容。
答案 0 :(得分:3)
您需要添加一个打印页面事件处理程序,将您的数据输出到打印机
如果strFileName
是包含数据的文件名,则
我认为这个example in MSDN恰好符合
如果您查看MSDN中有关DocumentName属性的文档,您将找到此声明
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.
答案 1 :(得分:1)
试试这个:
private void button1_Click(object sender, EventArgs e)
{
System.IO.StreamReader filetoprint;
filetoprint = new System.IO.StreamReader(@"D:\\m.txt");
printDocument1.Print();
filetoprint.Close();
}