我有这个要求,我必须从 Web 应用程序打印收据。我找到了在字符串中获取数据、生成临时文本文件、将其发送到打印机并删除文件的方法。一切都很好,除了打印部分,它什么也没产生,一个白色的文档!我在发送临时文件进行打印之前检查了我的临时文件并且那里有数据,也许我以错误的方式发送文件或忘记添加一个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 页,其中没有任何内容。