【C#】C#调用Bartender模板打印,输出图片,PDF

1,691 阅读1分钟

​上篇文章:【C#】C#调用Bartender模板打印

首先,新建bt模板,再代码调用

直接上代码

public class BTHelper
{



    /// <summary>
    /// 打印
    /// </summary>
    /// <param name="printerName">打印机名称</param>
    /// <param name="btFileName">bt文件</param>
    public static void Print(string printerName, string btFileName)
    {
        BarTender.Application btApp = new BarTender.Application();
        BarTender.Format btFormat;

        btFormat = btApp.Formats.Open(btFileName);
        btFormat.PrintSetup.Printer = printerName;
        btFormat.PrintSetup.NumberSerializedLabels = 2;

        btFormat.SetNamedSubStringValue("Name", "2021-06-12");
        btFormat.SetNamedSubStringValue("Barcode", "123456789");

        btFormat.PrintOut(true, false);

        btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);
        btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);
    }

    public static void ExportToFile(string btFileName)
    {
        BarTender.Application btApp = new BarTender.Application();
        BarTender.Format btFormat;

        btFormat = btApp.Formats.Open(btFileName);

        btFormat.SetNamedSubStringValue("Name", "2021-06-12");
        btFormat.SetNamedSubStringValue("Barcode", "123456789");

        //btFormat.SaveAs(btFileName, true);

        btFormat.ExportToFile(@"E:/BT/temp.jpg", "jpg", BarTender.BtColors.btColors24Bit, BarTender.BtResolution.btResolutionPrinter, BarTender.BtSaveOptions.btSaveChanges);
        btFormat.Close(BarTender.BtSaveOptions.btSaveChanges);
        btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);

    }

}

调用

string fileName = @"E:\BT\test.btw";

//BTHelper.Print("导出为WPS PDF", fileName);

BTHelper.ExportToFile(fileName);

不仅可以输出,图片格式,也可以输出pdf格式,只需修改文件名后缀和文件格式参数即可

输出pdf格式的,也可以使用“导出为WPS PDF”方式,但是纸张大小是A4纸大小

效果