Search code examples
c#.netbluetooththermal-printer

Printing a thermal ticket using PrintDocument with a Bluetooth-connected printer using .NET in Windows10


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.


Solution

  • 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 :(

    1. Pair the printer in the Bluetooth section.
    2. Click on "More Bluetooth options".

    screenshot_of_configuration_1

    1. Open the "COM Ports" tab.
    2. Press the "Add" button.
    3. Select the printer from the list or click "Explore".
    4. Click "OK".

    screenshot_of_configuration_2

    Remember the COM port assigned. In my case, it is "COM3".

    screenshot_of_configuration_3

    Now, go to the printers and select your printer (previously installed).

    1. Click on "Manage Printer" or a similar option.

    screenshot_of_configuration_4

    Finally, assign the COM port to your printer:

    1. Open the printer properties.
    2. Open the "Ports" tab.
    3. Select the COM port that we saw before in the Bluetooth settings.
    4. Click "OK".

    screenshot_of_configuration_5

    This configuration worked for me! I hope it helps somebody else!