目的:在数据库中设定标签格式,及定义的Substring设定值类型。
      再通过给定义的文件路径及格式,打印。
版本:9.4
代码如下:
if (!File.Exists(filepath))
            {
                MessageBox.Show("打印文件不存在!", "警告", MessageBoxButtons.OK);
                return;
            }            string connstr = "";            try
            {
                engine = new Engine();
                engine.Start();
                LabelFormatDocument btFormat = engine.Documents.Open(FormPath);
                btFormat.PrintSetup.PrinterName = printName;                // Assign number of identical copies if it is not datasourced.
                if (!(printCopies >= 1))
                    MessageBox.Show("Warning", "Copies must be an integer greater than or equal to 1.", MessageBoxButtons.OK);
                else
                    btFormat.PrintSetup.IdenticalCopiesOfLabel = printCopies;
//连接txt文件作为数据库
                TextFile textFileDB = new TextFile("TextfileDatabase");
                textFileDB.FileName = filepath;
                btFormat.DatabaseConnections.SetDatabaseConnection(textFileDB); //取文件名作为数据库域名称
                string filename = print_set_header.Rows[0]["FORMAT_NAME"].ToString() ;
                filename = filename.Replace(".btw", ""); //根据标签Substring取值类型赋值
                for (int i = 0; i < print_set_detail.Rows.Count; i++)
                {
                    //指定文字
                    if ("T".Equals(print_set_detail.Rows[i]["TYPE"].ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        btFormat.SubStrings[print_set_detail.Rows[i]["VARIABLE"].ToString()].Value = print_set_detail.Rows[i]["COL"].ToString();
                    }
                    else
                    //数据库字段
                    {
                        btFormat.SubStrings[print_set_detail.Rows[i]["VARIABLE"].ToString()].Value = filename +"."+ Convert.ToInt32(print_set_detail.Rows[i]["COL"]).ToString();
                    }
                }
                btFormat.Print();                btFormat.Close(SaveOptions.DoNotSaveChanges);
                engine.Stop();
                engine.Dispose();
            }
            catch (PrintEngineException exception)
            {
                // If the engine is unable to start, a PrintEngineException will be thrown.
                MessageBox.Show("Warning", exception.Message, MessageBoxButtons.OK);
                //this.Close(); // Close this app. We cannot run without connection to an engine.
                return;
            }在以下代码处报错:
btFormat.DatabaseConnections.SetDatabaseConnection(textFileDB);
错误提示为:
A database with the name "TextfileDatabase" does not exist.