发送文件到打印机,没有打印 [英] Send file to printer, nothing is printed

查看:198
本文介绍了发送文件到打印机,没有打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的要求,我必须从Web应用程序打印收据。我发现的方式来获得数据的字符串,生成一个临时文本文件,将它发送到打印机,并删除该文件。一切都只是打印部分好,它会产生什么,一个白色的文件!我检查了我的临时文件发送它们进行打印之前,且有数据在那里,也许我发送文件中的错误的方式,或忘了加一个 PrintPageEventHandler (我M不太确定这个最后一个,也许我只是不知道)。

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).

这是是我所做的:

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页它

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.

推荐答案

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

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


  • 打开在code上面的文件保存的StreamReader在全球
    比如VAR。

  • 添加页面事件处理程序

  • 在退出关闭流

我觉得在这个MSDN 例如适合完全相同

I think that this example in MSDN fits exactly

如果你看一下关于<一个MSDN中的文档href=\"http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.documentname.aspx\"相对=nofollow> DocumentName 财产,你会发现这句话

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.

这篇关于发送文件到打印机,没有打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆