TFORMer SDK - NET 8
Generate Output

This sample demonstrates different ways to generate output based on a FormLayout and on JobData.

See also
Basic Concept / Printing
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
class OutputSamples
{
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
Job CreateJobStandAloneForm ()
{
// Create a new Job instance
Job printJob = new Job();
// Select the stand-alone FormLayout
printJob.RepositoryName = "C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Examples/Command Line/ODBCReportPDF/ODBCReportPDF.tff";
return printJob;
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
Job CreateJobRepositoryBasedForm ()
{
// Create a new Job instance
Job printJob = new Job();
// Select the Repository based FormLayout
printJob.RepositoryName = "C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Examples/Demo Repository/Demos.tfr";
printJob.ProjectName = "TFORMer_Runtime_Examples";
printJob.FormName = "BarcodeLabels";
return printJob;
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void CsvDataSample ()
{
// Create a new Job instance
Job printJob = new Job();
// Select the FormLayout (stand-alone or Repository-based)
// ...
// Create a new JobData instance
// Here we import a CSV and specify separator and qualifier character
JobDataCsv jobData = new JobDataCsv("/temp/Import.txt", ',', '"');
// Assign the JobData to the Job
printJob.JobData = jobData;
// Select PDF output to /temp/out.pdf
printJob.OutputName = "/temp/out.pdf";
printJob.PrinterType = PrinterType.PdfFile;
// Generate output based on the FormLayout and the JobData
printJob.Print ();
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void OdbcDataSample ()
{
// Create a new Job instance
Job printJob = new Job();
// Select the stand-alone FormLayout named ODBCReportPDF.tff
// This FormLayout is usually installed as part of the TFORMer Examples in
// - Windows Vista or later: C:/Program Data/TEC-IT/TFORMer/8/Examples
// - Older Microsoft Windows versions: C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Examples
printJob.RepositoryName = "C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Examples/Command Line/ODBCReportPDF/ODBCReportPDF.tff";
// Create a new JobData instance using the installed sample ODBC connection named TFORMer_Sample
JobData jobData = new JobDataOdbc("DSN=TFORMer_Sample", "", "", "SELECT FROM tbl_Example");
// Assign the JobData to the Job
printJob.JobData = jobData;
// Select PDF output to /temp/out.pdf
printJob.OutputName = "/temp/out.pdf";
printJob.PrinterType = PrinterType.PdfFile;
// Generate output based on the FormLayout and the JobData
printJob.Print ();
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void DataSourceDataSample ()
{
// Create a new Job instance
Job printJob = new Job();
// Select the stand-alone FormLayout named (6) Samples_Picking List.tff
// This FormLayout is usually installed as part of TFORMer in
// - Windows Vista or later: C:/Program Data/TEC-IT/TFORMer/8/Templates
// - Older versions of Microsoft Windows: C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Templates
printJob.RepositoryName = "C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Templates/(6) Samples_Picking List.tff";
// Create a new JobData instance using the DataSource named "ODBC" defined inside the Formlayout
JobDataDataSource jobData = new JobDataDataSource("ODBC");
// Specify any DataSourceParameters
jobData.ParameterValues.Add ("PickingListParameter", "2");
// Assign the JobData to the Job
printJob.JobData = jobData;
// Select PDF output to /temp/out.pdf
printJob.OutputName = "/temp/out.pdf";
printJob.PrinterType = PrinterType.PdfFile;
// Generate output based on the FormLayout and the JobData
printJob.Print();
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void UseRecordSetData (Job printJob)
{
// Create a new JobData instance.
// Here we are using the JobDataRecordSet which accepts Records containing name/value pairs
// Create a new Record
Record record = new Record();
// Add some name/value pairs for the DataField values to the Record
record.Data.Add("ArticleName", "Speaker System HF1");
record.Data.Add("ArticleNo", "12001234");
record.Data.Add("ArticlePrice", "498.98");
// Add the Record to the JobData
jobData.Records.Add(record);
// Create a second Record
record = new Record();
// This Record should be printed two times
record.NumberOfCopies = 2;
record.Data.Add("ArticleName", "Record Box 12 CDs");
record.Data.Add("ArticleNo", "12021231");
record.Data.Add("ArticlePrice", "8.85");
jobData.Records.Add(record);
// Assign the JobData to the Job
printJob.JobData = jobData;
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void UseCsvData (Job printJob)
{
// Create a new JobData instance.
// Here we import a CSV and specify separator and qualifier character
JobDataCsv jobData = new JobDataCsv("path/data.csv", ',', '"');
// Assign the JobData to the Job
printJob.JobData = jobData;
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void UseXmlData (Job printJob)
{
// Create a new JobData instance.
// Here we import a XML file (must comply with the supported XML schemes)
JobDataXml jobData = new JobDataXml("path/data.xml");
// Assign the JobData to the Job
printJob.JobData = jobData;
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void UseOdbcData (Job printJob)
{
// Create a new JobData instance
// Here we import data from an ODBC connection using the specified SQL SELECT statement
JobDataOdbc jobData = new JobDataOdbc (
"DSN=TFORMer_Sample",
"", // Username
"", // Password
"SELECT * FROM tbl_Example"
);
// Assign the JobData to the Job
printJob.JobData = jobData;
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void UseOdbcDataSetDsn (Job printJob)
{
// Create a new JobDataOdbc instance
JobDataOdbc jobDataOdbc = new JobDataOdbc();
// Set the DSN programmatically
jobDataOdbc.Dsn = "DSN=TFORMer_Sample";
// Assign the JobData to the Job
printJob.JobData = jobDataOdbc;
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void UseDataSourceData (Job printJob)
{
// Create a new JobDataDataSource instance
// The DataSource named myTextDataSource must be defined in the FormLayout or Repository
JobDataDataSource jobData = new JobDataDataSource ("myTextDataSource");
// Set the DataSourceParameter named parFile
jobData.ParameterValues.Add ("parFile", "/YourPath/InputData.txt");
// Assign the JobData to the Job
printJob.JobData = jobData;
}
//-------------------------------------------------------------------------------------------
// **** Does this also iterate all global fields? Guess not, probably we would have to iterate global project AND local project
//-------------------------------------------------------------------------------------------
void IterateUsedDataFields (Project project, FormLayout formlayout)
{
// Enumerate all DataFields in the Project
for (DataField field = project.FirstDataField; field != null; field = field.Next)
{
// Query the DataField usage in the given FormLayout
DataFieldUsage usage = formlayout.GetDataFieldUsage(field.Name);
// may the DataField value be set (this depends on the usage of the DataField within the FormLayout)
if (usage == DataFieldUsage.Normal)
{
// Do something
Console.WriteLine(field.Name);
}
}
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void PrintToPrinter (Job printJob)
{
// Select the printer name on Windows or Linux (CUPS printer name), use null for the default printer
printJob.PrinterName = null;
// Print
printJob.Print();
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void PrintToPdf (Job printJob)
{
// Select PDF output to /temp/out.pdf
printJob.OutputName = "/temp/out.pdf";
printJob.PrinterType = TECIT.TFORMer.PrinterType.PdfFile;
// Generate PDF
printJob.Print();
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void SetPrinterTypePdf (Job printJob)
{
// Select PDF output to /temp/out.pdf with the PDFFILE prefix
printJob.PrinterName = "PDFFILE:/temp/out.pdf";
// which is equivalent to
// Select PDF output to /temp/out.pdf with PrinterType and OutputName
printJob.PrinterType = PrinterType.PdfFile;
printJob.OutputName = "/temp/out.pdf";
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void PrintToStream(Job printJob)
{
// Select PDF output
printJob.PrinterType = PrinterType.PdfFile;
// Generate output using the streaming API (no temporary files will be needed on the file-system)
// For demonstration purposes we provide a FileStream, a PDF file named /temp/out.pdf will be generated
// In real life you will most likely use the stream to generate the response for some web-client
using (FileStream streamOut = new FileStream("/temp/out.pdf", FileMode.Create))
{
// Generate PDF
printJob.PrintToStream (streamOut);
}
}
//-------------------------------------------------------------------------------------------
// Delegate to create a new stream
// This delegate will be called for each requested stream before any output is generated
// You may use this delegate to open files or to reserve memory
//-------------------------------------------------------------------------------------------
Stream CreateStream (string sName)
{
// For demonstration purposes we create a file stream
Stream stream = new FileStream (sName, FileMode.Create);
return stream;
}
//-------------------------------------------------------------------------------------------
// Delegate to close a stream
// This delegate will be called for each requested stream if it is no longer used
// You may use this delegate for cleanup purposes
//-------------------------------------------------------------------------------------------
void CloseStream(Stream stream)
{
stream.Close ();
}
//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void PrintToStreams(Job printJob)
{
// Generate a PNG image named StreamToFileExample.png
// The provided OutputName is passed to CreateStream
// If multiple pages are generated page numbers will get appended (e.g. StreamToFileExample_002.png)
printJob.OutputName = "StreamToFileExample.png";
printJob.PrinterType = PrinterType.ImagePng;
// Generate output with the streaming API using the specified delegates
printJob.PrintToStreams (CreateStream, CloseStream);
}
//-------------------------------------------------------------------------------------------
// M A I N
//-------------------------------------------------------------------------------------------
// Licenses TFORMer SDK and calls the various sample functions
//-------------------------------------------------------------------------------------------
static void Main(string[] args)
{
// License TFORMer SDK (the license key is only for demonstration purposes)
TFORMer.License("John Doe", LicenseKind.Workstation, 1, "00000000000000000000000000000000");
OutputSamples sample = new OutputSamples();
Console.WriteLine("Create Job for stand-alone FormLayout ...");
Job printJob = sample.CreateJobStandAloneForm();
try {
Console.WriteLine("Iterate used fields ...");
Repository repository = new Repository(printJob.RepositoryName, false, true);
sample.IterateUsedDataFields(repository.GlobalProject, repository.GlobalProject.FirstFormLayout);
Console.WriteLine("Import data from ODBC ...");
sample.UseOdbcData (printJob);
Console.WriteLine("Print to default printer ...");
sample.PrintToPrinter (printJob);
Console.WriteLine("Use Stream API ...");
sample.PrintToStreams (printJob);
}
catch(TFORMerException ex){
Console.WriteLine("Error");
}
finally {
// Free resources used by TFORMer SDK
printJob.Dispose ();
// To invalid the object internally set the variable to null
printJob = null;
}
Console.WriteLine();
Console.WriteLine ("Create Job for Repository based FormLayout ...");
printJob = sample.CreateJobRepositoryBasedForm();
try
{
Console.WriteLine ("Set some data ...");
sample.UseRecordSetData(printJob);
Console.WriteLine ("Generate PDF ...");
sample.PrintToPdf(printJob);
}
catch (TFORMerException ex)
{
Console.WriteLine ("Error");
}
finally
{
// Free resources used by TFORMer SDK
printJob.Dispose();
// To invalid the object internally set the variable to null
printJob = null;
}
}
} // class OutputSamples
Provides access to DataField information.
Definition: DataField.cs:57
DataField Next
Gets the next DataField defined in the Project.
Definition: DataField.cs:416
Provides access to a FormLayout which is referenced by a Repository or available as stand-alone FormL...
Definition: FormLayout.cs:66
DataFieldUsage GetDataFieldUsage(string dataFieldName)
Retrieves the usage of a specific DataField in a FormLayout.
Definition: FormLayout.cs:367
Provides access to JobDataCsv (a CSV-file that serves as JobData for a Job).
Definition: JobDataCsv.cs:20
Provides access to a user-defined DataSource, which is defined in the FormLayout or a Repository.
Definition: JobDataDataSource.cs:18
Abstract base class for JobData.
Definition: JobData.cs:18
Provides access to JobDataOdbc (an ODBC connection that serves as JobData for a Job).
Definition: JobDataOdbc.cs:20
Provides access to JobDataRecordSet (a set of in-memory Record objects which serves as JobData for a ...
Definition: JobDataRecordSet.cs:18
List< Record > Records
Gets/Sets the list of Records.
Definition: JobDataRecordSet.cs:31
Provides access to JobDataXml (a XML file that serves as JobData for a Job).
Definition: JobDataXml.cs:20
This is the main class of TFORMer SDK for generating output.
Definition: Job.cs:266
string RepositoryName
Gets/Sets the filename of the Repository or the filename of the stand-alone FormLayout for this Job.
Definition: Job.cs:456
void Print()
Prints the Job using the selected FormLayout and the current JobData.
Definition: Job.cs:886
Provides access to Project information of a Repository or a stand-alone link FormLayout.
Definition: Project.cs:22
FormLayout FirstFormLayout
Gets the first FormLayout of the Project.
Definition: Project.cs:295
DataField FirstDataField
Gets the first DataField of the Project.
Definition: Project.cs:215
Provides access to a Record.
Definition: JobDataRecordSet.cs:66
Dictionary< string, string > Data
Gets/Sets the values for the Record.
Definition: JobDataRecordSet.cs:92
Opens or creates a Repository, opens a stand-alone FormLayout.
Definition: Repository.cs:24
Project GlobalProject
Gets the global Project of the Repository.
Definition: Repository.cs:478
Exceptions thrown by TFORMer SDK.
Definition: TFormerException.cs:17
Provides methods to query status information and to license TFORMer SDK.
Definition: TFormer.cs:81
static void License(string licensee, LicenseKind kind, int numberOfLicenses, string key)
Applies a license to this instance of the TFORMer SDK.
Definition: TFormer.cs:119
PrinterType
This enumeration specifies the type of output generated by TFORMer SDK.
Definition: Job.cs:26
DataFieldUsage
Describes the usage of a DataField within a FormLayout.
Definition: FormLayout.cs:15
LicenseKind
The license types for the TFORMer SDK.
Definition: TFormer.cs:24

© 2006-2023 - all rights reserved by TEC-IT Datenverarbeitung GmbH
Generated on Thu Dec 28 2023 18:45:19 for TFORMer SDK - NET with doxygen 1.9.4