Class CsvPrinter

Object
com.zebra.sdk.printer.CsvPrinter

public class CsvPrinter extends Object
A class used to print template formats using comma separated values as input data.

package test.zebra.sdk.printer.examples;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 
 import com.zebra.sdk.printer.CsvPrinter;
 
 public class CsvPrinterExample {
 
     public static void main(String[] args) {
 
         csvPrintingExample();
         cnCsvPrintingExample();
     }
 
     //
     // These examples demonstrate how to use the one-line printing capability of the CsvPrinter class.
     //
     // They assume that a ZPL template containing variable fields appropriate to the CSV data
     // specified exists on the host device. In this case, a PC with the file named CsvPrinterExampleTemplate.zpl
     // saved at "c:\CsvPrinterExampleTemplate.zpl". The contents of this file should be...
     //
     // ^XA^DFCsvExamp.zpl^FS
     // ^A0N,100,100^FO100,100^FN1"Name"^FS
     // ^A0N,100,100^FO100,200^FN2"Street"^FS
     // ^A0N,100,100^FO100,300^FN3"City"^FS
     // ^A0N,100,100^FO100,400^FN4"State"^FS
     // ^A0N,100,100^FO100,500^FN5"Zip"^FS
     // ^XZ
     //
 
     public static void csvPrintingExample() {
 
         // The possible inputs to the one-line CSV printing function(s)
 
         String destinationDevice = "192.168.1.32";
 
         String templateFilename = "c:\\CsvPrinterExampleTemplate.zpl";
         String defaultQuantityString = "1";
         boolean verbose = true;
 
         // If the destination device argument is 'null' then any data that would have been sent to a destination device,
         // had one been specified, is captured in 'outputDataStream'. This provides a way to test your output &
         // configuration without having an actual printer available or without wasting media even if a printer is
         // available.
 
         System.out.println("\nThe destinationDevice connection string argument is null:");
         try {
             ByteArrayOutputStream outputDataStream = new ByteArrayOutputStream();
             CsvPrinter.print(null, getSampleCsvData(), templateFilename, defaultQuantityString, outputDataStream, verbose);
             System.out.println(outputDataStream.toString());
         } catch (Exception e) {
             e.printStackTrace();
         }
 
         // The outputDataStream argument may be null, in which case the data generated by the CsvPrinter class will
         // not be logged but will be sent to the destination device.
         System.out.println("\nThe outputDataStream argument is null:");
         try {
             CsvPrinter.print(destinationDevice, getSampleCsvData(), templateFilename, defaultQuantityString, null, verbose);
         } catch (Exception e) {
             e.printStackTrace();
         }
 
         // Both destinationDevice connection string AND outputDataStream arguments may be specified, in which case the
         // data generated by the CsvPrinter class will be sent to the destination device and logged to the
         // outputDataStream.
         System.out.println("\nBoth destinationDevice connection string and outputDataStream arguments are specified:");
         try {
             ByteArrayOutputStream outputDataStream = new ByteArrayOutputStream();
             CsvPrinter.print(destinationDevice, getSampleCsvData(), templateFilename, defaultQuantityString, outputDataStream, verbose);
             System.out.println(outputDataStream.toString());
         } catch (Exception e) {
             e.printStackTrace();
         }
 
         // At least one of these two (destinationDevice connection string and outputDataStream) arguments should be
         // specified.
     }
 
     private static ByteArrayInputStream getSampleCsvData() {
 
         String sampleCsvData = "John Smith,1234 Anystreet,Anycity,Anystate,12345\nJane Doe,5678 Anyroad,Anytown,Anystate,67890\n";
 
         return new ByteArrayInputStream(sampleCsvData.getBytes());
     }
 
     //
     // This example demonstrates how to use the one-line printing capability of the CsvPrinter class.
     //
     // It assume that a ZPL template containing variable fields appropriate to the CSV data
     // specified exists on the host device. In this case, a PC with the file named CnCsvPrinterExampleTemplate.zpl
     // saved at "c:\CnCsvPrinterExampleTemplate.zpl". The contents of this file should be...
     //
     // ^XA^DFE:CnCsvPrinterExampleTemplate.zpl^FS
     // ^A@N,75,75,E:ANMDS.TTF^CI28^FO0,100^FN1"Customer Name"^FS
     // ^A@N,75,75,E:ANMDS.TTF^FO0,200^FN2"Component Name"^FS^
     // ^A@N,75,75,E:ANMDS.TTF^FO0,300^FN3"Vendor Name"^FS
     // ^A@N,75,75,E:ANMDS.TTF^FO0,400^FN4"Vendor ID"^FS
     // ^A@N,75,75,E:ANMDS.TTF^FO0,500^FN5"Invoice Number"^FS
     // ^XZ
 
     private static void cnCsvPrintingExample() {
         // The possible inputs to the one-line Csv printing function(s)
 
         String destinationDevice = "192.168.1.32";
 
         String templateFilename = "C:\\CnCsvPrinterExampleTemplate.zpl";
         String defaultQuantityString = "1";
         boolean verbose = true;
 
         // The outputDataStream argument may be null, in which case the data generated by the CsvPrinter class will
         // not be logged but will be sent to the destination device.
         System.out.println("\nThe outputDataStream argument is null:");
         try {
             CsvPrinter.print(destinationDevice, getSampleCnCsvData(), templateFilename, defaultQuantityString, null, verbose);
         } catch (Exception e) {
             e.printStackTrace();
         }
 
     }
 
     private static ByteArrayInputStream getSampleCnCsvData() {
 
         String sampleCnCsvData = "东风伟世通汽车饰件系统有限公司,驾驶员侧仪表板下装饰件,供应商名称,供应商代码,订单号\n";
 
         return new ByteArrayInputStream(sampleCnCsvData.getBytes());
     }
 }
 
  • Method Details

    • print

      public static void print(InputStream sourceDataStream, String templateFilename, String defaultQuantityString, OutputStream outputDataStream) throws IOException, ConnectionException
      Print template formats using comma separated values as input data.
      Parameters:
      sourceDataStream - The source stream containing the CSV data.
      templateFilename - The template to merge the CSV data to.
      defaultQuantityString - The quantity, if not specified in the data.
      outputDataStream - Optional stream to send data to.
      Throws:
      IOException - if an I/O error occurs.
      ConnectionException - if it was not possible to connect to the device.
    • print

      public static void print(String destinationDevice, InputStream sourceDataStream, String templateFilename, String defaultQuantityString, OutputStream outputDataStream) throws IOException, ConnectionException
      Print template formats using comma separated values as input data.
      Parameters:
      destinationDevice - The connection string.
      sourceDataStream - The source stream containing the CSV data.
      templateFilename - The template to merge the CSV data to.
      defaultQuantityString - The quantity, if not specified in the data.
      outputDataStream - Optional stream to send data to.
      Throws:
      IOException - if an I/O error occurs.
      ConnectionException - if it was not possible to connect to the device.
    • print

      public static void print(InputStream sourceDataStream, String templateFilename, String defaultQuantityString, OutputStream outputDataStream, boolean verbose) throws IOException, ConnectionException
      Print template formats using comma separated values as input data.
      Parameters:
      sourceDataStream - The source stream containing the CSV data.
      templateFilename - The template to merge the CSV data to.
      defaultQuantityString - The quantity, if not specified in the data.
      outputDataStream - Optional stream to send data to.
      verbose - If true, print a running commentary to standard out.
      Throws:
      IOException - if an I/O error occurs.
      ConnectionException - if it was not possible to connect to the device.
    • print

      public static void print(String destinationDevice, InputStream sourceDataStream, String templateFilename, String defaultQuantityString, OutputStream outputDataStream, boolean verbose) throws IOException, ConnectionException
      Print template formats using comma separated values as input data.
      Parameters:
      destinationDevice - The connection string.
      sourceDataStream - The source stream containing the CSV data.
      templateFilename - The template to merge the CSV data to.
      defaultQuantityString - The quantity, if not specified in the data.
      outputDataStream - Optional stream to send data to.
      verbose - If true, print a running commentary to standard out.
      Throws:
      IOException - if an I/O error occurs.
      ConnectionException - if it was not possible to connect to the device.