C# – Print in Arial Font or any other font by sending raw data to the printer

cepsonprinting

private void printfunction(string cmd)
  {
   string command = cmd;

   // Create a buffer with the command
   Byte[] buffer = new byte[command.Length];
   buffer = System.Text.Encoding.ASCII.GetBytes(command);

   // Use the CreateFile external functo connect to the LPT1 port
   SafeFileHandle printer = CreateFile("LPT1:", FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);

   // Aqui verifico se a impressora é válida
   if (printer.IsInvalid == true)
   {
    MessageBox.Show("Printer not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
   }

   // Open the filestream to the lpt1 port and send the command
   FileStream lpt1 = new FileStream(printer, FileAccess.ReadWrite);
   lpt1.Write(buffer, 0, buffer.Length);

   // Close the FileStream connection
   lpt1.Close();
  }

I've been using the code function above to send raw data to my ESC/POS supported EPSON TM88III printer.

I've only 3 sent of fonts by default in printer. But I wan't to print in ARIAL FONT. How can we print in Arial font.

Please don't suggest me to use windows print spooler or printer driver. I want to print by sending raw data.

How can we do this?

The coding is done in C#.NET using Visual Studio 2008.

Best Answer

This is technically possible by putting the printer in graphics mode and sending pixel data. You'll have to create a monochrome bitmap in your program, the Bitmap and Graphics classes can get the job done. You'd use Graphics.DrawText with a Font initialized with Arial to get the text the way you want it. Encoding the bitmap pixels into printer commands is the non-trivial part, be sure to have a decent programming manual for the printer.

This is otherwise exactly what the printer driver does. It will be just as slow.

Related Topic