How to print in clientside printer using ActiveX (User control) in c#.Net?

Oct 8 2007 5:58 AM
Hi,

Can anyone tell me how to print in clientside printer using ActiveX(User Control) in C#.Net?

Actually i followed steps given in following link:

http://www.c-sharpcorner.com/UploadFile/dsandor/ActiveXInNet11102005040748AM/ActiveXInNet.aspx

i have included the following in AssemblyInfo.cs:
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile(@"..\..\ActiveXDotNetTry.snk")]

From htm page java script im calling the Public property UserText. In that property im calling an method given below (Which is use to print an voucher).

public void PrintLunchToken(string strTokenID)
        {
            PrintDocument printDoc = new PrintDocument();
            font = new System.Drawing.Font("Verdana", 9);
            printDoc.DocumentName = string.Empty;
            PrinterSettings printSetting = new PrinterSettings();
            //if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["LUNCHTOKENPRINTER"]))
            //    printSetting.PrinterName = ConfigurationManager.AppSettings["LUNCHTOKENPRINTER"];

            printSetting.Copies = 1;

            printDoc.PrinterSettings = printSetting;

            StringBuilder printText = new StringBuilder();
            printText.AppendLine("                  Senior Center");
            //getting the name of provider in the center of printed token
            string strProviderName = "PName"; //ProviderName;//Provider.GetName();
            int whiteSpace = (50 - strProviderName.Length) / 2;
            if (whiteSpace > 0)
            {
                for (int i = 0; i < whiteSpace; i++)
                {
                    strProviderName = " " + strProviderName;
                }
            }
            printText.AppendLine(strProviderName.ToUpper());
            printText.AppendLine("--------------------------------------------------");
            printText.AppendLine("                   Meal Voucher");
            printText.AppendLine("--------------------------------------------------");
            printText.AppendLine(" ");
            printText.AppendLine("NAME : " + "CName");//CnsmrName);
            printText.AppendLine("VOUCHER ID : " + strTokenID);
            printText.AppendLine("Date : " + DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss tt"));
            printText.AppendLine(" ");
            printText.AppendLine("--------------------------------------------------");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine(" ");
            printText.AppendLine("                 Have a Good Day !!");
            reader = new StringReader(printText.ToString());
            printDoc.PrintPage += new PrintPageEventHandler(this.PagePrint);
            printDoc.Print();
            reader.Close();
        }
        private void PagePrint(object sender, PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float linePosition = 0;
            int lineCount = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            String line = null;
            // gets the number of lines per page
            linesPerPage = e.MarginBounds.Height / font.GetHeight(e.Graphics);
            // iterate lines in string
            while (lineCount < linesPerPage && ((line = reader.ReadLine()) != null))
            {
                // set line postion from top margin
                linePosition = 0 + (lineCount * font.GetHeight(e.Graphics));
                // print line
                e.Graphics.DrawString(line, font, Brushes.Black, 0, linePosition, new StringFormat());
                lineCount++;
            }
            // are there more lines?
            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

but it is not working. Can you please help me to achieve this? Urgent.