C#动态改变BarTender模板中的内容零、新建BarTender模板和设置需动态填充内容的组件属性
请参考:
⼀、实现效果
⼆、实现动态填充BarTender模板内容核⼼
2.1、BarTender核⼼
/***
* Title:"智慧⼯⼚" 项⽬
* 主题:BarTender帮助类
* Description:
* 功能:
* 1、打印标签
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
*/
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Text;
namespace Utils
{
class BarTenderHelper
{
#region 基础参数
//BarTender程序
private static BarTender.Application barTenderApp = new BarTender.Application();
//BarTender格式
private static BarTender.Format barTenderFormat;
//BarTender设计对象
private static BarTender.DesignObject barTenderObject;
#endregion
/// <summary>
/// 打印标签(该⽅法适⽤于:指定barTender模板中图⽚内容设置为:从数据源获取⽂件名)
/// </summary>
/// <param name="printerName">打印机名称</param>
/// <param name="barTenderTemplatePath">barTender模版路径</param>
/// <param name="barTenderTmplateDic">barTender模板内容字典</param>
/// <returns>返回结果(true:表⽰成功)</returns>
public static bool Print(string printerName,string barTenderTemplatePath, Dictionary<string,string> barTenderTmplateDic) {
try
{
if (string.IsNullOrEmpty(printerName) || string.IsNullOrEmpty(barTenderTemplatePath) ||
barTenderTmplateDic == null || barTenderTmplateDic.Count <= 0)
{
return false;
}
barTenderApp = new BarTender.Application();
barTenderFormat = barTenderApp.Formats.Open(barTenderTemplatePath);
barTenderFormat.PrintSetup.Printer = printerName;
foreach (var item in barTenderTmplateDic)
{
barTenderFormat.SetNamedSubStringValue(item.Key, item.Value);
}
barTenderFormat.PrintOut(true, false);
Dispose();
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 打印标签(该⽅法适⽤于:指定barTender模板中图⽚内容设置为:从数据源获取⽂件名)
/// 打印标签(该⽅法适⽤于:指定barTender模板中图⽚内容设置为:从数据源获取⽂件名)
/// </summary>
/// <param name="printerName">打印机名称</param>
/// <param name="barTenderTemplatePath">barTender模版路径</param>
/// <param name="barTenderTmplateNoImageDic">barTender模板⾮图⽚内容字典</param>
/// <param name="barTenderTmplateImageDic">barTender模板图⽚内容字典</param>
/// <returns>返回结果(true:表⽰成功)</returns>
public static bool Print(string printerName, string barTenderTemplatePath,
Dictionary<string, string> barTenderTmplateNoImageDic, Dictionary<string, string> barTenderTmplateImageDic) {
try
{
if (string.IsNullOrEmpty(printerName) || string.IsNullOrEmpty(barTenderTemplatePath) ||
barTenderTmplateNoImageDic == null || barTenderTmplateNoImageDic.Count <= 0||
barTenderTmplateImageDic == null || barTenderTmplateImageDic.Count<=0)
{
return false;
}
barTenderApp = new BarTender.Application();
barTenderFormat = barTenderApp.Formats.Open(barTenderTemplatePath);
barTenderFormat.PrintSetup.Printer = printerName;
foreach (var item in barTenderTmplateNoImageDic)
{
barTenderFormat.SetNamedSubStringValue(item.Key, item.Value);
}
foreach (var item in barTenderTmplateImageDic)
{
//查图⽚对象
barTenderObject = barTenderFormat.Objects.Find(item.Key);
//指定图⽚对象的图⽚路径
barTenderObject.PicturePath = item.Value;
}
barTenderFormat.PrintOut(false, false);
Dispose();
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/
// <summary>
/// 关闭BarTender
/// </summary>
public static void Close()
{
barTenderFormat?.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
}
/// <summary>
/// 退出BarTender
/// </summary>
/// </summary>
public static void Quit()
{
barTenderApp?.Quit(BarTender.BtSaveOptions.btSaveChanges);
}
/// <summary>
/// 释放BarTender资源
/// </summary>
public static void Dispose()
{
barTenderFormat?.Close(BarTender.BtSaveOptions.btDoNotSaveChanges); barTenderApp?.Quit(BarTender.BtSaveOptions.btSaveChanges);
}
}//Class_end
}
2.2、获取本地打印机内容
generic是什么牌子打印机/***
* Title:"智慧⼯⼚" 项⽬
* 主题:打印机帮助类
* Description:
* 功能:
* 1、获取到本机默认打印机
* 2、获取本机的打印机列表
* Date:2021
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Text;
namespace Utils
{
class PrinterHelper
{
/// <summary>
/// 获取到本机默认打印机
/// </summary>
/// <returns>返回本机的默认打印机名称</returns>
public static string GetLocalDefaultPrinter()
{
PrintDocument printDocument = new PrintDocument();
return printDocument.PrinterSettings.PrinterName;
}
/// <summary>
/
// 获取本机的打印机列表
/// </summary>
/// <returns>返回当前的打印机名称列表</returns>
public static List<string> GetLocalPrinters()
{
List<string> printerList = new List<string>();
foreach (string printer in PrinterSettings.InstalledPrinters)
{
if (!printerList.Contains(printer))
{
printerList.Add(printer);
}
}
return printerList;
}
}//Class_end
}
三、使⽤⽅法
3.1、直接指定图⽚的路径,和组件名称,然后代码中只⽤调⽤该路径下需要填充的图⽚名称即可
【注意:该⽅式下:需要删除图⽚的后缀名,否则打印标签出来不可⽤】
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论