我正在使用以下方法创建txt文件,现在我不知道如何将此文件直接发送到打印机。我希望当用户点击button_click事件时,txt文件无需打印预览即可打印...等应用程序是Web部署的
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);
}
}
}
}
答案 0 :(得分:2)
是的,有可能......
首先,您要将参考System.Drawing
添加到项目中。之后您需要使用:
using System.Drawing;
using System.Drawing.Printing;
我将为控制台应用程序显示它,您可以轻松地将其转换为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);
}
这是代码。我只是使用GetStockObject
创建字体,您可以根据需要使用其他自定义字体。