TFORMer SDK - JAVA  8
Generate Output

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

See also:
Basic Concept / Printing
  
  import java.io.*;                             // io streams
  
  import com.tecit.TFORMer.*;                   // Licensing, Error handling, Enumerations
  import com.tecit.TFORMer.Repository.*;        // Provides access to Repository elements 
  import com.tecit.TFORMer.Printing.*;          // Contains functionality for a Print-Job
  import com.tecit.TFORMer.Enumerations.*;      // Contains all enumerations

class OutputSamples
{
//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static Job CreateJobStandAloneForm () throws TFormerException
{
  // Create a new Job instance
  Job printJob = new Job();
  // Select the stand-alone FormLayout
  printJob.setRepositoryName ("C:/Users/All Users/TEC-IT/TFORMer/8/Examples/Command Line/ODBCReportPDF/ODBCReportPDF.tff");

  return printJob;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static Job CreateJobRepositoryBasedForm () throws TFormerException
{
  // Create a new Job instance
  Job printJob = new Job();

  // Select the Repository based FormLayout
  printJob.setRepositoryName ("C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Examples/Demo Repository/Demos.tfr"); 
  printJob.setProjectName ("TFORMer_Runtime_Examples");
  printJob.setFormName ("BarcodeLabels");

  return printJob;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void CsvDataSample ()  throws TFormerException
{
  // 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("c:/temp/Import.txt", ',', '"');

  // Assign the JobData to the Job
  printJob.setJobData(jobData);

  // Select PDF output to /temp/out.pdf 
  printJob.setOutputName  ("/temp/out.pdf");
  printJob.setPrinterType (EPrinterType.PDFFile);

  // Generate output based on the FormLayout and the JobData
  printJob.print ();
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void OdbcDataSample ()  throws TFormerException
{
  // 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.setRepositoryName("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.setJobData(jobData);

  // Select PDF output to /temp/out.pdf 
  printJob.setOutputName  ("/temp/out.pdf");
  printJob.setPrinterType (EPrinterType.PDFFile);

  // Generate output based on the FormLayout and the JobData
  printJob.print ();
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void DataSourceDataSample () throws TFormerException
{
  // 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.setRepositoryName ("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.setParameterValue ("PickingListParameter", "2");

  // Assign the JobData to the Job
  printJob.setJobData (jobData);

  // Select PDF output to /temp/out.pdf 
  printJob.setOutputName  ("/temp/out.pdf");
  printJob.setPrinterType (EPrinterType.PDFFile);

  // Generate output based on the FormLayout and the JobData
  printJob.print();  
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void UseRecordSetData (Job printJob) throws TFormerException
{
  // 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
  JobDataRecordSet.Record record = new JobDataRecordSet.Record();
  // Add some name/value pairs for the DataField values to the Record
  record.setData("ArticleName", "Speaker System HF1");
  record.setData("ArticleNo", "12001234");
  record.setData("ArticlePrice", "498.98");
  // Add the Record to the JobData
  jobData.add(record);

  // Create a second Record
  record = new JobDataRecordSet.Record();
  // This Record should be printed two times
  record.setNumberOfCopies(2);
  record.setData("ArticleName", "Record Box 12 CDs");
  record.setData("ArticleNo", "12021231");
  record.setData("ArticlePrice", "8.85");
  jobData.add(record);

  // Assign the JobData to the Job
  printJob.setJobData(jobData); 
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void UseCsvData (Job printJob) throws TFormerException
{
  // 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.setJobData(jobData); 
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void UseXmlData (Job printJob) throws TFormerException
{
  // 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.setJobData(jobData); 
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void UseOdbcData (Job printJob) throws TFormerException
{
  // 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.setJobData(jobData); 
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void UseOdbcDataDSN (Job printJob) throws TFormerException
{
  // Create a new JobDataOdbc instance
  JobDataOdbc jobDataOdbc = new JobDataOdbc();

  // Set the DSN programmatically
  jobDataOdbc.setDsn("DSN=TFORMer_Sample");

  // Assign the JobData to the Job
  printJob.setJobData(jobDataOdbc); 
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void UseDataSourceData (Job printJob) throws TFormerException
{
  // 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.setParameterValue ("parFile", "/YourPath/InputData.txt");

  // Assign the JobData to the Job
  printJob.setJobData(jobData); 
}

//-------------------------------------------------------------------------------------------
// **** Does this also iterate all global fields? Guess not, probably we would have to iterate global project AND local project
//-------------------------------------------------------------------------------------------
static void IterateUsedDataFields (Project project, FormLayout formlayout) throws TFormerException
{
  // Enumerate all DataFields in the Project
  for (DataField field = project.getFirstDataField (); field != null; field = field.getNext ())
  {
    // Query the DataField usage in the given FormLayout
    DataFieldUsage usage = formlayout.getDataFieldUsage (field.getName ());

    // may the DataField value be set (this depends on the usage of the DataField within the FormLayout)
    if (usage == DataFieldUsage.Normal)
    {
      // Do something
    }
  }
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void PrintToPrinter (Job printJob) throws TFormerException
{
  // Select the printer name on Windows or Linux (CUPS printer name), use null for the default printer
  printJob.setPrinterName (null);   

  // Print
  printJob.print(); 
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void PrintToPdf (Job printJob) throws TFormerException
{
  // Select PDF output to /temp/out.pdf 
  printJob.setOutputName ("/temp/out.pdf");
  printJob.setPrinterType (EPrinterType.PDFFile);

  // Generate PDF
  printJob.print(); 
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void SetPrinterTypePdf (Job printJob) throws TFormerException
{  
  // Select PDF output to /temp/out.pdf with the PDFFILE prefix
  printJob.setPrinterName ( "PDFFILE:/temp/out.pdf" );

  // which is equivalent to

  // Select PDF output to /temp/out.pdf with setPrinterType and setOutputName
  printJob.setPrinterType ( EPrinterType.PDFFile );
  printJob.setOutputName  ( "/temp/out.pdf" );
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void PrintToStream(Job printJob) throws TFormerException, FileNotFoundException
{
  // Select PDF output 
  printJob.setPrinterType (EPrinterType.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
  OutputStream fos = new FileOutputStream("StreamToFileExample.pdf");

  // Generate PDF
  printJob.printToStream (fos);
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
static void PrintToStreams(Job printJob) throws TFormerException
{
  OutputStreamFactory factory = new OutputStreamFactory()
  {
    // Create a new stream 
    // This function will be called for each requested stream before any output is generated
    // You may use this function to open files or to reserve memory
    public OutputStream createOutputStream(String name)
    throws IOException
    {
      // For demonstration purposes we create a file stream
      return new FileOutputStream (name);
    }
  };

  // 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.setOutputName  ("StreamToFileExample.png");
  printJob.setPrinterType (EPrinterType.ImagePng);

  // Generate output with the streaming API using the specified factory
  printJob.printToStreams (factory);  
}

//-------------------------------------------------------------------------------------------
// M A I N
//-------------------------------------------------------------------------------------------
// Licenses TFORMer SDK and calls the various sample functions
//-------------------------------------------------------------------------------------------
public static void main (String[] args) throws TFormerException
{
  TFORMer.license("John Doe", ELicenseKind.Workstation, 1, "00000000000000000000000000000000"); 

  System.out.println ("Create Job for stand-alone FormLayout ...");
  Job printJob = CreateJobStandAloneForm ();
  
  try {
    System.out.println ("Iterate used fields ...");
    Repository repository = new Repository(printJob.getRepositoryName (), false, true);
    IterateUsedDataFields(repository.getGlobalProject (), repository.getGlobalProject ().getFirstFormLayout ());

    System.out.println ("Import data from ODBC ...");
    UseOdbcData    (printJob);

    System.out.println ("Print to default printer ...");
    PrintToPrinter (printJob);

    System.out.println ("Use Stream API ...");
    PrintToStreams (printJob);  
  }
  catch(TFormerException ex){
    // ... manage the exception
    System.out.println ("Exception ...");
    System.out.printf("  Description: %s\n", ex.getMessage( ));
  }
  finally {
  // free resources used by TFORMer SDK
  printJob.dispose();
  // it's a good idea to set the variable to null, because the object is now invalid internally.
  printJob = null;
  }
  
  System.out.println ("");
  System.out.println ("Create Job for Repository based FormLayout ...");
  printJob = CreateJobRepositoryBasedForm ();
  
  try {
    System.out.println ("Set some data ...");
    UseRecordSetData(printJob);
    
    System.out.println ("Print to default printer ...");
    PrintToPrinter (printJob);
    
    System.out.println ("Generate PDF ...");
    PrintToPdf(printJob);
    
    System.out.println ("Use Stream API ...");
    PrintToStreams (printJob);
    
  }
  catch(TFormerException ex){
    // ... manage the exception
    System.out.println("Exception : ");
    System.out.printf("  Description: %s\n", ex.getMessage( ));
  }
  finally {
  // free resources used by TFORMer SDK
  printJob.dispose();
  // it's a good idea to set the variable to null, because the object is now invalid internally.
  printJob = null;
  }    
}

} // class OutputSamples

© 2006-2024 - all rights reserved by TEC-IT Datenverarbeitung GmbH
Generated on Thu Oct 3 2024 05:08:23 for TFORMer SDK - JAVA with doxygen 1.7.6.1