C#打印繪圖的實現(xiàn)方法
本文實例講述了C#打印繪圖的實現(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
String drawString = "";
Font drawFont = null;
SolidBrush drawBrush = null;
float x = 0F;
float y = 0F;
StringFormat drawFormat = new StringFormat();
string test = "";
public string Test
{
get { return test; }
set { test = value; }
}
public Form1()
{
InitializeComponent();
//設(shè)置紙張大小
PaperSize paperSize = new PaperSize("DataOrder", 1023, 614);
printDocument1.DefaultPageSettings.PaperSize = paperSize;
}
//打印
private void button1_Click(object sender, EventArgs e)
{
printDocument1.PrintPage += new PrintPageEventHandler(MyPrintDoc_PrintPage);
try
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
printPreviewDialog1.ShowDialog();
printDocument1.Print();
}
catch
{
MessageBox.Show("請安裝打印機", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
protected void MyPrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
//字體 顏色 格式 坐標(biāo)
drawFont = new Font("Arial", 8);
drawBrush = new SolidBrush(Color.Black);
x = 0F;
y = 0F;
drawFormat.FormatFlags = StringFormatFlags.NoWrap;
//塊數(shù)
int num = 6;
float weightAll = 12.600F;
//標(biāo)題
//標(biāo)題第一行
string title = "鋼板入庫計量單";
string BillCode = "吊號:" + "20080505170";
string ClassType = "班別:" + "乙";
string type1 = "類型:" + "定軋";
string OrderNum = "記錄單號:" + "QW-Y14-02-06";
//標(biāo)題第二行
string ArriveStation = "到站:";
string SpLine = "專用線:";
string SaleNo = "銷售訂單號:";
//標(biāo)題第三行
string AcceptName = "收貨單位:" + "XXXX/12*2438*9144收二支";
string time = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString().PadLeft(2,'0')+"月"+DateTime.Now.Day.ToString()+"日";
//表格下數(shù)據(jù)
string BoardNum = "合計:" + num.ToString() + " 塊 " + weightAll.ToString()+" 噸 ";
string CheckMan1 = "檢查員:" + "";
string MeName = "計量員:"+"XX";
string CheckMan2 = "核對員:" + "";
//線條長度
Pen line = new Pen(drawBrush, 1);
//繪圖--字的位置
//繪圖--總標(biāo)題
e.Graphics.DrawString(title, drawFont, drawBrush, 400, 45, drawFormat);
//繪圖--標(biāo)題--第一行
//吊號
e.Graphics.DrawString(BillCode, drawFont, drawBrush, 125,83, drawFormat);
//班別
e.Graphics.DrawString(ClassType, drawFont, drawBrush, 354, 83, drawFormat);
//類型
e.Graphics.DrawString(type1, drawFont, drawBrush, 500, 83, drawFormat);
//記錄單號
e.Graphics.DrawString(OrderNum, drawFont, drawBrush, 685, 83, drawFormat);
//繪圖--標(biāo)題--第二行
//到站
e.Graphics.DrawString(ArriveStation, drawFont, drawBrush, 125, 106, drawFormat);
//專用線
e.Graphics.DrawString(SpLine, drawFont, drawBrush, 354, 106, drawFormat);
//銷售訂單號
e.Graphics.DrawString(SaleNo, drawFont, drawBrush, 500, 106, drawFormat);
//繪圖--標(biāo)題--第三行
//收貨單位
e.Graphics.DrawString(AcceptName, drawFont, drawBrush, 125, 129, drawFormat);
//時間
e.Graphics.DrawString(time, drawFont, drawBrush, 685, 129, drawFormat);
//繪圖--表格下數(shù)據(jù)
//合計
e.Graphics.DrawString(BoardNum, drawFont, drawBrush, 125, 568, drawFormat);
//檢查員
e.Graphics.DrawString(CheckMan1, drawFont, drawBrush, 400, 568, drawFormat);
//計量員
e.Graphics.DrawString(MeName, drawFont, drawBrush, 550, 568, drawFormat);
//核對員
e.Graphics.DrawString(CheckMan2, drawFont, drawBrush, 700, 568, drawFormat);
//繪圖 表格
float leftbianJu = 120;
float topbianJu = 152;
float tableWidth = 770;
float tableHeight = 393;
float cellwidth = 55;
float cellwidth1 = 110;
float cellheigh = 0F;
//繪圖--線的位置 外邊矩形
//橫
e.Graphics.DrawLine(line, leftbianJu, topbianJu,leftbianJu+tableWidth,topbianJu);
e.Graphics.DrawLine(line, leftbianJu, topbianJu + tableHeight, leftbianJu + tableWidth, topbianJu + tableHeight);
//豎
e.Graphics.DrawLine(line, leftbianJu, topbianJu, leftbianJu, topbianJu+tableHeight);
e.Graphics.DrawLine(line, leftbianJu + tableWidth, topbianJu, leftbianJu + tableWidth, topbianJu + tableHeight);
//e.Graphics.DrawLine(line, 15, 0, 15, 614);
//繪圖--內(nèi)部豎線的位置
y = topbianJu+tableHeight;
x = leftbianJu + cellwidth;
e.Graphics.DrawLine(line, x, topbianJu, x,y );
x= leftbianJu + cellwidth + cellwidth1;
e.Graphics.DrawLine(line,x , topbianJu, x, y);
x = leftbianJu + cellwidth + cellwidth1 * 2;
e.Graphics.DrawLine(line, x, topbianJu, x, y);
x = leftbianJu + cellwidth + cellwidth1 * 3;
e.Graphics.DrawLine(line, x, topbianJu, x, y);
x = leftbianJu + cellwidth * 2 + cellwidth1 * 3;
e.Graphics.DrawLine(line, x, topbianJu, x, y);
x = leftbianJu + cellwidth * 3 + cellwidth1 * 3;
e.Graphics.DrawLine(line, x, topbianJu, x, y);
x = leftbianJu + cellwidth * 4 + cellwidth1 * 3;
e.Graphics.DrawLine(line, x, topbianJu, x, y);
x = leftbianJu + cellwidth * 5 + cellwidth1 * 3;
e.Graphics.DrawLine(line, x, topbianJu, x, y);
x = leftbianJu + cellwidth * 6 + cellwidth1 * 3;
e.Graphics.DrawLine(line, x, topbianJu, x, y);
x = leftbianJu + cellwidth * 7 + cellwidth1 * 3;
e.Graphics.DrawLine(line, x, topbianJu, x, y);
//繪圖--內(nèi)部橫線的位置
//根據(jù)塊數(shù)計算表格高度
if (num != 0)
cellheigh = tableHeight / (float)num;
else
cellheigh = 0;
//繪圖--內(nèi)部橫線的位置
for (int i = 1; i < num; i++)
{
if (i == 1)
{
//數(shù)據(jù)
string str = "";
str = "序號";
x = leftbianJu + cellwidth / 5;
y = topbianJu + cellheigh / 3;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "卡片編號";
x = leftbianJu + cellwidth + cellwidth1 / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "爐號";
x = leftbianJu + cellwidth + cellwidth1 + cellwidth1 / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "物料編碼";
x = leftbianJu + cellwidth + cellwidth1*2 + cellwidth1 / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "牌號";
x = leftbianJu + cellwidth + cellwidth1*3 + cellwidth / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "厚度";
x = leftbianJu + cellwidth * 2 + cellwidth1 * 3 + cellwidth / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "寬度";
x = leftbianJu + cellwidth * 3 + cellwidth1 * 3 + cellwidth / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "長度";
x = leftbianJu + cellwidth * 4 + cellwidth1 * 3 + cellwidth / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "重量";
x = leftbianJu + cellwidth * 5 + cellwidth1 * 3 + cellwidth / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "級別";
x = leftbianJu + cellwidth * 6 + cellwidth1 * 3 + cellwidth / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
str = "偏差";
x = leftbianJu + cellwidth * 7 + cellwidth1 * 3 + cellwidth / 5;
e.Graphics.DrawString(str, drawFont, drawBrush, x, y, drawFormat);
}
//橫線
y = topbianJu + cellheigh * i;
e.Graphics.DrawLine(line, leftbianJu, y, leftbianJu + tableWidth, y);
}
e.HasMorePages = false;
}
運行效果如下圖所示:
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
使用checked語句防止數(shù)據(jù)溢出的解決方法
本篇文章是對用checked語句防止數(shù)據(jù)溢出的解決方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05詳解C#如何在不同工作簿之間復(fù)制選定單元格區(qū)域
處理Excel文檔時,我們經(jīng)常需要將數(shù)據(jù)整合到一個工作表以便于我們進行管理或數(shù)據(jù)對比。本文將演示如何通過編程方式將選定的單元格區(qū)域從一個工作簿復(fù)制到另一個工作簿2023-02-02C#中的WebRequest與WebResponse抽象類、DNS靜態(tài)類、Ping類介紹
這篇文章介紹了C#中的WebRequest與WebResponse抽象類、DNS靜態(tài)類、Ping類,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05c#重寫TabControl控件實現(xiàn)關(guān)閉按鈕的方法
這是關(guān)于c#重寫TabControl控件實現(xiàn)關(guān)閉按鈕的例子,整理了一下,與大家分享。2013-04-04C#客戶端HttpClient請求認(rèn)證及數(shù)據(jù)傳輸
本文詳細講解了C#客戶端HttpClient請求認(rèn)證及數(shù)據(jù)傳輸,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01