1.添加Windows服务项目
2.添加安装程序
在右键点击主界面,添加安装程序
3.添加组件
在工具箱拖动服务一个组件到主界面
4.主要实现代码
public bool SendTextFileToPrinter(string szFileName, string m_PrinterName, Log log, string sourcePath)
{
log.WriteLog("[" + m_PrinterName + "] 准备打印: " + szFileName);
using (PdfDocument doc = new PdfDocument())
{
doc.LoadFromFile(szFileName);
log.WriteLog(doc.Pages.Count.ToString());
//doc.PrintFromPage = 1;
//doc.PrintToPage = doc.Pages.Count;
doc.PrintSettings.PrinterName = m_PrinterName; //此行代码为选择打印机名称来打印
//毫米转百英寸
int printWidth = (int)(Math.Round(101 * 0.03937 * 100, 0));//394
int printHeight = (int)(Math.Round(152 * 0.03937 * 100, 0));//197
PaperSize paper = new PaperSize("123", printWidth, printHeight);
paper.RawKind = (int)PaperKind.Custom;
//设置打印的纸张大小
doc.PrintSettings.PaperSize = paper;
//需要选择FitSize打印模式 false为纵向打印
doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, false);
doc.Print();
}
log.WriteLog("[" + m_PrinterName + "] 打印成功!: "+ szFileName);
//删除打印成功的PDF
//if (File.Exists(szFileName))
// File.Delete(szFileName);
return true;
}
5.服务管理界面主要代码
1)初始化
private void FormMain_Load(object sender, EventArgs e)
{
try
{
m_Data = new DataSet();
m_SettingFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "TaskSettings.xml";
if (!File.Exists(m_SettingFile))
MakeTaskSettings(m_SettingFile);
m_Data.ReadXml(m_SettingFile);
if (m_Data == null || m_Data.Tables.Count < 1)
{
throw new Exception("No task settings found.");
}
this.dataGridView1.DataSource = m_Data;
this.dataGridView1.DataMember = m_Data.Tables[0].TableName;
if (m_Data.Tables[0].Rows.Count > 0)
dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
try
{
m_Service = new ServiceController("DamsonwarePrintServiceZpl");
if (m_Service.Status.Equals(ServiceControllerStatus.Running))
{
buttonStop.Enabled = true;
buttonStart.Enabled = false;
}
else
{
buttonStop.Enabled = false;
buttonStart.Enabled = true;
}
}
catch(Exception ee)
{
buttonStart.Enabled = false;
buttonStop.Enabled = false;
MessageBox.Show("Checking Service status have error: " + ee.Message);
}
}
2)保存代码
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
DataRow row;
if (m_New)
{
row = m_Data.Tables[0].NewRow();
m_Data.Tables[0].Rows.Add(row);
}
else
row = m_Data.Tables[0].Rows[this.dataGridView1.CurrentRow.Index];
string DataPath = textBoxDataPath.Text;
if (!System.IO.Directory.Exists(DataPath))
System.IO.Directory.CreateDirectory(DataPath);
//row["TaskName"] = textBoxJobName.Text;
row["LogFileName"] = textBoxLogFileName.Text;
//row["RunCycle"] = comboBoxRunCycle.Text;
//row["RepeatCycle"] = textBoxRepeatCycle.Text;
//row["JobStartTime"] = textBoxJobStartTime.Text;
//row["ErrorMailTo"] = textBoxErrorMailTo.Text;
//row["JobEnable"] = comboBoxJobEnable.Text;
row["PrinterName"] = textBoxPrinterName.Text;
row["DataPath"] = DataPath;
//row["ArchPath"] = textBoxArchPath.Text;
//row["FileFilter"] = textBoxFileFilter.Text;
m_Data.WriteXml(m_SettingFile);
this.groupBox1.Enabled = false;
this.buttonDelete.Enabled = true;
//this.buttonNew.Enabled = true;
this.buttonEdit.Enabled = true;
this.buttonSave.Enabled = false;
if (m_New)
{
dataGridView1.CurrentCell = dataGridView1.Rows[m_Data.Tables[0].Rows.Count - 1].Cells[0];
}
MessageBox.Show("Successfully saved!");
}
catch (Exception ex)
{
MessageBox.Show("Saving have error: " + ex.Message);
}
}
3)启动服务
private void buttonStart_Click(object sender, EventArgs e)
{
try
{
//m_Service.Start();
//ServiceController serviceController = new ServiceController("DamsonwarePrintServiceZpl");
//serviceController.Start();
//if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
//bat文件路径
string path = "启动服务并设置自动DamsonwarePrintServicePDF.bat";
Process pro = new Process();
FileInfo file = new FileInfo(path);
pro.StartInfo.WorkingDirectory = file.Directory.FullName;
pro.StartInfo.FileName = path;
pro.StartInfo.CreateNoWindow = false;
pro.Start();
pro.WaitForExit();
MessageBox.Show("PDF自动打印服务运行成功!");
buttonStop.Enabled = true;
buttonStart.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show("Trying to start service have error: " + ex.Message);
}
m_Service.Refresh();
}
效果图:
来源地址:C# 自动打印PDF快递面单WIN服务和winform管理界面
转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:www.88531.cn资享网,谢谢!^^
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END