将 txt 文件直接打印到打印机 aspx.net
print txt file direct to printer aspx.net
我正在使用以下方法创建 txt 文件,现在我不知道如何将此文件直接发送到 printer.I 希望当用户单击 button_click 事件时,txt 文件到无需打印预览即可打印...etc.application 已部署网络
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);
}
}
}
}
是的,有可能...
首先你在你需要使用的project.After中添加Reference 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
只是为了创建字体,如果需要,您可以使用其他自定义字体。
我正在使用以下方法创建 txt 文件,现在我不知道如何将此文件直接发送到 printer.I 希望当用户单击 button_click 事件时,txt 文件到无需打印预览即可打印...etc.application 已部署网络
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);
}
}
}
}
是的,有可能...
首先你在你需要使用的project.After中添加Reference 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
只是为了创建字体,如果需要,您可以使用其他自定义字体。