C#+CodeSoft 读取Label变量

环境:Windows 10,Visual Studio 2017,ASP.NET Mvc,CodeSoft 2018


在C#读取Label模板的变量,用的是Document的ReadVariables方法,参数是enumDataSource枚举,对应CodeSoft里面的数据源窗口的数据源类型。传不同的枚举值获取相应的数据源集合。

namespace LabelManager2
{
    public enum enumDataSource
    {
        lppxDataSourceCounter = 1,
        lppxDataSourceTableLookup = 2,
        lppxDataSourceDate = 3,
        lppxDataSourceFormula = 4,
        lppxDataSourceFree = 5,
        lppxDataSourceForm = 6,
        lppxDataSourceDataBase = 7
    }
}
数据源→枚举enumDataSource
codesoft数据源→C#枚举enumDataSource

拿到的数据源集合是一个二维数组,可以转Json字符串再转List。

数据如图:


代码: 利用Document提供ReadVariables方法,根据不同的enumDataSource枚举读取Label模板的变量集合。出来的是一个二维数组,第一个是变量名,第二个是值。循环二维数组,把他组合成一个键值对。

注意:外层的数组长度要除2。

using LabelManager2;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CommonLibrary.Helpers
{
    public class CodeSoftHelper
    {
        public static void PrintLabel(string labPath, string printerName, string serialNumber, int quantity = 1)
        {
            ApplicationClass labApp = null;
            Document doc = null;

            try
            {
                labApp = new ApplicationClass();
                labApp.Documents.Open(labPath, false);
                doc = labApp.ActiveDocument;

                object[,] lppxDataSourceFormula = (object[,])doc.ReadVariables(enumDataSource.lppxDataSourceFormula);
                string formulaName = lppxDataSourceFormula[0, 0].ToString();
                string formulaValue = lppxDataSourceFormula[0, 1].ToString();

                Dictionary<string, string> dic = new Dictionary<string, string>();
                for (int i = 0; i < lppxDataSourceFormula.Length / 2; i++)
                {
                    string key = "";
                    string val = "";
                    for (int j = 0; j < 2; j++)
                    {
                        if (j == 0){
                            key = lppxDataSourceFormula[i, j].ToString();
                        }else{
                            val = lppxDataSourceFormula[i, j].ToString();
                        }
                    }
                    dic[key] = val;
                }


                string filePath = "D:\\" + System.DateTime.Now.Year + System.DateTime.Now.Month + System.DateTime.Now.Day + System.DateTime.Now.Hour + System.DateTime.Now.Minute + System.DateTime.Now.Second + ".bmp";//保存图片的路径  为后面预览图片做准备

                string st = doc.CopyImageToFile(8, "BMP", 0, 100, filePath);

                //doc.Printer.SwitchTo(printerName);
                //doc.FormFeed();

                //doc.PrintDocument(quantity);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                labApp.Documents.CloseAll(true);
                labApp.Quit();
                GC.Collect(0);
            }
        }
    }
}

也可以先转成Json字符串,然后再转成一个List,List里面装的是一个由两个元素组成的字符串数组,第一元素是名称,第二个元素是值。 

                string json = JsonConvert.SerializeObject(doc.ReadVariables(enumDataSource.lppxDataSourceFormula));
                List<string[]> lppxDataSourceFormula = JsonConvert.DeserializeObject<List<string[]>>(json);

                Dictionary<string, string> dic = new Dictionary<string, string>();
                foreach (var arr in lppxDataSourceFormula)
                {
                    dic[arr[0]] = arr[1];
                }

猜你喜欢

转载自blog.csdn.net/u012835032/article/details/107914824