中心文字收据打印 [英] Center text for receipt printing

查看:451
本文介绍了中心文字收据打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我用来打印收据从C#某些code。

下面打印的code OK,但我挣扎对齐文本左,右和中央,

 图形显卡= e.Graphics;
字体的字体=新的字体(宋体,10);
浮fontHeight = font.GetHeight();
INT STARTX = 0;
INT startY = 0;
的int偏移量= 0;

graphics.DrawString(欢迎来到MSST,新的字体(宋体,14),新SolidBrush(Color.Black),STARTX,startY +偏移量);
偏移=偏移+ 20;

graphics.DrawString(无回执+ receptno + 1,新的字体(宋体,14),新SolidBrush(Color.Black),STARTX,startY +偏移量);
偏移=偏移+ 20;

graphics.DrawString(日期:+ DateTime.Today,新的字体(宋体,12),新SolidBrush(Color.Black),STARTX,startY +偏移量);
偏移=偏移+ 20;

graphics.DrawString(------------------------------------------,新字体(宋体,10),新SolidBrush(Color.Black),STARTX,startY +偏移量);
偏移=偏移+ 20;
 

谁能帮我用文字对齐?

更新:以下是所需的输出:

欢迎MSST 收据编号:3 日期:2014年5月24日10时06分22秒 ------------------------------------------

解决方案

下面是你的榜样的全code,使用三个 StringFormats 和一个额外的行显示右对齐文本。我还增加了一个前置号码并转换一切彩车 ..我使用的是面板吸取和设置布局矩形到面板的尺寸。你应该使用,当然您的打印目标,..

  INT receptno = 42;
显卡显卡= e.Graphics;

字体font10 =新的字体(宋体,10);
字体font12 =新的字体(宋体,12);
字体font14 =新的字体(宋体,14);

浮主导= 4;
浮lineheight10 = font10.GetHeight()+龙头;
浮lineheight12 = font12.GetHeight()+龙头;
浮lineheight14 = font14.GetHeight()+龙头;

浮STARTX = 0;
浮startY =领先;
浮偏移量= 0;

的StringFormat formatLeft =新的StringFormat(StringFormatFlags.NoClip);
的StringFormat formatCenter =新的StringFormat(formatLeft);
的StringFormat formatRight =新的StringFormat(formatLeft);

formatCenter.Alignment = StringAlignment.Center;
formatRight.Alignment = StringAlignment.Far;
formatLeft.Alignment = StringAlignment.Near;

的SizeF layoutSize =新的SizeF(yourPrintAreaWidth  - 偏移* 2,lineheight14);
的RectangleF布局=新的RectangleF(新的PointF(STARTX,startY +偏移量),layoutSize);

刷= Brushes.Black;

graphics.DrawString(欢迎来到MSST,font14,刷,布局,formatCenter);
偏移=偏移+ lineheight14;
布局=新的RectangleF(新的PointF(STARTX,startY +偏移量),layoutSize);
graphics.DrawString(无回执+ receptno + 1,font14,刷,布局,formatLeft);
偏移=偏移+ lineheight14;
布局=新的RectangleF(新的PointF(STARTX,startY +偏移量),layoutSize);
graphics.DrawString(日期:+ DateTime.Today,font12,刷,布局,formatLeft);
偏移=偏移+ lineheight12;
布局=新的RectangleF(新的PointF(STARTX,startY +偏移量),layoutSize);
graphics.DrawString(。PadRight(46,_),font10,刷,布局,formatLeft);
偏移=偏移+ lineheight10;
布局=新的RectangleF(新的PointF(STARTX,startY +偏移量),layoutSize);

graphics.DrawString(著作权SO,font10,刷,布局,formatRight);
偏移=偏移+ lineheight10;

font10.Dispose(); font12.Dispose(); font14.Dispose();
 

I have some code which I'm using to print a receipt from C#.

The code below prints OK but I'm struggling with aligning the text left right and center,

Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 10);
float fontHeight = font.GetHeight();
int startX = 0;
int startY = 0;
int Offset = 0;

graphics.DrawString("Welcome to MSST", new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;

graphics.DrawString("Recept No :" + receptno + 1, new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;

graphics.DrawString("Date :" + DateTime.Today, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;

graphics.DrawString("------------------------------------------", new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;

Can anyone help me out with text align?

UPDATE: Here is the desired output:

             Welcome to MSST             
Receipt No : 3
Date : 5/24/2014 10:06:22
------------------------------------------

解决方案

Here is the full code of your example, using three StringFormats and an added line to show right aligned text.. I have also added a leading number and converted everything to floats.. I was using a Panel to draw on and set the layout Rectangle to the Panel's dimensions. You should use your printing target, of course..

int receptno = 42;
Graphics graphics = e.Graphics;

Font font10 = new Font("Courier New", 10);
Font font12 = new Font("Courier New", 12);
Font font14 = new Font("Courier New", 14);

float leading = 4;
float lineheight10 = font10.GetHeight() + leading;
float lineheight12 = font12.GetHeight() + leading;
float lineheight14 = font14.GetHeight() + leading;

float startX = 0;
float startY = leading;
float Offset = 0;

StringFormat formatLeft = new StringFormat(StringFormatFlags.NoClip);
StringFormat formatCenter = new StringFormat(formatLeft);
StringFormat formatRight = new StringFormat(formatLeft);

formatCenter.Alignment = StringAlignment.Center;
formatRight.Alignment = StringAlignment.Far;
formatLeft.Alignment = StringAlignment.Near;

SizeF layoutSize = new SizeF(yourPrintAreaWidth - Offset * 2, lineheight14);
RectangleF layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);

Brush  brush = Brushes.Black;

graphics.DrawString("Welcome to MSST", font14, brush, layout, formatCenter);
Offset = Offset + lineheight14;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("Recept No :" + receptno + 1, font14, brush, layout, formatLeft);
Offset = Offset + lineheight14;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("Date :" + DateTime.Today, font12, brush, layout, formatLeft);
Offset = Offset + lineheight12;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("".PadRight(46,'_'), font10, brush, layout, formatLeft);
Offset = Offset + lineheight10;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);

graphics.DrawString("copyright SO", font10, brush, layout, formatRight);
Offset = Offset + lineheight10;

font10.Dispose();   font12.Dispose();  font14.Dispose();

这篇关于中心文字收据打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆