【问题标题】:Send file to printer, nothing is printed将文件发送到打印机,不打印任何内容
【发布时间】:2012-07-23 19:32:16
【问题描述】:

我有这个要求,我必须从 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 页,其中没有任何内容。

【问题讨论】:

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

标签: c# asp.net printing


【解决方案1】:

您需要添加一个打印页面事件处理程序,将您的数据输出到打印机。
如果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.

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2011-08-03
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      相关资源
      最近更新 更多