1 Reply Latest reply on Dec 27, 2017 10:03 AM by Robin West

    How to print from Xamarin Forms to FMT Template stored in the Printer

    Kavitha Kandasamy

      Hi,

       

      This is regarding the printing from Xamarin Forms. Currently we are developing a Xamarin Forms application that runs on both Android and iOS targeting this printer <ZQ520>.

       

      We went through the samples provided in the Xamarin_LinkOS_Developer_Demo and saw how the data is printed using LineDemoView (LineDemoView.cs)

       

      But in our application we are making use of templates where the template is installed on the printer and the data has to be send from Xamarin Forms to the printer <ZQ520> via Bluetooth. We are using the template in the FMT Format. A sample FMT template is attached for your preview.

       

      Our doubt/question:

       

      Pairing not working in iOS platform from iphone 5s and iphone 6. We tried from Settings , bluetooth devices. How to pair  with the printer from iPhone.

       

      How do we pass the data from Xamarin Forms application to the FMT Template stored in the Zebra Printer <ZQ520>. Our data is saved in SQLite database on the hand held device (Android and iPhone).

       

      Kindly assist us with sample code how to pass the data to the FMT template from Xamarin forms after retrieving it from SQLite database.

       

      Thanks,

        • Re: How to print from Xamarin Forms to FMT Template stored in the Printer
          Robin West

          Hi Kavitha,

          The sample code has a demo of stored formats for ZPL, but essentially the same code works for CPCL FMT files.  Change the file extension to "FMT" and make sure the SDK knows you are using CPCL with the ZebraPrinterFactory.  Refer to the documentation for how the IFieldDescription works.   I would add some error handling to the area of line 52 in case the array is smaller than the number of fields.

          For the Bluetooth on iOS, make sure to check the info.plist file is updated.

           

                  //modified from https://github.com/Zebra/LinkOS-Xamarin-Samples/blob/ZSDK_DevDemos/ZSDK_DevDemos/Xamarin_LinkOS_Developer_Demo/Xamarin_LinkOS_Developer_Demo/FormatDemoView.cs
                  private void StartGetTemplatesFromPrinter()
                  {
                      //CheckPrinter();
                      IConnection connection = myPrinter.Connection;
                      try
                      {
                          connection.MaxTimeoutForRead = 5000;
                          connection.TimeToWaitForMoreData = 1000;
                          connection.Open();
          
          
                          if (!CheckPrinterLanguage(connection))
                          {
                              resetPage();
                              return;
                          }
                          IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(PrinterLanguage.CPCL, connection);
                          string[] fileNames = printer.RetrieveFileNames(new string[] { "FMT" });
                      }
                      catch(Exception e)
                      {
                          ShowErrorAlert(e.Message + " - On printer");
                      }
                      finally
                      {
                          if ((connection != null) && (connection.IsConnected))
                              connection.Close();
                          resetPage();
                      }
                  }
          
          //modified from https://github.com/Zebra/LinkOS-Xamarin-Samples/blob/ZSDK_DevDemos/ZSDK_DevDemos/Xamarin_LinkOS_Developer_Demo/Xamarin_LinkOS_Developer_Demo/FormatView.cs
                  private IFieldDescription[] PrintFormatOnPrinter(string fileNameOnPrinter, string[] mydata)
                  {
                      IConnection connection = myPrinter.Connection;
                      IFieldDescription[] fields = new IFieldDescription[0];
                      try
                      {
                          connection.Open();
                          CheckPrinterLanguage(connection);
                          IZebraPrinter printer = ZebraPrinterFactory.Current.GetInstance(connection);
          
          
                          byte[] rawformat = printer.RetrieveFormatFromPrinter(fileNameOnPrinter);
                          string formatstring = Encoding.UTF8.GetString(rawformat, 0, rawformat.Length);
                          fields = printer.GetVariableFields(formatstring);
          
          
                          Dictionary<int, string> vars = new Dictionary<int, string>();
                          int i = 0;
                          foreach(IFieldDescription field in fields)
                          {
                              vars.Add(field.FieldNumber, mydata[i]);
                              i++;
                          }
                          printer.PrintStoredFormat(fileNameOnPrinter, vars);
                      }
                      catch (Exception e)
                      {
                          System.Diagnostics.Debug.WriteLine("Exception:" + e.Message);
                          ShowErrorAlert("GetFieldsFromPrinter"+e.Message);
                      }
                      finally
                      {
                          if ((connection != null) && (connection.IsConnected))
                              connection.Close();
                      }
                      return fields;
                  }
          

          Hope this helps,