C#调用Bartender模板打印.Net的SDK

前提条件:

安装Bartender打印软件,我用的是10.1,然后用注册机注册,注册后版本选择Enterprise Automation

源码下载地址:https://download.csdn.net/download/HorseRoll/12439808

上面的下载地址是用积分下载的,如果没积分可以用这个链接付费下载:https://download.csdn.net/download/HorseRoll/15738889

1.项目中引用Seagull.BarTender.Print.dll文件,安装目录SDK文件夹下有,实在找不到,下载源码,里面有

2.代码编写

using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BarTenderLabel
{
    public partial class Form1 : Form
    {
        // Common strings.
        private readonly string appName = "Print Job Status Monitor";

        private Engine engine = null; // The BarTender Print Engine.
        private LabelFormatDocument format = null; // The currently open Format.
        private string thumbnailFile = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create and start a new BarTender Print Engine.
            try
            {
                engine = new Engine(true);
            }
            catch (PrintEngineException exception)
            {
                // If the engine is unable to start, a PrintEngineException will be thrown.
                MessageBox.Show(this, exception.Message, appName);
                this.Close(); // Close this app. We cannot run without connection to an engine.
                return;
            }

            // Get the list of printers
            Printers printers = new Printers();
            foreach (Printer printer in printers)
            {
                cboPrinters.Items.Add(printer.PrinterName);
            }

            if (printers.Count > 0)
            {
                // Automatically select the default printer.
                cboPrinters.SelectedItem = printers.Default.PrinterName;
            }

            // Create a uniquely named, temporary file on disk for our thumbnails.
            thumbnailFile = Path.GetTempFileName();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Close the previous format
                if (format != null)
                    format.Close(SaveOptions.DoNotSaveChanges);

                //If the user selected a format, open it in BarTender.
                txtFilename.Text = openFileDialog.FileName;

                try
                {
                    format = engine.Documents.Open(openFileDialog.FileName);
                }
                catch (System.Runtime.InteropServices.COMException comException)
                {
                    MessageBox.Show(this, String.Format("Unable to open format: {0}\nReason: {1}", openFileDialog.FileName, comException.Message), appName);
                    format = null;
                }

                // Only allow print button if we successfully loaded the format.
                btnPrint.Enabled = (format != null);

                if (format != null)
                {
                    format.SubStrings["test1"].Value = textBox1.Text;
                    //format.SubStrings["test2"].Value = textBox1.Text;
                    format.SubStrings["pic2"].Value = textBox2.Text;
                    // Select the printer in use by the format.
                    cboPrinters.SelectedItem = format.PrintSetup.PrinterName;

                    //Generate a thumbnail for it.
                    format.ExportImageToFile(thumbnailFile, ImageType.JPEG, Seagull.BarTender.Print.ColorDepth.ColorDepth24bit, new Resolution(picThumbnail.Width, picThumbnail.Height), OverwriteOptions.Overwrite);
                    picThumbnail.ImageLocation = thumbnailFile;
                }
                else
                {
                    // Clear any previous image.
                    picThumbnail.ImageLocation = "";
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            // Disable the button until we are finished printing.
            btnPrint.Enabled = false;

            // Get the printer from the dropdown and assign it to the format.
            format.PrintSetup.PrinterName = cboPrinters.SelectedItem.ToString();

            // Create a messages object to be filled up by Print.
            Messages messages;

            // Print the Format.
            format.Print(appName, System.Threading.Timeout.Infinite, out messages);
        }
    }
}

3.如果说需要把文件进行输出图片预览

string previewPath = Application.StartupPath;//文件路径
format.ExportPrintPreviewToFile(previewPath, "PrintPreview" + textBox1.Text + ".jpg", ImageType.JPEG, Seagull.BarTender.Print.ColorDepth.ColorDepth24bit, new Resolution(picPreview.Width, picPreview.Height), System.Drawing.Color.White, OverwriteOptions.Overwrite, true, true, out messages);
ShowImage(new Bitmap(previewPath + "\\PrintPreview" + textBox1.Text + "1.jpg"));//进行PictureBox显示

4.如果BTW模板文件中有数据需要动态加载的

format.SubStrings["test1"].Value = textBox1.Text;
format.SubStrings["test2"].Value = textBox2.Text;

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马儿不吃草

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值