Printing invoices on receipt paper is a common requirement for retail, hospitality, and similar industries. In this post, we’ll walk through how to print invoice on receipt printers using C#.
Print Invoice on receipt paper in C#
Getting Started
In my previous post, I have explained how to print an invoice on A4 size paper. Here this post will guide you through how to print an invoice on receipt paper using C#.
We’ll cover:- Understanding receipt printers
- Understanding receipt paper
- Code examples to print invoice on receipt printer
- Code examples to print invoice using ESC/POS Commands
Printing an invoice on receipt paper in C# typically involves using a thermal printer (e.g., Epson, Star, Zebra) and printing in text mode, often via a serial, USB, or network port.
Receipt printers commonly use ESC/POS (Escape sequences for Point Of Sale), a command set used to control formatting, line feeds, bold text, etc.
What is a Receipt Printer?
Receipt printers are typically thermal printers designed for fast, low-cost printing on narrow paper rolls (commonly 58mm or 80mm wide). These printers are often connected via:
- USB
- Serial (RS232)
- Network (Ethernet)
- Bluetooth
What is Receipt Paper?
Receipt paper is a type of paper used to print receipts at points of sale (POS) in retail stores, restaurants, gas stations, ATMs, and other businesses. It comes in rolls that fit into receipt printers or cash registers. There are a few different types of receipt paper, but the most common one is thermal paper.
Types of Receipt Paper:
- Thermal Paper
- Bond Paper
- Carbonless Paper (NCR)
Common Sizes:
- Thermal paper rolls: 80mm x 80mm or 57mm x 40mm (width x diameter)
- Bond Paper rolls: Vary more depending on the printer
What You Need:
- Printer installed on your system.
- A printer name (you can get from
Control Panel > Printers
). - Optionally, ESC/POS commands for formatting (e.g., for bold, alignment, cut, etc.).
- The System.Drawing.Printing namespace for printing.
Print to Receipt Printer in C#
Here’s a basic example of printing a receipt with hardcoded content.using System;
using System.Drawing;
using System.Drawing.Printing;
class ReceiptPrinter
{
static void Main()
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "Your_Printer_Name"; // Replace with your receipt printer name
pd.PrintPage += new PrintPageEventHandler(PrintReceipt);
pd.Print();
}
static void PrintReceipt(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
Font font = new Font("Consolas", 10);
float lineHeight = font.GetHeight(g);
float x = 10;
float y = 10;
// Sample receipt text
g.DrawString("MY STORE", new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, x, y); y += lineHeight + 5;
g.DrawString("Date: " + DateTime.Now.ToString(), font, Brushes.Black, x, y); y += lineHeight;
g.DrawString("--------------------------------", font, Brushes.Black, x, y); y += lineHeight;
g.DrawString("Item Qty Price", font, Brushes.Black, x, y); y += lineHeight;
g.DrawString("Apple 2 1.00", font, Brushes.Black, x, y); y += lineHeight;
g.DrawString("Orange 1 0.75", font, Brushes.Black, x, y); y += lineHeight;
g.DrawString("--------------------------------", font, Brushes.Black, x, y); y += lineHeight;
g.DrawString("Total: $2.75", new Font("Consolas", 10, FontStyle.Bold), Brushes.Black, x, y); y += lineHeight + 5;
g.DrawString("Thank you!", font, Brushes.Black, x, y); y += lineHeight;
// End page
e.HasMorePages = false;
}
}
Using ESC/POS Commands
If your receipt printer supports ESC/POS, you can send raw bytes directly for more control (bold, alignment, cut paper).
Example: Send Raw ESC/POS Commandsusing System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
class EscPosPrinter
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);
[DllImport("winspool.drv", SetLastError = true)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", SetLastError = true)]
public static extern bool StartDocPrinter(IntPtr hPrinter, int level, [In] ref DOCINFOA di);
[DllImport("winspool.drv", SetLastError = true)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.drv", SetLastError = true)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", SetLastError = true)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", SetLastError = true)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, out int dwWritten);
[StructLayout(LayoutKind.Sequential)]
public struct DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
public static void PrintReceipt(string printerName)
{
IntPtr hPrinter;
DOCINFOA di = new DOCINFOA
{
pDocName = "Receipt",
pDataType = "RAW"
};
if (OpenPrinter(printerName, out hPrinter, IntPtr.Zero))
{
if (StartDocPrinter(hPrinter, 1, ref di))
{
StartPagePrinter(hPrinter);
string receiptText = "MY STORE\r\n" +
"------------------------------\r\n" +
"Apple 2 $1.00\r\n" +
"Orange 1 $0.75\r\n" +
"------------------------------\r\n" +
"Total $2.75\r\n" +
"\r\nThank you!\r\n\r\n";
// ESC/POS Cut Command
byte[] cutCommand = new byte[] { 0x1D, 0x56, 0x41, 0x10 }; // Partial cut
byte[] receiptBytes = Encoding.ASCII.GetBytes(receiptText);
byte[] finalBytes = new byte[receiptBytes.Length + cutCommand.Length];
Buffer.BlockCopy(receiptBytes, 0, finalBytes, 0, receiptBytes.Length);
Buffer.BlockCopy(cutCommand, 0, finalBytes, receiptBytes.Length, cutCommand.Length);
IntPtr pUnmanagedBytes = Marshal.AllocCoTaskMem(finalBytes.Length);
Marshal.Copy(finalBytes, 0, pUnmanagedBytes, finalBytes.Length);
WritePrinter(hPrinter, pUnmanagedBytes, finalBytes.Length, out _);
Marshal.FreeCoTaskMem(pUnmanagedBytes);
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
}
static void Main()
{
PrintReceipt("Your_Receipt_Printer_Name");
}
}
Sample Invoice Content
Here’s an example of what your receipt might include:
Store Name
123 Street Name
City, State ZIP
Date: 10/07/2025 Time: 11:15 AM
-------------------------------
Item Qty Price
Apple 2 1.00
Banana 1 0.50
Orange 3 0.75
-------------------------------
Subtotal: 4.75
Tax (5%): 0.24
Total: 4.99
-------------------------------
Thank you for shopping!
Tips
- Use monospaced fonts like Consolas for layout consistency.
- Most receipt printers are 32 or 42 characters wide — adjust your text accordingly.
- Always test on your physical printer, as rendering and formatting may vary.
- For complex layouts or graphics, use printer-specific SDKs or drivers.
Summary
Print Invoice on Receipt Paper in C# is straightforward once you understand how to format the content and send it to the printer. Here, we have discussed two methods you can use to print an invoice on receipt paper. I hope this was helpful to you.
Thanks