C#调用把BarTender模板Word格式.docx

上传人:b****5 文档编号:20373057 上传时间:2023-01-22 格式:DOCX 页数:16 大小:230.52KB
下载 相关 举报
C#调用把BarTender模板Word格式.docx_第1页
第1页 / 共16页
C#调用把BarTender模板Word格式.docx_第2页
第2页 / 共16页
C#调用把BarTender模板Word格式.docx_第3页
第3页 / 共16页
C#调用把BarTender模板Word格式.docx_第4页
第4页 / 共16页
C#调用把BarTender模板Word格式.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

C#调用把BarTender模板Word格式.docx

《C#调用把BarTender模板Word格式.docx》由会员分享,可在线阅读,更多相关《C#调用把BarTender模板Word格式.docx(16页珍藏版)》请在冰豆网上搜索。

C#调用把BarTender模板Word格式.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1