[英]print txt file direct to printer aspx.net
i'm using following method to create txt file, and now I don't know how to send this file direct to printer.I want when user click on button_click event, txt file to be printed without print preview...etc.application is web deployed 我正在使用以下方法创建txt文件,但现在我不知道如何直接将此文件发送到打印机。我希望当用户单击button_click事件时,无需打印预览即可打印txt文件...等是网络部署的
public void Print()
{
string path = @"c:\Restaurant\kitchen.txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
if (Label53.Text == "4")
{
String s = Label47.Text;
s = String.Format(s.Replace("S", "С").Replace("U", "У").Replace("P", "П").Replace("A", "А"));
sw.WriteLine(s);
}
}
}
}
Yes it is possible... 对的,这是可能的...
First you to add Reference System.Drawing
to the project.After that you need to use: 首先您将Reference
System.Drawing
添加到项目中,之后您需要使用:
using System.Drawing;
using System.Drawing.Printing;
I will show it for console application, you can convert it to asp.net easily. 我将为控制台应用程序显示它,您可以轻松将其转换为asp.net。
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr GetStockObject(int fnObject);
private static string stringToPrint;
static void Main(string[] args)
{
PrintDocument doc = new PrintDocument();
ReadFile(doc);
doc.PrintPage += doc_PrintPage;
doc.Print();
}
private static void ReadFile(PrintDocument printDocument1)
{
string docName = "Test.txt";
string docPath = @"c:\";
printDocument1.DocumentName = docName;
using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
stringToPrint = reader.ReadToEnd();
}
}
static void doc_PrintPage(object sender, PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, Font.FromHfont(GetStockObject(0)),
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, Font.FromHfont(GetStockObject(0)), Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > 0);
}
This was the code. 这是代码。 I'm using
GetStockObject
just to create Font you can use another custom Font, if you want. 我使用
GetStockObject
只是为了创建Font,可以根据需要使用另一个自定义Font。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.