前提条件:
安装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;