SoFunction
Updated on 2025-03-01

C# calls the printer to implement printing

1. Quote

        #region 
        /*
          *
          * Instructions for use
          Need to be installed through NuGet, which is essential
          */

        string inputString;

        /// <summary>
        /// Get the printer driver name        /// </summary>
        private void getPrintDocumentlist()
        {
            PrintDocument print = new PrintDocument();
            string sDefault = ;//Default printer name            comboBox_drive.(sDefault);

            comboBox_drive.Text = sDefault;//Show the default driver name            foreach (string sPrint in )//Get all printer names            {
                if (sPrint != sDefault)
                {
                    comboBox_drive.(sPrint);
                }
            }

        }
        /// <summary>
        /// Print and draw        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            Font titleFont = new Font("Song-style", 9, );//Title Font
            Font fntTxt = new Font("Song-style", 9, );//Text
            Brush brush = new SolidBrush();//Drawing brush
            Pen pen = new Pen(); //Line Color
            Point po = new Point(10, 10);
            try
            {
                //Draw String                (GetPrintSW().ToString(), titleFont, brush, po);//Print content             
                
                //Draw horizontal lines                //Point[] point = { new Point(20, 50), new Point(200, 50) };//The vertical coordinate remains unchanged                //(pen, point);
                //Draw vertical lines                //Point[] points1 = { new Point(60, 70), new Point(60, 70 + 40) };//The horizontal coordinate remains unchanged                //(pen, points1);
                //Draw a rectangle                //(pen, 20, 70, 90, 90);
            }

            catch (Exception ex)
            {
                (this, "Print error!", "hint", , );

            }

        }
       /// <summary>
       /// Get the print content       /// </summary>
       /// <returns></returns>
        public StringBuilder GetPrintSW()
        {
            StringBuilder sb = new StringBuilder();

            string tou = "XXXXXX Technology Co., Ltd.";

            string address = "Yaohai District, Hefei City, Anhui Province";

            string saleID = "100010000001";    //Single number
            string item = "project";

            decimal price = 25.00M;

            int count = 5;

            decimal total = 0.00M;

            decimal fukuan = 500.00M;

            (" " + tou + " \n");

            ("-----------------------------------------");

            ("date:" + () + " " + "Single Number:" + saleID);

            ("-----------------------------------------");

            ("project" + "    " + "quantity" + "  " + "unit price" + "    " + "Subtotal");

            for (int i = 0; i < count; i++)
            {
                decimal xiaoji = (i + 1) * price;

                (item + (i + 1) + "    " + (i + 1) + "   " + price + "    " + xiaoji);

                total += xiaoji;

            }

            ("-----------------------------------------");

            ("quantity:" + count + " Total: " + total);

            ("Payment:" + fukuan);

            ("Cash change:" + (fukuan - total));

            ("-----------------------------------------");

            ("address:" + address + "");

            ("Tel: 13000000000");

            ("Thank you for your patronage and welcome you next time!");

            ("-----------------------------------------");

            return sb;

        }


        /// <summary>
        /// Generate barcode        /// </summary>
        /// <param name="content">Content</param>        /// &lt;returns&gt;&lt;/returns&gt;
        public static Image GenerateBarCodeBitmap(string content)
        {
            using (var barcode = new Barcode()
            {
                IncludeLabel = true,
                Alignment = ,
                Width = 250,
                Height = 100,
                RotateFlipType = ,
                BackColor = ,
                ForeColor = ,
            })
            {
                return (TYPE.CODE128B, content);
            }
        }
        #endregion

2. Quote

        #region   
        /// &lt;summary&gt;
        /// Print test        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;&lt;/param&gt;
        /// &lt;param name="e"&gt;&lt;/param&gt;
        private void printbt_Click(object sender, EventArgs e)
        {
            string qd = comboBox_drive.Text;//The driver name selected by the drop-down list            var printDocument = new PrintDocument();
            //Specify the printer             = qd;//Driver name
             += new PrintPageEventHandler(printDocument1_PrintPage);
            try
            {
               
                //Print preview                //PrintPreviewDialog ppd = new PrintPreviewDialog();
                // = printDocument;
                //();

                //Print                ();
            }
            catch (InvalidPrinterException)
            {

            }
            finally
            {
                ();
            }
        }
        /// &lt;summary&gt;
        /// BarTender Print        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;&lt;/param&gt;
        /// &lt;param name="e"&gt;&lt;/param&gt;
        private void BarTender_Click(object sender, EventArgs e)
        {
            try

            {
                //Writing a reference using in the program is essential;                //After installing Bartender, you can find the corresponding dll in the root directory of the installation or system32 after class.                #region 
                Engine btEngine = new Engine();
                ();
                string lj =  + "";  //It's a template for BT                LabelFormatDocument btFormat = (lj);

                // Assign the corresponding fields of the BTW template                ["name"].Value ="Liming";
                ["code"].Value = "1234567890";

                //Specify the printer name                 = "WPS Virtual Printer";

                //Change the label and print several copies of serialization                 = 1;

                //Print number of copies                 = 1;
                Messages messages;

                int waitout = 10000; // 10 seconds timeout                Result nResult1 = ("Label Printing Software", waitout, out messages);
                 = ;

                //Not saving modification to open template                ();

                //End the Print Engine                ();
                #endregion


            }
            catch (Exception ex)
            {
                ("error message: " + );
                return;
            }
        }

        #endregion

3. Quote Interop.

        #region  Interop.
        /// &lt;summary&gt;
        /// Print function CodeSoft        /// &lt;/summary&gt;
        /// <param name="PrintParam1">Print template parameter value 1</param>        /// <param name="PrintParam2">Print template parameter value 2</param>        /// <param name="PrintParam3">Print template parameter value 3</param>        /// <param name="PrintParam4">Print template parameter value 4</param>        /// &lt;returns&gt;&lt;/returns&gt;
        public bool SoftCodePrint(string PrintParam1 = "", string PrintParam2 = "", string PrintParam3 = "", string PrintParam4 = "")
        {
            bool result = false;
            int printNum = 2;//Print number of copies            try
            {
                string text = ;
                ApplicationClass labApp = null;
                Document doc = null;
                string labFileName =  + "Template\\" + "";//Template address                if (!(labFileName))
                {
                    throw new Exception("No tag template found");
                }

                for (int i = 0; i &lt; printNum; i++)
                {
                    labApp = new ApplicationClass();
                    (labFileName, false);// Call the designed label file                    doc = ;

                    //Can configure the printing information through the configuration file                    ("Template variable name 1").Value = PrintParam1;
                    ("Template variable name 2").Value = PrintParam2;
                    ("Template Variable Name 3").Value = PrintParam3;
                    ("Template Variable Name 4").Value = PrintParam4;
                    (1);
                }

                ();
                result = true;
            }
            catch (Exception ex)
            {

            }
            return result;

        }
        #endregion

dll download address

This is all about this article about C# calling printers to implement printing. I hope it will be helpful to everyone's learning and I hope everyone will support me more.