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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sourabh DhimanPosted May 23, 2024, 8:47 AM
@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 ChadhaPosted May 23, 2024, 7:58 AM
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 contentReplace 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.