TFORMer SDK - DLL/Library  8
Generate Output

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

See also:
Basic Concept / Printing
#ifdef _WIN32

  #define TECIT_DLLIMPORT
  #include "TFormer.h"
  #undef  TECIT_DLLIMPORT

#else

  #define TECIT_DLLIMPORT    
  #define _TEC_UNIX
  #define TEC_UNIX_BUILD

  #include <TECITStd/TECITStd.h>
  #include <TFormer.h>

  #undef  TECIT_DLLIMPORT
  #undef  TECIT_DLLIMPORT
  #undef  _TEC_UNIX
  #undef  TEC_UNIX_BUILD

#endif

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE ConnectToStandAloneForm (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  // Select the stand-alone FormLayout
  eCode = TFormer_SetRepositoryName   (hTForm, "C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Examples/Command Line/ODBCReportPDF/ODBCReportPDF.tff");

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE ConnectToRepositoryBasedForm (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  // Select the Repository based FormLayout
  eCode = TFormer_SetRepositoryName   (hTForm, "C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Examples/Demo Repository/Demos.tfr");
  eCode = TFormer_SetProjectName      (hTForm, "TFORMer_Runtime_Examples");
  eCode = TFormer_SetFormName         (hTForm, "BarcodeLabels");

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE CsvDataSample (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  // Create a new Job instance  

  // Select the FormLayout (stand-alone or Repository-based)
  // ... 

  // Here we import a CSV and specify separator and qualifier character
  eCode = TFormer_SetTxtDataFile (hTForm, "c:/temp/Import.txt", ',', '\"');
  
  // Select PDF output
  if (eCode == ErrOk)
    eCode = TFormer_SetPrinterType  (hTForm, ePrinterType_PDFFile);

  // To /temp/out.pdf
  if (eCode == ErrOk)
    eCode = TFormer_SetOutputName   (hTForm, "/temp/out.pdf");

  // Generate output based on the FormLayout and the JobData
  if (eCode == ErrOk)
    eCode = TFormer_Print           (hTForm);

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE OdbcDataSample (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  
  // 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  
  eCode = TFormer_SetRepositoryName (hTForm, "C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Examples/Command Line/ODBCReportPDF/ODBCReportPDF.tff");  

  // Here we are using the installed sample ODBC connection named TFORMer_Sample
  eCode = TFormer_SetODBCData       (hTForm, "DSN=TFORMer_Sample", "", "", "SELECT * FROM tbl_Example");

  // Select PDF output
  if (eCode == ErrOk)
    eCode = TFormer_SetPrinterType  (hTForm, ePrinterType_PDFFile);

  // To /temp/out.pdf
  if (eCode == ErrOk)
    eCode = TFormer_SetOutputName   (hTForm, "/temp/out.pdf");

  // Generate output based on the FormLayout and the JobData
  if (eCode == ErrOk)
    eCode = TFormer_Print           (hTForm);

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE DataSourceDataSample (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  // 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
  TFormer_SetRepositoryName (hTForm, "C:/Documents and Settings/All Users/Application Data/TEC-IT/TFORMer/8/Templates/(6) Samples_Picking List.tff");  
      
  // Here we are using the DataSource named "ODBC" defined inside the Formlayout 
  eCode = TFormer_SetDatasourceName  (hTForm, "ODBC");
  
  // Select PDF output
  if (eCode == ErrOk)
    eCode = TFormer_SetPrinterType  (hTForm, ePrinterType_PDFFile);

  // To /temp/out.pdf
  if (eCode == ErrOk)
    eCode = TFormer_SetOutputName   (hTForm, "/temp/out.pdf");

  // Generate output based on the FormLayout and the JobData
  if (eCode == ErrOk)
    eCode = TFormer_Print           (hTForm);

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE UseRecordSetData (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;
  
  // Clear the Data Cache 
  eCode = TFormer_ResetData           (hTForm);

  // Create a new Record 
  eCode = TFormer_NewRecord           (hTForm);
  // Add some name/value pairs for the DataField values to the Record
  eCode = TFormer_SetVarValue         (hTForm, "ArticleNo",     "12001234");
  eCode = TFormer_SetVarValue         (hTForm, "ArticleName",   "Speaker System HF1");
  eCode = TFormer_SetVarValue         (hTForm, "ArticlePrice",  "498.98");                   

  // Create a Second Record 
  eCode = TFormer_NewRecord           (hTForm);                                           
  // This Record should be printed two times
  eCode = TFormer_SetRecordCopy       (hTForm, 2);
  // Add some name/value pairs for the DataField values to the Record
  eCode = TFormer_SetVarValue         (hTForm, "ArticleNo",     "12021231");                 
  eCode = TFormer_SetVarValue         (hTForm, "ArticleName",   "Record Box 12 CDs"); 
  eCode = TFormer_SetVarValue         (hTForm, "ArticlePrice",  "8.85");                   

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE UseCsvData (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  // Here we import a CSV and specify separator and qualifier character
  eCode = TFormer_SetTxtDataFile (hTForm, "path/data.csv", ',', '"');

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE UseXmlData (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  // Use the XML file InputData.XML as data source 
  eCode = TFormer_SetXmlDataFile (hTForm, "path/data.xml"); 

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE UseOdbcData (HTFORM hTForm)
{  
  ERRCODE eCode       = ErrOk;
  // Here we import data from an ODBC connection using the specified SQL SELECT statement
  eCode = TFormer_SetODBCData (
      hTForm, 
      "DSN=TFORMer_Sample", 
      NULL, /* User */
      NULL, /* Password */
      "SELECT * FROM tbl_Example"
  );

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE UseOdbcDataDSN (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  eCode = TFormer_SetODBCData (hTForm, "DSN=TFORMer_Sample", "", "", "SELECT * FROM tbl_Example");

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE UseDataSourceData (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  // The DataSource named myTextDataSource must be defined in the FormLayout or Repository
  // Retrieve data using the predefined data source myTextDataSource 
  eCode = TFormer_SetDatasourceName   (hTForm, "myTextDataSource");                    
  // Set the DataSourceParameter named parFile
  eCode = TFormer_SetDSParameterValue (hTForm, "parFile", "/YourPath/InputData.txt");  

  return eCode;
}

//-------------------------------------------------------------------------------------------
// **** Does this also iterate all global fields? Guess not, probably we would have to iterate global project AND local project
// set pszProjectName to NULL to iterate datafields of global Project
//-------------------------------------------------------------------------------------------
ERRCODE IterateUsedDataFields (HTFORM hTForm, LPCSTR pszProjectName)
{
  ERRCODE eCode = ErrOk;

  // Enumerate all DataFields in the Project
  HPROJECTVAR_ITERATOR  it                = TFormer_GetFirstProjectVarIt (hTForm, pszProjectName, &eCode);
  BOOL                  bProjectVarValid  = (it != NULL);
  LPCSTR                pszDataFieldName  = NULL;

  // TYPE_E_ELEMENTNOTFOUND should not be treated as error
  if (eCode == TYPE_E_ELEMENTNOTFOUND)
    eCode = ErrOk;  

  // iterate all DataFields  
  while (bProjectVarValid && (eCode == ErrOk))
  {
    // Query the DataField usage in the given FormLayout
    EVarUsage eVarUsage = TFormer_ProjectVarItGetUsage (hTForm, it, &eCode);      
    if ((eVarUsage == eVarUsage_Normal) && (eCode == ErrOk))
    {
      pszDataFieldName = TFormer_ProjectVarItGetName (it, &eCode);
      if (pszDataFieldName != NULL && eCode == ErrOk)
        printf ("  %s\n", pszDataFieldName);
    }

    // get next DataField
    bProjectVarValid = (TFormer_GetNextProjectVarIt (it) == ErrOk);
  }
  // release iterator
  TFormer_FreeProjectVarIt (it);

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE PrintToPrinter (HTFORM hTForm)
{  
  ERRCODE eCode           = ErrOk;
  LPCSTR  pszPrinterName  = NULL;
  // Select the printer name on Windows or Linux (CUPS printer name), use NULL for the default printer
  eCode = TFormer_SetPrinterName  (hTForm, pszPrinterName, NULL);

  // Print 
  eCode = TFormer_Print           (hTForm);                          

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE PrintToPdf (HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;

  // Select PDF output
  eCode = TFormer_SetPrinterType  (hTForm, ePrinterType_PDFFile);
  // To /temp/out.pdf
  eCode = TFormer_SetOutputName   (hTForm, "/temp/out.pdf");
  // Generate PDF
  eCode = TFormer_Print           (hTForm);                          

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE SetPrinterTypePdf (HTFORM hTForm)
{  
  ERRCODE eCode = ErrOk;

  // Select PDF output to /temp/out.pdf with the PDFFILE prefix
  eCode = TFormer_SetPrinterName  (hTForm, "PDFFILE:/temp/out.pdf", NULL);              

  // which is equivalent to

  // Select PDF output to /temp/out.pdf with setPrinterType and setOutputName
  eCode = TFormer_SetPrinterType  (hTForm, ePrinterType_PDFFile);
  eCode = TFormer_SetOutputName   (hTForm, "/temp/out.pdf");

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
void PrintToStream()
{
  // not supported by C API, included just for completeness
}

// Define the call-back function for streaming
ERRCODE __stdcall StreamCallback (BYTE* pBuffer, INT nSizeBuffer, DWORD dwFlags, LPARAM lParam)
{
  ERRCODE eCode = ErrOk;
  if      (dwFlags == eWriteCallBack_Data)
  { 
    // Process data (copy or write the data to the stream)
  }    
  else if (dwFlags == eWriteCallBack_Close)
  {
    // Close the current stream
  }    
  else if (dwFlags == eWriteCallBack_Open)
  {
    // Open a new stream, the name which is set in TFormer_SetOutputName is provided via pBuffer (UTF8 encoded)
  }

  return eCode;
}

//-------------------------------------------------------------------------------------------
// 
//-------------------------------------------------------------------------------------------
ERRCODE PrintToStreams(HTFORM hTForm)
{
  ERRCODE eCode = ErrOk;
  
  // Generate a PNG image named StreamToFileExample.png
  // The provided OutputName is passed to call-back function
  // If multiple pages are generated page numbers will get appended (e.g. StreamToFileExample_002.png)
  eCode = TFormer_SetOutputName   (hTForm, "StreamToFileExample.png");
  eCode = TFormer_SetPrinterType  (hTForm, ePrinterType_ImagePng);

  // Generate PDF
  // Instead of #TFormer_Print use #TFormer_PrintToStream and provide the callback function.
  eCode = TFormer_PrintToStream   (hTForm, StreamCallback, (LPARAM)0);

  return eCode;
}

//-------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------
void ReportError (ERRCODE eCode, HTFORM hTForm, LPCSTR pszFunction)
{
  LPCSTR pszErrDescription = NULL;
  LPCSTR pszErrSource      = NULL;
  LPCSTR pszErrInformation = NULL;

  if (eCode == ErrOk)
    return;

  if (pszFunction)
    printf ("Error in %s\n", pszFunction);

  printf ("Error         : 0x%08x\n", eCode);

  if (hTForm)
  {  
    pszErrDescription   = TFormer_GetLastErrorDescription (hTForm);
    pszErrInformation   = TFormer_GetLastErrorInformation (hTForm);
    pszErrSource        = TFormer_GetLastErrorSource      (hTForm);

    if (pszErrDescription && pszErrDescription[0])
      printf("TFError       : %s\n", pszErrDescription);

    if (pszErrInformation && pszErrInformation[0])
      printf("TFInformation : %s\n", pszErrInformation);

    if (pszErrSource && pszErrSource[0])
      printf("TFSource      : %s\n", pszErrSource);
  }
}

//-------------------------------------------------------------------------------------------
// M A I N
//-------------------------------------------------------------------------------------------
// Licenses TFORMer SDK and calls the various sample functions
//-------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
  ERRCODE eCode   = ErrOk;
  HTFORM  hTForm  = NULL;
  // Allocate memory and retrieve TFORMer handle 
  hTForm = TFormer_Init (NULL); 
  if (!hTForm)
    return;

  TFormer_License ("John Doe", LICKIND_WORKSTATION, 1, "00000000000000000000000000000000"); 

  // Samples for stand-alone FormLayouts
  printf ("Stand-alone FormLayout samples\n");
  printf ("Connect to tff file ...\n");
  eCode = ConnectToStandAloneForm (hTForm);
  ReportError (eCode, hTForm, "ConnectToStandAloneForm");


  if (eCode == ErrOk)
  { 
    printf ("Iterate used fields ...\n");
    eCode = IterateUsedDataFields (hTForm, NULL);
    ReportError (eCode, hTForm, "IterateUsedDataFields");
    eCode = ErrOk; // Clear Error    
  }

  if (eCode == ErrOk)
  { 
    printf ("Import data from ODBC ...\n");
    eCode = UseOdbcData (hTForm);
    ReportError (eCode, hTForm, "UseOdbcData");
  }

  if (eCode == ErrOk)
  { 
    printf ("Print to default printer ...\n");
    eCode = PrintToPrinter (hTForm);
    ReportError (eCode, hTForm, "PrintToPrinter");
  }

  if (eCode == ErrOk)
  {
    printf ("Use Stream API ...\n");
    eCode = PrintToStreams (hTForm);
    ReportError (eCode, hTForm, "PrintToStreams");      
  }

  // Samples for Repository files
  printf ("\n----------------------\nRepository samples\n");
  printf ("Connect to repository ...\n");
  eCode = ConnectToRepositoryBasedForm (hTForm);    
  ReportError (eCode, hTForm, "ConnectToRepositoryBasedForm");      

  if (eCode == ErrOk)
  {
    printf ("Set some data ...\n");
    eCode = UseRecordSetData (hTForm);    
    ReportError (eCode, hTForm, "UseRecordSetData");      
  }

  if (eCode == ErrOk)
  {
    printf ("Generate PDF ...\n");
    eCode = PrintToPdf (hTForm);    
    ReportError (eCode, hTForm, "PrintToPdf");      
  }

  // Free resources used by TFORMer SDK
  TFormer_Exit (hTForm);
}

© 2006-2021 - all rights reserved by TEC-IT Datenverarbeitung GmbH
Generated on Wed Nov 17 2021 12:13:03 for TFORMer SDK - DLL/Library with doxygen 1.7.6.1