受欢迎的博客标签

BarTender 集成模式之二:C# BarTender .net sdk调用BarTender 模板并打印标签

Published

1.电脑装 BarTender2016软件(勾选 .Net SDK)

2.新建WINFORM测试项目

3.添加COM 组件(由安装软件目录SDK文件夹)

4.     //添加COM 引用BarTender

        实例化
        BarTender.Application btapp;
        BarTender.Format btformat;

5.打印方法

1.方法一、引用“Interop.BarTender.dll”单个打印
2.方法二、引用“Seagull.BarTender.Print.dll”批量打印数据
see:https://www.cnblogs.com/weifeng123/p/14366672.html
1) start bartender engine
2) create label format object link to a format
3) specify substring name and value data mapping
4) specify printer
5) print
6) stop/close the engine

vb

btEngine.Start()

Dim messages As Seagull.BarTender.Print.Messages = Nothing
Dim str As String = "c:\bartender\mylabel.btw"
Dim labelFormatDocument As LabelFormatDocument = btEngine.Documents.Open(str)
For Each tstr As Seagull.BarTender.Print.SubString In labelFormatDocument.SubStrings

   Console.WriteLine("SubString Name: " & tstr.Name & ", SubString Type: " & tstr.Type & ", SubString Value: " & tstr.Value)

  'labelFormatDocument.SubStrings.SetSubString("name", "value")

Next

labelFormatDocument.PrintSetup.PrinterName = "Printer path \\server\printer"
messages = Nothing
Dim waitForCompletionTimeout As Integer = 10000 ' 10 seconds
Dim result As Result = labelFormatDocument.Print("test", waitForCompletionTimeout, messages)
Dim messageString As String = Constants.vbLf + Constants.vbLf & "Messages:"
For Each message As Seagull.BarTender.Print.Message In messages
  messageString &= Constants.vbLf + Constants.vbLf + message.Text
Next message
If result = Result.Failure Then
  MessageBox.Show(Me, "Print Failed" & messageString, "test")
Else
 MessageBox.Show(Me, "Label was successfully sent to printer." & messageString, "test")
End If 

 

' Close all open formats.
btEngine.Documents.CloseAll(SaveOptions.SaveChanges)
' Stop the BarTender print engine.
btEngine.Stop()

Modifying named sub-strings currently requires you to follow exactly the steps you've followed if the data on the label needs to change for every label that prints. If you want to send variable data for several labels in one go, you will need to connect your BT document to a text database file (you would create and fill before):

Imports Seagull.BarTender.Print.Database

' Application Code

' ...

Dim btEngine As New Engine()

' Start a BarTender print engine

btEngine.Start()

' Open a label format

Dim btFormat As LabelFormatDocument = btEngine.Documents.Open("c:\MyLabel.btw")

' Set the TextFile database connection file name

CType(btFormat.DatabaseConnections("TextFileDB"), TextFile).FileName = "c:\NutritionInformationEurope.txt"

' Print the label format document

btFormat.Print()

' Stop the engine

btEngine.Stop()

https://support.seagullscientific.com/hc/en-us/community/posts/216559727-Generating-Label-Using-net

c#

private void button1_Click(object sender, EventArgs e)
        {
            btapp = new BarTender.Application();
            btformat = btapp.Formats.Open(@"C:\Users\Administrator\Desktop\lable1.btw", false, "");
            btformat.PrintSetup.NumberSerializedLabels = 2; //设置打印份数
            btformat.SetNamedSubStringValue("txtGoodCode1", "品号");//

            btformat.SetNamedSubStringValue("txtGoodName1", "品名");//设置“数据源名称”
            btformat.SetNamedSubStringValue("txtOrderPo1", "订单");//
            btformat.SetNamedSubStringValue("txtCustomerGoodCode1", "客户品号");//
            btformat.SetNamedSubStringValue("txtCustomerGoodName1", "品名");//
            btformat.SetNamedSubStringValue("txtLotNo1", "批次品号");//
            btformat.SetNamedSubStringValue("txtQty1", "批次品号");//

         
            btformat.PrintOut(true, false); //第二个参数设置是否跳出打印属性;

          //btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges); //退出时是否保存标签  

       btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);//退出时同步退出bartender进程



原文链接:https://blog.csdn.net/u013002790/article/details/75207975