I would like to know how to print a thermal ticket using PrintDocument with a Bluetooth-connected printer using .NET in a Windows 10.
If I connect the printer via USB, there is no problem. Here is a simple code version I am currently using:
public void PrintTicketUSB(string printerName)
{
var printDocument = new PrintDocument();
printDocument.PrinterSettings.PrinterName = printerName;
printDocument.PrintPage += (sender, e) => PrintTicketPage(e.Graphics);
printDocument.Print();
}
private void PrintTicketPage(Graphics graphics)
{
float yPos = 0;
int padding = 5;
Font fontTitol = new Font("Arial", 40, FontStyle.Bold);
Font font = new Font("Arial", 10, FontStyle.Regular);
float yPos = padding;
graphics.DrawString("Title", fontTitol, Brushes.Black, padding, yPos);
yPos += fontTitol.GetHeight();
graphics.DrawString("Hello world", font, Brushes.Black, padding, yPos);
yPos += font.GetHeight();
}
When I connect the printer via Bluetooth (Windows 10), I assign it to COM3 and use this code, which works:
public void PrintTicketBT()
{
var serialPort = new SerialPort("COM3");
if(!serialPort.IsOpen)
serialPort.Open();
serialPort.Write("Hello world\n");
serialPort.Close();
}
Is there a way to use PrintDocument with Bluetooth? Are there any references I can consult?
Thank you.
After some testing, I managed to solve my problem and configure the Bluetooth printer as a local printer. These are the steps:
*Please note that my Windows 10 is in Spanish, so the names may be different on your system. Sorry, I still don't have enough reputation to include inline pictures :(
Remember the COM port assigned. In my case, it is "COM3".
Now, go to the printers and select your printer (previously installed).
Finally, assign the COM port to your printer:
This configuration worked for me! I hope it helps somebody else!