C#打印繪圖的實現方法
本文實例講述了C#打印繪圖的實現方法。分享給大家供大家參考。具體實現方法如下:
代碼如下:
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();
//設置紙張大小
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)
{
//字體 顏色 格式 坐標
drawFont = new Font(“Arial”, 8);
drawBrush = new SolidBrush(Color.Black);
x = 0F;
y = 0F;
drawFormat.FormatFlags = StringFormatFlags.NoWrap;
//塊數
int num = 6;
float weightAll = 12.600F;
//標題
//標題第一行
string title = “鋼板入庫計量單”;
string BillCode = “吊號:” + “20080505170”;
string ClassType = “班別:” + “乙”;
string type1 = “類型:” + “定軋”;
string OrderNum = “記錄單號:” + “QW-Y14-02-06”;
//標題第二行
string ArriveStation = “到站:”;
string SpLine = “專用線:”;
string SaleNo = “銷售訂單號:”;
//標題第三行
string AcceptName = “收貨單位:” + “XXXX/12*2438*9144收二支”;
string time = DateTime.Now.Year.ToString() + “年” + DateTime.Now.Month.ToString().PadLeft(2,’0′)+”月”+DateTime.Now.Day.ToString()+”日”;
//表格下數據
string BoardNum = “合計:” + num.ToString() + ” 塊 ” + weightAll.ToString()+” 噸 “;
string CheckMan1 = “檢查員:” + “”;
string MeName = “計量員:”+”XX”;
string CheckMan2 = “核對員:” + “”;
//線條長度
Pen line = new Pen(drawBrush, 1);
//繪圖–字的位置
//繪圖–總標題
e.Graphics.DrawString(title, drawFont, drawBrush, 400, 45, drawFormat);
//繪圖–標題–第一行
//吊號
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);
//繪圖–標題–第二行
//到站
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);
//繪圖–標題–第三行
//收貨單位
e.Graphics.DrawString(AcceptName, drawFont, drawBrush, 125, 129, drawFormat);
//時間
e.Graphics.DrawString(time, drawFont, drawBrush, 685, 129, drawFormat);
//繪圖–表格下數據
//合計
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);
//繪圖–內部豎線的位置
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);
//繪圖–內部橫線的位置
//根據塊數計算表格高度
if (num != 0)
cellheigh = tableHeight / (float)num;
else
cellheigh = 0;
//繪圖–內部橫線的位置
for (int i = 1; i < num; i++)
{
if (i == 1)
{
//數據
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#打印繪圖的實現方法
本文地址: http://www.paobuke.com/develop/c-develop/pbk23178.html
相關內容
C#文件上傳與下載的實現方法C#中反射和擴展方法如何運用C# 最齊全的上傳圖片方法淺析JAVA中過濾器、監聽器、攔截器的區別C#中Socket通信用法實例詳解C#基礎語法:Base關鍵字學習筆記Unity3D動態對象優化代碼分享C#中的委托、事件學習筆記
C#打印繪圖的實現方法