C#调用把BarTender模板Word格式.docx
《C#调用把BarTender模板Word格式.docx》由会员分享,可在线阅读,更多相关《C#调用把BarTender模板Word格式.docx(16页珍藏版)》请在冰豆网上搜索。
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(@"
format.Print("
printmelabel"
//format.Print("
Selectprinter"
outmessages);
//format.Close(SaveOptions.SaveChanges
);
format.Print();
engine.Dispose();
#endregion
#region打印第二种方法
privatevoidPrintLable2()
using(Engineengine=newEngine())
//启用一个打印引擎
engine.Start();
//创建一个模板对象
//打印
//改变标签打印数份连载
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();
#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"
Name"
JohnDoe"
Quantity"
10"
intdd=ff+i;
i++;
btFormat.SubStrings["
].Value=aa+dd.ToString();
btFormat.PrintSetup.NumberOfSerializedLabels=1;
btFormat.PrintSetup.IdenticalCopiesOfLabel=1;
ResultnResult=btFormat.Print();
btFormat.PrintSetup.PrinterName="
btFormat.Close(SaveOptions.DoNotSaveChanges);
}
三、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'
UsingbtEngineAsNewEngine(True)
Dosomethingwiththeengine.
StoptheBarTenderprocess.
btEngine.Stop()
EndUsing
Intheaboveexample,anengineiscreatedandstartedimplicitlybypassing'
asanargumenttotheconstructor.TheengineisthenstoppedbycallingtheEngine.Stopmethod.Thisterminatesthebackgroundbartend.exeprocess.Finally,Engine.Disposeiscalledautomaticallywhenexecutionleavesthe'
using'
statement,releasingallEngineresources.
ItisalsopossibletostarttheengineexplicitlyafterithasbeencreatedusingthedefaultEngineconstructorandtheEngine.Startmethod.Bydefault,Engine.Stopwillcloseallopenformatswithoutsaving,butanoverloadedversionisprovidedthatallowsmanualcontrol.
Thefollowingexampleshowsalternativecodeforstartingandstoppinganengineandsavingchanges:
using(EnginebtEngine=newEngine())
//Applicationspecificcode
//Explicitlystarttheengine
btEngine.Start();
//Application-specificcode
//Assumingtheapplicationwantstosavechanges,
//itcanbeeasilydoneatStoptime.
btEngine.Stop(SaveOptions.SaveChanges);
Applicationspecificcode
Explicitlystarttheengine
btEngine.Start()
Application-specificcode
Assumingtheapplicationwantstosavechanges,
itcanbeeasilydoneatStoptime.
btEngine.Stop(SaveOptions.SaveChanges)
Intheaboveexample,anewEngineiscreated,butnotstarteduntillater.Someapplicationactivityisassumedtoexecute,thentheStopmethodiscalled.Inthiscase,changestolabelsdonewhileusingtheenginearesavedbacktofile.TheSaveOptionsenumerationspecifiestheoperationconcerningopenlabelformatstoperformduringexitofBarTender.
Intheaboveexamplesandmanyotherexamplesinthisdocument,theEngine.Disposemethodiscalledimplicitlybya'
statement.Whileitisnotalwaysappropriatetoutilize'
itisaconvenientwaytoensureDisposeiscalledeveniftheblockisexitedduringanexception.
UseEngineasaFieldinaClass
Theaboveexamples,andmostexamplesinthisdocument,presentuseofanEngineinstanceinasinglemethod.Thisapproachisnotpracticalformostrealapplications.StartingandstoppingEngineobjects,andbyextensionBarTenderprocesses,shouldbedoneasrarelyaspossibleforoptimalperformance.Ideally,Engineinstancesshouldbestartedonceandonlystoppedattheendoftheapplicationtominimizetheoverheadofmanagingprocesses.Themoststraightforwardobject-orientedapproachismakeanEngineobjectafieldinaclassandallowtheencapsulatingclasstodeterminetheEngineobject'
slifetime.
ThefollowingistheminimalsuggestedcodeformakinganEngineobjectafieldinaclass:
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)
//Stoptheprocessandrelea