c#實現控制打印機並繪圖

第一步:確保電腦已經下載了打印機驅動,並且本地可以正常使用打印機
第二部:上代碼
1.添加button按鈕,因爲是測試用的,所以選擇創建的項目是web頁面,頁面類型爲web窗體。
2.添加按鈕點擊事件:<asp:Button runat=“server” Text=“打印測試” id=“printTest” OnClick=“printTest_Click”/>
3:編寫後臺代碼記錄如下:
using System;
using System.Drawing;
using System.Drawing.Printing;
namespace PrintContrl
{

public partial class print : System.Web.UI.Page
{
    //需要顯示的行數
    int rowCount = 5;
    //每行高度
    int rowCol = 40;
    private int count = 40;//打印條數
    private int currentPrint = 0;


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 打印測試
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void printTest_Click(object sender, EventArgs e)
    {
        PrintDocument print = new PrintDocument();
        print.PrintPage += new PrintPageEventHandler(printDocument_PrintA4Page);
        string name = "CAB SQUIX 4.3/300";

        print.PrinterSettings.PrinterName = name;

        print.DefaultPageSettings.Landscape = true;//設置橫向打印,不設置默認是縱向的
        print.PrintController = new System.Drawing.Printing.StandardPrintController();
        int printCount = count / (rowCount - 1) + ((count % (rowCount - 1)) != 0 ? 1 : 0);

        for (int i = 1; i <= printCount; i++)
        {
            currentPrint = i;
            print.Print();
        }
    }
    /// <summary>
    /// 打印頁面內容填充
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void printDocument_PrintA4Page(object sender, PrintPageEventArgs e)
    {
        Font titleFont = new Font("黑體", 18, System.Drawing.FontStyle.Bold);//標題字體           
        Font fntTxt = new Font("宋體", 10, System.Drawing.FontStyle.Regular);//正文文字           
        System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//畫刷           
        System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);           //線條顏色           
        System.Drawing.Point po = new System.Drawing.Point(10, 10);

        try
        {
            e.Graphics.DrawString("標題", titleFont, brush, new System.Drawing.Point(20, 40));
            e.Graphics.DrawString("logo", titleFont, brush, new System.Drawing.Point(200, 40));
            //畫橫線
            Point[] point = { new Point(20, 50), new Point(200, 50) };
            e.Graphics.DrawLines(pen, point);


            e.Graphics.DrawString("本次打印時間:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"), fntTxt, brush, new System.Drawing.Point(750, rowCount * rowCol + 85));

            //畫矩形
            e.Graphics.DrawRectangle(pen, 20, 70, 90, rowCount * rowCol);


            //劃橫線
            for (int i = 0; i < this.rowCount - 1; i++)
            {
                Point[] points = { new Point(20, 70 + (i + 1) * this.rowCol), new Point(110, 70 + (i + 1) * this.rowCol) };
                e.Graphics.DrawLines(pen, points);
            }


            //劃豎線
            Point[] points1 = { new Point(60, 70), new Point(60, 70 + this.rowCol * this.rowCount) };
            e.Graphics.DrawLines(pen, points1);

            //頭部
            e.Graphics.DrawString("產品編號", fntTxt, brush, new System.Drawing.Point(25, 75));
            e.Graphics.DrawString("1000034", fntTxt, brush, new System.Drawing.Point(65, 75));

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

}

備註:本實例主要通過PrintDocument對象對打印機實現控制

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章