C# 将文件发送到打印机,不打印任何内容

C# 将文件发送到打印机,不打印任何内容,c#,asp.net,printing,C#,Asp.net,Printing,我有这个要求,我必须打印一张来自Web应用程序的收据。我找到了以字符串形式获取数据、生成临时文本文件、将其发送到打印机并删除该文件的方法。除了打印部分,一切都很好,它不生成任何东西,一个白色文档!在将临时文件发送到打印之前,我已经检查了临时文件,其中有数据,可能是我发送文件的方式错误,或者忘记添加PrintPageEventHandler(我不太确定最后一个,或者我只是不知道) 这就是我所做的: private void printTextfile(String strFileName) {

我有这个要求,我必须打印一张来自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!
}

请告诉我是否有其他方法,我只需要查看打印的数据。注意:我没有使用白色前景色或类似的颜色,打印机可以接收大约1MB的文本,只打印一页而不打印任何内容。

您需要添加一个打印页事件处理程序,将数据输出到打印机。
如果
strFileName
是包含数据的文件名,则

  • 在上面的代码中打开文件,将streamreader保存在全局文件中 实例变量
  • 添加页面事件处理程序
  • 在出口处关闭小溪
我认为这正合适

如果您查看MSDN中有关属性的文档,您会发现此语句

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.

您需要添加一个打印页面事件处理程序,将数据输出到打印机。
如果
strFileName
是包含数据的文件名,则

  • 在上面的代码中打开文件,将streamreader保存在全局文件中 实例变量
  • 添加页面事件处理程序
  • 在出口处关闭小溪
我认为这正合适

如果您查看MSDN中有关属性的文档,您会发现此语句

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.
试试这个:

 private void button1_Click(object sender, EventArgs e)
        {
            System.IO.StreamReader filetoprint;
              filetoprint = new System.IO.StreamReader(@"D:\\m.txt");
              printDocument1.Print();
              filetoprint.Close();
        }
试试这个:

 private void button1_Click(object sender, EventArgs e)
        {
            System.IO.StreamReader filetoprint;
              filetoprint = new System.IO.StreamReader(@"D:\\m.txt");
              printDocument1.Print();
              filetoprint.Close();
        }

调试此程序时,是否返回strpInterName的值或空值..?向打印机发送数据的代码在哪里?您正在设置DocumentName,然后调用Print,但这不会打印任何内容。@DJKRAZE没有错误,或者打印机无法打印任何文件。调试时,是否会返回strpInterName的值或空值..?向打印机发送数据的代码在哪里?您正在设置DocumentName,然后调用Print,但这不会打印任何内容。@DJKRAZE没有错误,否则打印机无法打印任何文件。