HTML tags are getting printed via printer when sending a request to print from c# console application.When HTML tags are being printed directly through a printer from a C# console application
Loading
HTML tags are getting printed via printer when sending a request to print from c# console application.When HTML tags are being printed directly through a printer from a C# console application
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sourabh DhimanRank #2,987 · 483 reputationPosted May 23, 2024, 4:47 AM EDT
@Abhishek I have used this code for printing
try
{
//using (TcpClient client = new TcpClient())
//{
// await client.ConnectAsync(printerIPAddress, printerPort);
// using (NetworkStream stream = client.GetStream())
// {
// await stream.WriteAsync(pdfBytes, 0, pdfBytes.Length);
// await stream.FlushAsync();
// }
//}
using (TcpClient client = new TcpClient())
{
await client.ConnectAsync(printerIPAddress, printerPort);
using (NetworkStream stream = client.GetStream())
using (StreamWriter writer = new StreamWriter(stream))
{
string plainText = Regex.Replace(htmlContent, "<[^>]*>", "");
//await writer.WriteAsync(plainText);
await writer.FlushAsync();
}
}
}
Abhishek ChadhaRank #402 · 6.2K reputationPosted May 23, 2024, 3:58 AM EDT
PrintDocumentclass to create a document object and specify formatting options.PrintDocumentobject, handle thePrintPageevent to specify the content to be printed, and then send the document to the printer.Here's a basic example of how you can use
PrintDocumentto print formatted contentusing System; using System.Drawing; using System.Drawing.Printing; class Program { static void Main(string[] args) { PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(PrintPage); pd.Print(); } private static void PrintPage(object sender, PrintPageEventArgs e) { // Example: Print "Hello, World!" with red color and large font Font font = new Font("Arial", 16); Brush brush = new SolidBrush(Color.Red); e.Graphics.DrawString("Hello, World!", font, brush, 100, 100); } }Replace the content in the
PrintPageevent handler with your HTML rendering logic or plain text formatting as needed. This will allow you to control how the content is printed by the printer.