C#调用把BarTender模板.docx
- 文档编号:27624550
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:21
- 大小:229.94KB
C#调用把BarTender模板.docx
《C#调用把BarTender模板.docx》由会员分享,可在线阅读,更多相关《C#调用把BarTender模板.docx(21页珍藏版)》请在冰豆网上搜索。
C#调用把BarTender模板
1、应用“Seagull.BarTender.Print“命名空间,
2、代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
//usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
usingSeagull.BarTender.Print;
//usingSeagull.BarTender;
namespaceBarTenderPrintTest1
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
//Bar
stringdc=@"D:
\liping\BartenderPrint\BarTenderPrintTest1\BarTenderPrintTest1\bin\Debug\HW_CaiHe_and_packbox.btw";
intff=163704004;
stringaa="00057616-";
inti=1;
privatevoidbtn_print_Click(objectsender,EventArgse)
{
PrintLabel3();
}
#region第一种写法
privatevoidPrintLabel1()
{
//创建一个BarTender打印引擎,并启用
Engineengine=newEngine(true);
//创建一个模板对象
//LabelFormatDocumentformat=engine.Documents.Open("c:
\\test.btw");
LabelFormatDocumentformat=engine.Documents.Open(@"D:
\liping\BartenderPrint\BarTenderPrintTest1\BarTenderPrintTest1\bin\Debug\HW_CaiHe_and_packbox.btw");
format.Print("printmelabel");
//format.Print("Selectprinter",outmessages);
//format.Close(SaveOptions.SaveChanges );
format.Print();
engine.Dispose();
}
#endregion
#region打印第二种方法
privatevoidPrintLable2()
{
using(Engineengine=newEngine())
{
//启用一个打印引擎
engine.Start();
//创建一个模板对象
LabelFormatDocumentformat=engine.Documents.Open(@"D:
\liping\BartenderPrint\BarTenderPrintTest1\BarTenderPrintTest1\bin\Debug\HW_CaiHe_and_packbox.btw");
//打印
//format.Print("printmelabel");
//改变标签打印数份连载
format.PrintSetup.NumberOfSerializedLabels=1;
//设定印标签打印数量
format.PrintSetup.IdenticalCopiesOfLabel=1;
ResultnResult=format.Print();
//指定打印机打印,不指定则使用默认打印机
format.PrintSetup.PrinterName="BarCodePrinterT-4503E";
//Displaytheprintresult.
//Console.WriteLine("Printstatus="+nResult);
MessageBox.Show("打印提示:
"+nResult);
//Closethecurrentformatwithoutsaving.
//SaveOptions有三个值DoNotSaveChanges:
不保存,PromptSave:
提示是否保存 SaveChanges:
保存
format.Close(SaveOptions.DoNotSaveChanges);
//结束打印引擎
engine.Stop();
}
}
#endregion
#region第三种打印方法改变标签的值
privatevoidPrintLabel3()
{
using(Engineengine=newEngine(true))
{
LabelFormatDocumentbtFormat=engine.Documents.Open(dc);
//MessageBox.Show(btFormat.SubStrings[0].Name);
////获取标签的值
//stringAddressSubstring=btFormat.SubStrings["HWbarcode1"].Value;
//MessageBox.Show(AddressSubstring);
//修改标签的值
//btFormat.SubStrings["Address"].Value="1313MockingbirdLane,Anywhere,USA";
//btFormat.SubStrings["Name"].Value="JohnDoe";
//btFormat.SubStrings["Quantity"].Value="10";
intdd=ff+i;
i++;
btFormat.SubStrings["HWbarcode1"].Value=aa+dd.ToString();
//改变标签打印数份连载
btFormat.PrintSetup.NumberOfSerializedLabels=1;
//设定印标签打印数量
btFormat.PrintSetup.IdenticalCopiesOfLabel=1;
ResultnResult=btFormat.Print();
//指定打印机打印,不指定则使用默认打印机
btFormat.PrintSetup.PrinterName="BarCodePrinterT-4503E";
MessageBox.Show("打印提示:
"+nResult);
btFormat.Close(SaveOptions.DoNotSaveChanges);
//结束打印引擎
engine.Stop();
}
}
#endregion
}
}
三、bartender说明文档部分
CreatingaBarTenderPrintEngine
TheEngineclassrepresentsaBarTenderprocessandprovidesthebackboneforprogrammingwiththeBarTenderPrintSDK.AllprogramswrittenwiththeBarTenderPrintSDKwillrelyontheEngineclasstoprovideBarTenderprintfunctionality.
TheBarTenderPrintEngine
TheBarTenderprocess(bartend.exe)providesstandardBarTenderfunctionality,suchasopeninglabelformats,changinglabelsettings,andprinting.TheBarTenderbackgroundprocessiscontrolledusinganinstanceoftheEngineclass.
TheRoleoftheEngineClass
TheEngineclasscontainsmanymethods,properties,andeventstoassistinprintingandcontrollingtheBarTenderPrintEngine.
FeaturesoftheEngineClass
TheEngineclassallowsusersto:
∙Start,stop,andrestartaBarTenderbackgroundprocess.
∙Open,access,andsavelabelformats.
∙UseEngine-leveleventstomonitorprinting.
∙ManagetheBarTenderApplicationwindow.
HowTo:
StartandStopanEngine
AnenginemustbecreatedandstartedinordertolaunchaBarTenderprocessandcommenceprinting.TheEngineclassprovidestheEngine.StopmethodtoexplicitlyshutdowntheBarTenderPrintEngine.Iftheengineisnotstopped,abartend.exeprocessmaybeleftrunninginthebackground.AftercallingtheEngine.Stopmethod,itisbestpracticetocalltheEngine.Disposemethod.TheDisposemethodensuresallnon-memoryresourcesareproperlyreleasedfortheclass;thisincludesshuttingdowntheBarTenderprocessifEngine.Stopwasnotsuccessfullycalled.
Thefollowingistheminimalcodenecessarytocreate,start,stop,anddisposeanEngineobject:
InC#:
//Callingconstructorwith'true'automaticallystartsengine.
using(EnginebtEngine=newEngine(true))
{
//Dosomethingwiththeengine.
//StoptheBarTenderprocess.
btEngine.Stop();
}
InVB:
'Callingconstructorwith'true'automaticallystartsengine.
UsingbtEngineAsNewEngine(True)
'Dosomethingwiththeengine.
'StoptheBarTenderprocess.
btEngine.Stop()
EndUsing
Intheaboveexample,anengineiscreatedandstartedimplicitlybypassing'true'asanargumenttotheconstructor.TheengineisthenstoppedbycallingtheEngine.Stopmethod.Thisterminatesthebackgroundbartend.exeprocess.Finally,Engine.Disposeiscalledautomaticallywhenexecutionleavesthe'using'statement,releasingallEngineresources.
ItisalsopossibletostarttheengineexplicitlyafterithasbeencreatedusingthedefaultEngineconstructorandtheEngine.Startmethod.Bydefault,Engine.Stopwillcloseallopenformatswithoutsaving,butanoverloadedversionisprovidedthatallowsmanualcontrol.
Thefollowingexampleshowsalternativecodeforstartingandstoppinganengineandsavingchanges:
InC#:
using(EnginebtEngine=newEngine())
{
//Applicationspecificcode
//Explicitlystarttheengine
btEngine.Start();
//Application-specificcode
//Assumingtheapplicationwantstosavechanges,
//itcanbeeasilydoneatStoptime.
btEngine.Stop(SaveOptions.SaveChanges);
}
InVB:
UsingbtEngineAsNewEngine(True)
'Applicationspecificcode
'Explicitlystarttheengine
btEngine.Start()
'Application-specificcode
'Assumingtheapplicationwantstosavechanges,
'itcanbeeasilydoneatStoptime.
btEngine.Stop(SaveOptions.SaveChanges)
EndUsing
Intheaboveexample,anewEngineiscreated,butnotstarteduntillater.Someapplicationactivityisassumedtoexecute,thentheStopmethodiscalled.Inthiscase,changestolabelsdonewhileusingtheenginearesavedbacktofile.TheSaveOptionsenumerationspecifiestheoperationconcerningopenlabelformatstoperformduringexitofBarTender.
Intheaboveexamplesandmanyotherexamplesinthisdocument,theEngine.Disposemethodiscalledimplicitlybya'using'statement.Whileitisnotalwaysappropriatetoutilize'using',itisaconvenientwaytoensureDisposeiscalledeveniftheblockisexitedduringanexception.
HowTo:
UseEngineasaFieldinaClass
Theaboveexamples,andmostexamplesinthisdocument,presentuseofanEngineinstanceinasinglemethod.Thisapproachisnotpracticalformostrealapplications.StartingandstoppingEngineobjects,andbyextensionBarTenderprocesses,shouldbedoneasrarelyaspossibleforoptimalperformance.Ideally,Engineinstancesshouldbestartedonceandonlystoppedattheendoftheapplicationtominimizetheoverheadofmanagingprocesses.Themoststraightforwardobject-orientedapproachismakeanEngineobjectafieldinaclassandallowtheencapsulatingclasstodeterminetheEngineobject'slifetime.
ThefollowingistheminimalsuggestedcodeformakinganEngineobjectafieldinaclass:
InC#:
publicclassEngineWrapper:
IDisposable
{
//EngineField
privateEnginem_engine=null;
//Thispropertywillcreateandstarttheenginethefirsttimeitis
//called.Mostmethodsinthisclass(andmethodsinchildclasses)
//shouldutilizethispropertyinsteadofthem_enginefield.
protectedEngineBtEngine
{
get
{
//Iftheenginehasnotbeencreatedyet,createandstartit.
if(m_engine==null)
{
m_engine=newEngine(true);
}
returnm_engine;
}
}
//ImplementIDisposable
publicvoidDispose()
{
//Theengineonlyneedstobestoppedanddisposedifitwas
//created.Usethefieldhere,nottheproperty.Otherwise,
//youmightcreateanewinstanceintheDisposemethod!
if(m_engine!
=null)
{
//St
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 调用 BarTender 模板
![提示](https://static.bdocx.com/images/bang_tan.gif)