Search code examples
c#txtextcontrol

How to print HTML using TXTEXTCONTROL


I am working on window application. I am facing a problem to print a webpage or HTML using TXTEXTControl. What I need to do is, Pass html to method and it will print.

public void PrintHtml(string html)
{ 
 PrintDocument pd = new PrintDocument();
            pd.PrinterSettings = new PrinterSettings();
            pd.PrinterSettings.PrinterName = printerName;
            textControl1.Text =html;
            pd.PrinterSettings.PrintFileName = "d:\\abc.pdf";
            pd.DefaultPageSettings.PaperSize = new PaperSize("Label", (int)textControl1.PageSize.Width, (int)textControl1.PageSize.Height);
            pd.DefaultPageSettings.Margins = new Margins((int)textControl1.PageMargins.Left, (int)textControl1.PageMargins.Right, (int)textControl1.PageMargins.Top, (int)textControl1.PageMargins.Bottom);
  pd.DefaultPageSettings.Landscape = false;
 textControl1.Print(pd);
    }

Anyone, please guide me, how to print html.


Solution

  • You need to load html to print your html page.

        public void PrintHtml(string html)
        { 
    textControl1.Load("your file path",StringStreamType.HTMLFormat)
         PrintDocument pd = new PrintDocument();
                    pd.PrinterSettings = new PrinterSettings();
                    pd.PrinterSettings.PrinterName = printerName;
                    textControl1.Text =html;// this line not needed.
                    pd.PrinterSettings.PrintFileName = "d:\\abc.pdf";
                    pd.DefaultPageSettings.PaperSize = new PaperSize("Label", (int)textControl1.PageSize.Width, (int)textControl1.PageSize.Height);
                    pd.DefaultPageSettings.Margins = new Margins((int)textControl1.PageMargins.Left, (int)textControl1.PageMargins.Right, (int)textControl1.PageMargins.Top, (int)textControl1.PageMargins.Bottom);
          pd.DefaultPageSettings.Landscape = false;
         textControl1.Print(pd);
            }