您的位置:首页 > 编程语言 > C#

c# print right left up down

2016-11-04 16:32 155 查看
```
private void button1_Click(object sender, EventArgs e)
{

System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.DocumentName = pd.PrinterSettings.MaximumCopies.ToString();
pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage);
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
pd.Print();
}

private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int w = 30;
int h = 100;

string ss = "实现真正意义的校园一卡通,就需要做到各类关联系统的学生、教师身份统一认证,最好能做到比如饭卡系统";
Rectangle fontRectanle = new Rectangle(50, 20, w, h);

Font f = new Font("宋体", 14);

e.Graphics.PageUnit = System.Drawing.GraphicsUnit.Millimeter;
e.Graphics.DrawRectangle(new System.Drawing.Pen(Brushes.Black), fontRectanle);//画边框

DrawStringWrap(e.Graphics, f, ss, fontRectanle);

}

/// <summary>
/// 绘制文本自动换行(超出截断)
/// </summary>
/// <param name=\"graphic\">绘图图面</param>
/// <param name=\"font\">字体</param>
/// <param name=\"text\">文本</param>
/// <param name=\"recangle\">绘制范围</param>
private void DrawStringWrap(Graphics graphic, Font font, string text, Rectangle recangle)
{
List<string> textRows = GetStringRows(graphic, font, text, recangle);

int rowHeight = (int)(Math.Ceiling(graphic.MeasureString("测", font).Height));

int wordWidth = (int)(Math.Ceiling(graphic.MeasureString("测", font).Width));//计算一个字的宽度

int top = recangle.Top;// (recangle.Height - rowHeight * drawRowCount) / 2;
int left = recangle.Left + recangle.Width - wordWidth;

StringFormat drawFormat = new StringFormat(StringFormatFlags.DirectionVertical);
for (int i = 0; i < textRows.Count; i++)
{

Rectangle fontRectanle = new Rectangle(left - wordWidth * i, top, wordWidth, recangle.Height);
graphic.DrawString(textRows[i], font, new SolidBrush(Color.Black), fontRectanle, drawFormat);

}

}

/// <summary>
/// 将文本分行
/// </summary>
/// <param name=\"graphic\">绘图图面</param>
/// <param name=\"font\">字体</param>
/// <param name=\"text\">文本</param>
/// <param name=\"width\">行宽</param>
/// <returns></returns>
private List<string> GetStringRows(Graphics graphic, Font font, string text, Rectangle recangle)
{

int wordWidth = (int)(Math.Ceiling(graphic.MeasureString("测", font).Height));//计算一个字的高度

int rows = recangle.Height / wordWidth; //计算能显示多少行

rows += (int)(rows * 0.5);//0.5是各系数,应能定义

int cols = text.Length / rows;//计算需要分多少列
if (text.Length % rows >= 0)
{
cols++;
}

List<string> textRows = new List<string>();

string tem = text;
for (int index = 0; index < cols; index++)
{
try
{
textRows.Add(tem.Substring(index * rows, rows));
}
catch
{
textRows.Add(tem.Substring(index * rows));
}
}

return textRows;
}


“`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#