dir.by  
  Search  
Programming, development, testing
UWP (Universal Windows Platform) C# app for Windows
We write a program in UWP C# that prints text on a receipt EPSON printer (to find a receipt printer I use the C# code PointOfService.PosPrinter.GetDeviceSelector ...)
  Looked at 686 times    
 We write a program in UWP C# that prints text on a receipt EPSON printer (to find a receipt printer I use the C# code PointOfService.PosPrinter.GetDeviceSelector ...) 
last updated: 12 October 2025
Note 1 You must have the printer driver installed. If you don't, then need to download and install the driver for the EPSON printer ...

Note 2 You must have EPSON OPOS ADK installed. If you don't have it, then need to download and install EPSON OPOS ADK ...

Note 3 You must have a receipt EPSON printer added to the program EPSON OPOS ADK. If you don't have This is how you need to add a receipt EPSON printer to the program EPSON OPOS ADK ...
 
Download an example:
AppUWP1_PosPrinter.zip ...
size: 23 kb
Step 1. Create a new C# UWP application
Step 2. In the project, add access to Point of Service (this is access to devices)
Step 3. I added a code to the MainPage.xaml.cs file to print text on a receipt printer
  C#  
using System;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Devices.PointOfService;

namespace AppUWP1
{
     public sealed partial class MainPage : Page
     {
          public MainPage()
          {
               this.InitializeComponent();
               this.Loaded += MainPage_Loaded;
          }

          private void MainPage_Loaded(object sender, RoutedEventArgs e)
          {
               Task.Run(async () =>
               {
                    // find printer
                    Windows.Devices.PointOfService.PosPrinter posPrinter = await HelperPosPrinter.FindPosPrinterAsync();
                    ClaimedPosPrinter claimedPrinter = await posPrinter.ClaimPrinterAsync();
                    ReceiptPrintJob job = await HelperPosPrinter.PreparePrinter(claimedPrinter);

                    // print text
                    StringBuilder bodyToPrint = new StringBuilder();
                    bodyToPrint.AppendLine("Hello");
                    bodyToPrint.AppendLine("Thank you");
                    HelperPosPrinter.PrintText(job, bodyToPrint.ToString());

                    // finish
                    bool result = await HelperPosPrinter.FinishPrint(job, claimedPrinter);
               });

          }
     }
}
Step 4. Added a new file HelperPosPrinter.cs my useful functions for finding a printer and printing text
  C#  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.PointOfService;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
using Windows.Storage;

namespace AppUWP1
{
     class HelperPosPrinter
     {
          public static async Task<BitmapFrame> ConvertFileToBitmap(StorageFolder storageFolder, string filename)
          {
               try
               {
                    StorageFile file = await storageFolder.GetFileAsync(filename);
                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                    {
                         BitmapDecoder bitmapDecoder = await BitmapDecoder.CreateAsync(stream);
                         return await bitmapDecoder.GetFrameAsync(0);
                    }
               }
               catch (Exception ex)
               {
               }
               return null;
          }

          public static void PrintImage(ReceiptPrintJob job, BitmapFrame bitmapFrame, PosPrinterAlignment align, uint maxWidth)
          {
               try
               {
                    job.PrintBitmap(bitmapFrame, align);
               }
               catch (Exception ex)
               {
               }
          }

          public static async Task<ReceiptPrintJob> PreparePrinter(ClaimedPosPrinter claimedPrinter)
          {
               await claimedPrinter.EnableAsync();

               claimedPrinter.Receipt.CharactersPerLine = 56;
               claimedPrinter.Receipt.LineSpacing = 20;
               claimedPrinter.Receipt.IsLetterQuality = true;

               return claimedPrinter.Receipt.CreateJob();
          }

          public static void PrintText(ReceiptPrintJob job, string textToPrint)
          {
               job.Print(textToPrint);
          }

          public static async Task<bool> FinishPrint(ReceiptPrintJob job, ClaimedPosPrinter claimedPrinter)
          {
               try
               {
                    bool result = await job.ExecuteAsync();
                    claimedPrinter.Dispose();
                    return result;
               }
               catch (Exception e)
               {
               }
               return false;
          }

          public static async Task<Windows.Devices.PointOfService.PosPrinter> FindPosPrinterAsync()
          {
               try
               {
                    string posAsql = Windows.Devices.PointOfService.PosPrinter.GetDeviceSelector();
                    DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(posAsql);

                    int count = deviceCollection.Count();
                    foreach (var item in deviceCollection)
                    {
                         var printer = await Windows.Devices.PointOfService.PosPrinter.FromIdAsync(item.Id);
                         if (printer != null)
                              return printer;
                    }
               }
               catch (Exception)
               {
               }
               return null;
          }
     }

}
 
Running the program
The text is printed
 
← Previous topic
Adding a receipt EPSON printer to the EPSON OPOS ADK program
 
Next topic →
Write a program in UWP C# that prints text and images on a receipt EPSON printer
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
Download and install Microsoft Visual Studio to write UWP applications on a computer with the Windows system
Open Visual Studio (to write the project UWP)
Creating a new C# UWP app on a Windows computer
Web API requests
Write a UWP C# program that sends a Web API GET request to a local server
Receipt printer (writing a C# program for printing)
Download and install the library EPSON OPOS ADK (this library is needed to print on an Epson printer using C# code) | I have an EPSON TM-T20X receipt printer
Adding a receipt EPSON printer to the EPSON OPOS ADK program
We write a program in UWP C# that prints text on a receipt EPSON printer (to find a receipt printer I use the C# code PointOfService.PosPrinter.GetDeviceSelector ...)
Write a program in UWP C# that prints text and images on a receipt EPSON printer
Write a program in UWP C# that prints an image of any size on a receipt EPSON POS printer (during execution, change the width of the picture = the width of the paper)
Write a program in UWP C# that prints a qr code on a receipt EPSON printer

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика