我有两个字符串和一个图像,我想要在物理上打印出来。我遇到的问题是,每当文本太长时,它看起来都是中心。我有以下代码,使文本“居中”,使其位于图像上方,并将第二个文本定位在图像的左下角。
graphic.DrawString(textBox1.Text, font, Brushes.Black, pageWidth*0.5f,pageHeight*0.1f);
graphic.DrawString(textBox2.Text, fontSmall, Brushes.Black, pageWidth*0.27f, pageHeight*0.52f);
如何使文本始终居中,例如,如果您在单词中使用中间对齐,则会将文本空格化,以使其双方都有,而不仅仅是一个?
float pageWidth = e.PageSettings.PrintableArea.Width;
float pageHeight = e.PageSettings.PrintableArea.Height;
Font font = new Font("Arial", 90, FontStyle.Bold);
Font fontSmall = new Font("Arial", 30, FontStyle.Bold);
StringFormat str = new StringFormat(StringFormatFlags.NoClip);
str.Alignment = StringAlignment.Center;
Graphics graphic = e.Graphics;
SizeF textSize = new SizeF(pageHeight, font.Height);//pageWidth*0.2f);
SizeF barcodeSize = new SizeF(pageHeight * 0.9f, pageWidth * 0.45f);
RectangleF textBig = new RectangleF(new PointF(0f,pageWidth*0.1f), textSize);
RectangleF barcodeImageRec = new RectangleF(new PointF((pageHeight-pageHeight*0.9f)/2, textBig.Bottom), barcodeSize);
RectangleF textSmall = new RectangleF(new PointF(barcodeImageRec.Left+50, barcodeImageRec.Bottom), textSize);
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
b.EncodedImage?.Dispose();
graphic.DrawString(textBox1.Text.ToUpper(), font, Brushes.Black, textBig, str);
b.Encode(TYPE.CODE128A, textBox2.Text.ToUpper(), Color.Black, Color.Transparent);
graphic.DrawImage(b.EncodedImage, barcodeImageRec);
str.Alignment = StringAlignment.Near;
graphic.DrawString(textBox2.Text.ToUpper(), fontSmall, Brushes.Black, textSmall);
font.Dispose();
fontSmall.Dispose();
上面你可以看到我在打印过程中使用的编辑过的代码,有没有更好的方法呢?