如何通过使用C#窗口服务通过打印机打印数据打印文本文件
问题描述:
如何通过打印机自动打印基于给定文件名的文本文件,而无需在C sharp窗口服务中执行任何手动手动工作,它不是为我工作,任何人给我建议。如何通过使用C#窗口服务通过打印机打印数据打印文本文件
using System.Management;
private Font printFont;
private StreamReader streamToPrint;
private void GetPrinters(string fileName)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
string printerName = "";
foreach (ManagementObject printer in searcher.Get())
{
printerName = printer["Name"].ToString().ToLower();
if (printerName.Equals(@"\\chenraqdc1.raqmiyat.local\hp laserjet black chennai"))
{
Console.WriteLine("Printer = " + printer["Name"]);
if (printer["WorkOffline"].ToString().ToLower().Equals("true"))
{
// printer is offline by user
label1.Text = "Your Plug-N-Play printer is not connected.";
}
else
{streamToPrint = new StreamReader(fileName);
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
streamToPrint.Close();
}
}
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height/
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
答
你可以做这样的事情......
注:这是示例代码如何使用C#窗口服务打印PDF文件,如果你要打印的文本文件,你可以更改此代码
带有按钮(cmdGetPrinters)和ListView的Windows窗体 (lstPrinters)。列表视图有两列定义 - “属性”和 “值”,它将描述安装在本地 机器上的打印机。下面的代码实现了魔术。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; System.Drawing;
using System.Text; System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;
public partial class frmPrintDisplay : Form
{
public frmPrintDisplay()
{
InitializeComponent();
}
private void cmdGetPrinters_Click(object sender, EventArgs e)
{
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mo = new ManagementObjectSearcher(query);
ManagementObjectCollection printers = mo.Get();
foreach (ManagementObject printer in printers)
{
PropertyDataCollection printerProperties = printer.Properties;
string printerPath = printer.Path.ToString() ;
PropertyDataCollection.PropertyDataEnumerator test =
printer.Properties.GetEnumerator();
while(! (test.MoveNext()== false))
{
lstPrinters.Items.Add(
new ListViewItem(new string[]
{
test.Current.Name,
(
(test.Current.Value == null) ?
"n/a" : test.Current.Value.ToString()
)
})
);
}
}
}
}
此图显示了这个小窗口应用程序的结果。请注意“名称”属性,这将用于将文本文件发送到打印机。这显示在打印机的“ShareName”属性下。在同一个域/工作组中的测试机器上,您必须安装新的网络打印机,并指出安装情况以查看第一台计算机上的共享打印机。这实际上使得第一台计算机成为了打印机服务器,并且您可以为客户端重新设置设置。
现在到测试机......应用上述code building
保存文本文件到一个临时目录名为C:\ Program Files文件[应用程序名称] \ TEMP \
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; System.Drawing;
using System.IO;
using System.Text; System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;
private void print(ref DirectoryInfo tempDir)
{
try
{
foreach(FileInfo file in tempDir.GetFiles("*.pdf"))
{
file.CopyTo("\\\\XYZ\\Phaser77\\" + file.Name);
}
}
catch(Exception ee){ }
}
我希望这将有助于你
我是否需要设置任何打印机设置,y它没有得到打印 – pravz
代码是不会抛出任何错误,pd.Print();系统来到这条线它显示一个小的对话框打印文件,但文件没有得到打印,任何方式善意地帮助如何禁用该打印对话框 – pravz