C#調(diào)用打印機(jī)實(shí)現(xiàn)打印
更新時間:2022年04月29日 11:41:33 作者:農(nóng)碼一生
這篇文章介紹了C#調(diào)用打印機(jī)實(shí)現(xiàn)打印的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、引用BarcodeStandard.dll
#region BarcodeStandard.dll
/*
*
* 使用說明
需要通過NuGet進(jìn)行安裝BarcodeLib.dll,必不可少
*/
string inputString;
/// <summary>
/// 獲取所以打印機(jī)驅(qū)動名稱
/// </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ū)動名稱
foreach (string sPrint in PrinterSettings.InstalledPrinters)//獲取所有打印機(jī)名稱
{
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);//畫刷
Pen pen = new Pen(Color.Black); //線條顏色
Point po = new Point(10, 10);
try
{
//畫String
e.Graphics.DrawString(GetPrintSW().ToString(), titleFont, brush, po);//打印內(nèi)容
//畫橫線
//Point[] point = { new Point(20, 50), new Point(200, 50) };//縱坐標(biāo)不變
//e.Graphics.DrawLines(pen, point);
//畫豎線
//Point[] points1 = { new Point(60, 70), new Point(60, 70 + 40) };//橫坐標(biāo)不變
//e.Graphics.DrawLines(pen, points1);
//畫矩形
//e.Graphics.DrawRectangle(pen, 20, 70, 90, 90);
}
catch (Exception ex)
{
MessageBox.Show(this, "打印出錯!", "提示", 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"; //單號
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() + " " + "單號:" + saleID);
sb.AppendLine("-----------------------------------------");
sb.AppendLine("項(xiàng)目" + " " + "數(shù)量" + " " + "單價" + " " + "小計(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("電話: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>
/// 打印測試
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printbt_Click(object sender, EventArgs e)
{
string qd = comboBox_drive.Text;//下拉列表選擇的驅(qū)動名稱
var printDocument = new PrintDocument();
//指定打印機(jī)
printDocument.PrinterSettings.PrinterName = qd;//驅(qū)動名稱
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
{
//程序中寫入引用 using Seagull.BarTender.Print.dll,必不可少;
//安裝Bartender后,在安裝的根目錄或者system32下課可找到對應(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);
//對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秒 超時
Result nResult1 = btFormat.Print("標(biāo)簽打印軟件", waitout, out messages);
btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
//不保存對打開模板的修改
btFormat.Close(Seagull.BarTender.Print.SaveOptions.DoNotSaveChanges);
//結(jié)束打印引擎
btEngine.Stop();
#endregion
}
catch (Exception ex)
{
MessageBox.Show("錯誤信息: " + 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("沒有找到標(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;
//可通過配置檔進(jìn)行配置打印信息
doc.Variables.FreeVariables.Item("模板變量名稱1").Value = PrintParam1;
doc.Variables.FreeVariables.Item("模板變量名稱2").Value = PrintParam2;
doc.Variables.FreeVariables.Item("模板變量名稱3").Value = PrintParam3;
doc.Variables.FreeVariables.Item("模板變量名稱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)打印的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法
這篇文章主要介紹了C#獲取所有SQL Server數(shù)據(jù)庫名稱的方法,涉及C#針對sql server數(shù)據(jù)庫的簡單查詢技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
Unity實(shí)現(xiàn)鼠標(biāo)拖動3D物體
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)鼠標(biāo)拖動3D物體,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10
.net(c#)中的new關(guān)鍵字詳細(xì)介紹
在 C# 中,new 關(guān)鍵字可用作運(yùn)算符、修飾符或約束2013-10-10
c# 實(shí)現(xiàn)網(wǎng)頁加載后將頁面截取為長圖片
這篇文章主要介紹了c# 實(shí)現(xiàn)網(wǎng)頁加載后將頁面截取為長圖片的方法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2021-01-01

