Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

c# - Center text for receipt printing

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
------------------------------------------
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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..

enter image description here

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...