C#調(diào)用打印機(jī)實(shí)現(xiàn)打印
更新時(shí)間:2022年04月29日 11:41:33 作者:農(nóng)碼一生
這篇文章介紹了C#調(diào)用打印機(jī)實(shí)現(xiàn)打印的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
一、引用BarcodeStandard.dll
#region BarcodeStandard.dll
/*
*
* 使用說(shuō)明
需要通過(guò)NuGet進(jìn)行安裝BarcodeLib.dll,必不可少
*/
string inputString;
/// <summary>
/// 獲取所以打印機(jī)驅(qū)動(dòng)名稱(chēng)
/// </summary>
private void getPrintDocumentlist()
{
PrintDocument print = new PrintDocument();
string sDefault = print.PrinterSettings.PrinterName;//默認(rèn)打印機(jī)名
comboBox_drive.Items.Add(sDefault);
comboBox_drive.Text = sDefault;//顯示默認(rèn)驅(qū)動(dòng)名稱(chēng)
foreach (string sPrint in PrinterSettings.InstalledPrinters)//獲取所有打印機(jī)名稱(chēng)
{
if (sPrint != sDefault)
{
comboBox_drive.Items.Add(sPrint);
}
}
}
/// <summary>
/// 打印繪制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Font titleFont = new Font("宋體", 9, FontStyle.Bold);//標(biāo)題字體
Font fntTxt = new Font("宋體", 9, FontStyle.Regular);//正文文字
Brush brush = new SolidBrush(Color.Black);//畫(huà)刷
Pen pen = new Pen(Color.Black); //線(xiàn)條顏色
Point po = new Point(10, 10);
try
{
//畫(huà)String
e.Graphics.DrawString(GetPrintSW().ToString(), titleFont, brush, po);//打印內(nèi)容
//畫(huà)橫線(xiàn)
//Point[] point = { new Point(20, 50), new Point(200, 50) };//縱坐標(biāo)不變
//e.Graphics.DrawLines(pen, point);
//畫(huà)豎線(xiàn)
//Point[] points1 = { new Point(60, 70), new Point(60, 70 + 40) };//橫坐標(biāo)不變
//e.Graphics.DrawLines(pen, points1);
//畫(huà)矩形
//e.Graphics.DrawRectangle(pen, 20, 70, 90, 90);
}
catch (Exception ex)
{
MessageBox.Show(this, "打印出錯(cuò)!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
/// 獲取打印內(nèi)容
/// </summary>
/// <returns></returns>
public StringBuilder GetPrintSW()
{
StringBuilder sb = new StringBuilder();
string tou = "XXXXXX科技有限公司";
string address = "安徽省合肥市瑤海區(qū)";
string saleID = "100010000001"; //單號(hào)
string item = "項(xiàng)目";
decimal price = 25.00M;
int count = 5;
decimal total = 0.00M;
decimal fukuan = 500.00M;
sb.AppendLine(" " + tou + " \n");
sb.AppendLine("-----------------------------------------");
sb.AppendLine("日期:" + DateTime.Now.ToShortDateString() + " " + "單號(hào):" + saleID);
sb.AppendLine("-----------------------------------------");
sb.AppendLine("項(xiàng)目" + " " + "數(shù)量" + " " + "單價(jià)" + " " + "小計(jì)");
for (int i = 0; i < count; i++)
{
decimal xiaoji = (i + 1) * price;
sb.AppendLine(item + (i + 1) + " " + (i + 1) + " " + price + " " + xiaoji);
total += xiaoji;
}
sb.AppendLine("-----------------------------------------");
sb.AppendLine("數(shù)量:" + count + " 合計(jì): " + total);
sb.AppendLine("付款:" + fukuan);
sb.AppendLine("現(xiàn)金找零:" + (fukuan - total));
sb.AppendLine("-----------------------------------------");
sb.AppendLine("地址:" + address + "");
sb.AppendLine("電話(huà):130000000000");
sb.AppendLine("謝謝惠顧歡迎下次光臨!");
sb.AppendLine("-----------------------------------------");
return sb;
}
/// <summary>
/// 生成條形碼
/// </summary>
/// <param name="content">內(nèi)容</param>
/// <returns></returns>
public static Image GenerateBarCodeBitmap(string content)
{
using (var barcode = new Barcode()
{
IncludeLabel = true,
Alignment = AlignmentPositions.CENTER,
Width = 250,
Height = 100,
RotateFlipType = RotateFlipType.RotateNoneFlipNone,
BackColor = Color.White,
ForeColor = Color.Black,
})
{
return barcode.Encode(TYPE.CODE128B, content);
}
}
#endregion二、引用Seagull.BarTender.Print.dll
#region Seagull.BarTender.Print.dll
/// <summary>
/// 打印測(cè)試
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printbt_Click(object sender, EventArgs e)
{
string qd = comboBox_drive.Text;//下拉列表選擇的驅(qū)動(dòng)名稱(chēng)
var printDocument = new PrintDocument();
//指定打印機(jī)
printDocument.PrinterSettings.PrinterName = qd;//驅(qū)動(dòng)名稱(chēng)
printDocument.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
try
{
//打印預(yù)覽
//PrintPreviewDialog ppd = new PrintPreviewDialog();
//ppd.Document = printDocument;
//ppd.ShowDialog();
//打印
printDocument.Print();
}
catch (InvalidPrinterException)
{
}
finally
{
printDocument.Dispose();
}
}
/// <summary>
/// BarTender打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BarTender_Click(object sender, EventArgs e)
{
try
{
//程序中寫(xiě)入引用 using Seagull.BarTender.Print.dll,必不可少;
//安裝Bartender后,在安裝的根目錄或者system32下課可找到對(duì)應(yīng)的dll
#region
Engine btEngine = new Engine();
btEngine.Start();
string lj = AppDomain.CurrentDomain.BaseDirectory + "test.btw"; //test.btw是BT的模板
LabelFormatDocument btFormat = btEngine.Documents.Open(lj);
//對(duì)BTW模版相應(yīng)字段進(jìn)行賦值
btFormat.SubStrings["name"].Value ="Liming";
btFormat.SubStrings["code"].Value = "1234567890";
//指定打印機(jī)名
btFormat.PrintSetup.PrinterName = "WPS 虛擬打印機(jī)";
//改變標(biāo)簽打印數(shù)份連載
btFormat.PrintSetup.NumberOfSerializedLabels = 1;
//打印份數(shù)
btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
Messages messages;
int waitout = 10000; // 10秒 超時(shí)
Result nResult1 = btFormat.Print("標(biāo)簽打印軟件", waitout, out messages);
btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
//不保存對(duì)打開(kāi)模板的修改
btFormat.Close(Seagull.BarTender.Print.SaveOptions.DoNotSaveChanges);
//結(jié)束打印引擎
btEngine.Stop();
#endregion
}
catch (Exception ex)
{
MessageBox.Show("錯(cuò)誤信息: " + ex.Message);
return;
}
}
#endregion三、引用 Interop.LabelManager2.dll
#region Interop.LabelManager2.dll
/// <summary>
/// 打印功能 CodeSoft
/// </summary>
/// <param name="PrintParam1">打印模板參數(shù)值1</param>
/// <param name="PrintParam2">打印模板參數(shù)值2</param>
/// <param name="PrintParam3">打印模板參數(shù)值3</param>
/// <param name="PrintParam4">打印模板參數(shù)值4</param>
/// <returns></returns>
public bool SoftCodePrint(string PrintParam1 = "", string PrintParam2 = "", string PrintParam3 = "", string PrintParam4 = "")
{
bool result = false;
int printNum = 2;//打印份數(shù)
try
{
string text = string.Empty;
ApplicationClass labApp = null;
Document doc = null;
string labFileName = AppDomain.CurrentDomain.BaseDirectory + "Template\\" + "Test.Lab";//模板地址
if (!File.Exists(labFileName))
{
throw new Exception("沒(méi)有找到標(biāo)簽?zāi)0?);
}
for (int i = 0; i < printNum; i++)
{
labApp = new ApplicationClass();
labApp.Documents.Open(labFileName, false);// 調(diào)用設(shè)計(jì)好的label文件
doc = labApp.ActiveDocument;
//可通過(guò)配置檔進(jìn)行配置打印信息
doc.Variables.FreeVariables.Item("模板變量名稱(chēng)1").Value = PrintParam1;
doc.Variables.FreeVariables.Item("模板變量名稱(chēng)2").Value = PrintParam2;
doc.Variables.FreeVariables.Item("模板變量名稱(chēng)3").Value = PrintParam3;
doc.Variables.FreeVariables.Item("模板變量名稱(chēng)4").Value = PrintParam4;
doc.PrintDocument(1);
}
labApp.Quit();
result = true;
}
catch (Exception ex)
{
}
return result;
}
#endregion到此這篇關(guān)于C#調(diào)用打印機(jī)實(shí)現(xiàn)打印的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 以MDF文件鏈接數(shù)據(jù)庫(kù)的示例代碼
本篇文章主要介紹了C# 以MDF文件鏈接數(shù)據(jù)庫(kù)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
C#獲取所有SQL Server數(shù)據(jù)庫(kù)名稱(chēng)的方法
這篇文章主要介紹了C#獲取所有SQL Server數(shù)據(jù)庫(kù)名稱(chēng)的方法,涉及C#針對(duì)sql server數(shù)據(jù)庫(kù)的簡(jiǎn)單查詢(xún)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C# List中FindAll用法的一些簡(jiǎn)單示例
本篇文章只要是對(duì)C# List中FindAll用法的一些簡(jiǎn)單示例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
Unity實(shí)現(xiàn)鼠標(biāo)拖動(dòng)3D物體
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)鼠標(biāo)拖動(dòng)3D物體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
.net(c#)中的new關(guān)鍵字詳細(xì)介紹
在 C# 中,new 關(guān)鍵字可用作運(yùn)算符、修飾符或約束2013-10-10
c# 實(shí)現(xiàn)網(wǎng)頁(yè)加載后將頁(yè)面截取為長(zhǎng)圖片
這篇文章主要介紹了c# 實(shí)現(xiàn)網(wǎng)頁(yè)加載后將頁(yè)面截取為長(zhǎng)圖片的方法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2021-01-01

