public async Task PrintReceipt(string textToPrint)
{
try
{
PosPrinter posPrinter = await PosPrinter.GetDefaultAsync();
if (posPrinter == null)
{
// Handle case where no printer is found.
return;
}
_claimedPrinter = await posPrinter.ClaimPrinterAsync();
// Set the character set ID on the claimed printer object.
// 858 is a common European code page that includes '£'.
_claimedPrinter.CharacterSet = 858;
await _claimedPrinter.EnableAsync();
ReceiptPrintJob job = _claimedPrinter.Receipt.CreateJob();
//// Get the correct encoding for the WPC1252 code page
//Encoding wpc1252 = Encoding.GetEncoding(1252);
//// Convert the string to a byte array using the WPC1252 encoding
//byte[] encodedBytes = wpc1252.GetBytes(textToPrint);
//// Now, convert the byte array back to a string
//// This is the string you should send to the printer
//string encodedString = wpc1252.GetString(encodedBytes);
job.Print(textToPrint);
bool result = await job.ExecuteAsync();
if (result)
{
// Printing was successful.
}
else
{
logger.LogWarning("Printing failed. Checking printer status.");
if (_claimedPrinter.IsCoverOpen)
{
logger.LogWarning("Printer cover is open.");
}
if (_claimedPrinter.Receipt.IsPaperEmpty)
{
logger.LogWarning("Receipt printer is out of paper.");
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
_claimedPrinter.Dispose();
}
}
Я пытался явно установить набор символов, а также пытался закодировать строку, однако любые символы £ в параметре textToPrint не печатаются, а вместо этого печатается этот символ.
Epson TM-m30III настроен в служебном приложении следующим образом: [img]https://i.sstatic.net/JfSjpiP2.png[/img]
Я пытаюсь распечатать на принтере в классе C# с помощью Windows.Devices.PointOfService библиотека. Мой код: [code]public async Task PrintReceipt(string textToPrint) { try { PosPrinter posPrinter = await PosPrinter.GetDefaultAsync();
if (posPrinter == null) { // Handle case where no printer is found. return; }
// Set the character set ID on the claimed printer object. // 858 is a common European code page that includes '£'. _claimedPrinter.CharacterSet = 858;
//// Get the correct encoding for the WPC1252 code page //Encoding wpc1252 = Encoding.GetEncoding(1252);
//// Convert the string to a byte array using the WPC1252 encoding //byte[] encodedBytes = wpc1252.GetBytes(textToPrint);
//// Now, convert the byte array back to a string //// This is the string you should send to the printer //string encodedString = wpc1252.GetString(encodedBytes);
job.Print(textToPrint);
bool result = await job.ExecuteAsync();
if (result) { // Printing was successful. } else { logger.LogWarning("Printing failed. Checking printer status.");
if (_claimedPrinter.IsCoverOpen) { logger.LogWarning("Printer cover is open."); } if (_claimedPrinter.Receipt.IsPaperEmpty) { logger.LogWarning("Receipt printer is out of paper."); } } } catch (Exception e) { Console.WriteLine(e); throw; } finally { _claimedPrinter.Dispose(); } } [/code] Я пытался явно установить набор символов, а также пытался закодировать строку, однако любые символы £ в параметре textToPrint не печатаются, а вместо этого печатается этот символ. [img]https://i.sstatic.net/xFy3k66i.png[/img]