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;
using TECIT.TFORMer;


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 
  JobDataRecordSet jobData = new JobDataRecordSet();

  // 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

© 2006-2024 - all rights reserved by TEC-IT Datenverarbeitung GmbH
Generated on Wed Aug 7 2024 17:29:42 for TFORMer SDK - NET with doxygen 1.7.6.1