FAQ: TFORMer SDK
How Can I Import a CSV File via Program Code?
How Can I Import a CSV File via Program Code?
The following sample code shows how to use CSV import with a CSV data source defined in the TFF layout file (using TFORMer Designer). For more flexibility it has been configured with a parameter, which specifies the file path: parFilePath
TFORMer Java API
// create new job
job = new Job();
File file = new File ("c:\\Temp\\MyTemplate.tff");
job.setRepositoryName(file.getAbsolutePath());
// Select the Data Source named 'ImportFromCSV' (defined in the Form Layout)
JobDataDataSource jobData = new JobDataDataSource ("ImportFromCSV");
// Set the Data Source Parameter named 'parFilePath'
jobData.setParameterValues ("parFilePath", "C:\\Temp\\myImportFile.csv");
// Set the output format:
// e.g. EPrinterType.PDFFile, EPrinterType.ZPLFile, EPrinterType.ImagePng
job.setPrinterType(EPrinterType.PDFFile);
// Output file name
job.setOutputName(System.getProperty("user.dir") + File.separator + "MyLabel.PDF");
System.out.println("Printing to " + job.getOutputName());
// Generate Output
job.setJobData(jobData);
job.print();
The advantage with using a data source defined in the layout: The data source definition may contain the mapping between import fields and layout fields, in addition you can add calculated fields (on demand). Optional data source parameters give you the flexibility for programming or command line interaction.