Sample Code: Create And Assign Csv Database 追蹤

0
Avatar
Shotaro Ito

Asked how to print multiple record in single job by .net SDK.
I knew that's possible by using SetDatabaseConnection() method, however I haven't tried...
Thanks Jiji!

Created by BarTender 9.4SR3 + Visual C# 2005.

[code]
// Sample code to demonstrate How to dynamically create and assign text database in .net SDK
// This method come to handy when you need to print multiple record within single print job.
// Create a btw format with Text database (CSV) connection.
// (Please cosider to use Commander (Commander script / XML Script) to achieve same result too.)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CreateCSV_cs
{
public partial class Form1 : Form
{
Seagull.BarTender.Print.Engine btEngine;
Seagull.BarTender.Print.LabelFormatDocument btFormat;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// Run BarTender
btEngine = new Seagull.BarTender.Print.Engine();
btEngine.Start();
// Show BarTender UI for debugging purpose (Set "None" to hide)
btEngine.Window.VisibleWindows = Seagull.BarTender.Print.VisibleWindows.All;
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// Quit BarTender
btEngine.Stop(Seagull.BarTender.Print.SaveOptions.DoNotSaveChanges);
}

private void button1_Click(object sender, EventArgs e)
{
// Open format at same folder as executable
btFormat = btEngine.Documents.Open(Application.StartupPath + @"\FMT1.btw");
//btFormat.PrintSetup.PrinterName("Zebra TLP3844-Z"); // specify printer

// Prepare data (you can be more creative.)
string buf = "";
buf += "\"FNAME\",\"LNAME\",\"DOB\"\r\n"; // header
buf += "\"Steven\",\"Jobs\",\"1955/02/24\"\r\n"; //record 1
buf += "\"Stephen\",\"Wozniak\",\"1950/08/11\"\r\n";
buf += "\"John\",\"Sculley\",\"1939/04/06\"\r\n";
buf += "\"Gilbert\",\"Amerio\",\"1943/03/01\"\r\n";

// Create text database as a temporary file
string tmpFile = Path.GetTempFileName();
File.WriteAllText(tmpFile, buf);

// Assign the text database to curtent format's primary database
Seagull.BarTender.Print.Database.TextFile tf = new Seagull.BarTender.Print.Database.TextFile(btFormat.DatabaseConnections[0].Name);
tf.FileName = tmpFile;
btFormat.DatabaseConnections.SetDatabaseConnection(tf);
btFormat.PrintSetup.ReloadTextDatabaseFields = true; // Fix when field order is different from design time

// Print
btFormat.Print();

// Delete text database
File.Delete(tmpFile);

}
}
}[/code]

登入寫評論。