using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Media; using System.Globalization; namespace Utils.Printers { public class PrinterUtil {//TODO:改为文字受宽度限制:即当文字超长时,截断超长部分 /// /// 绘制文字(打印用) /// /// 文字左上角的起始坐标(不是文字的实际坐标,实际坐标还受到左边距和行距的影响) /// DrawingContext /// 文字 /// 字的大小 /// 左边距。文字最左侧的距起始坐标左侧的距离 /// 行距 /// Typeface /// 下一行文字的起始Y坐标位置 /// 文字最右上角的坐标 public static Point DrawText(Point startPoint, DrawingContext dc, string text, double fontSize, double textLeftMargin, double lineDistance, Typeface typeFace, out double nextLineY) { var ft = GetFormattedText(text, typeFace, fontSize); double textHeight; if (string.IsNullOrEmpty(text)) textHeight = GetFormattedText("龙", typeFace, fontSize).Height; else textHeight = ft.Height; dc.DrawText(ft, new Point(startPoint.X + textLeftMargin, startPoint.Y + lineDistance / 2)); nextLineY = startPoint.Y + lineDistance + textHeight; return new Point(startPoint.X + ft.Width + textLeftMargin, startPoint.Y); } /// /// 绘制文字(打印用) /// /// 文字左上角的起始坐标(不是文字的实际坐标,实际坐标还受到左边距和行距的影响) /// DrawingContext /// 文字 /// 字的大小 /// 行距 /// Typeface /// 下一行文字的起始Y坐标位置 /// 文字最右上角的坐标 public static Point DrawText(Point startPoint, DrawingContext dc, string text, double fontSize, double lineDistance, Typeface typeFace, out double nextLineY) { return DrawText(startPoint, dc, text, fontSize, 0, lineDistance, typeFace, out nextLineY); } /// /// 绘制文字(打印用) /// /// 文字左上角的起始坐标(不是文字的实际坐标,实际坐标还受到左边距和行距的影响) /// DrawingContext /// 文字 /// 字的大小 /// 行距 /// Typeface /// 文字最右上角的坐标 public static Point DrawText(Point startPoint, DrawingContext dc, string text, double fontSize, double lineDistance, Typeface typeFace) { double nextLineY; return DrawText(startPoint, dc, text, fontSize, 0, lineDistance, typeFace, out nextLineY); } /// /// 绘制带框的文字(打印用) /// /// 框的左上角的坐标 /// DrawingContext /// 文字 /// 字的大小 /// Typeface /// 框的宽度 /// 框的粗细 /// 文字左侧到框的距离 /// 行距 /// 框的底线的Y坐标 /// 框的右上角坐标 public static Point DrawTextWithRectangle(Point startPoint, DrawingContext dc, string text, double fontSize, Typeface typeFace, double rectangleWidth, double rectangleThickness, double textLeftMargin, double lineDistance, out double nextLineY) { var rightPoint = DrawText(startPoint, dc, text, fontSize, textLeftMargin, lineDistance, typeFace, out nextLineY); dc.DrawRectangle(null, new Pen(Brushes.Black, rectangleThickness), new Rect(startPoint.X, startPoint.Y, rectangleWidth, nextLineY - startPoint.Y)); return new Point(startPoint.X + rectangleWidth, startPoint.Y); } /// /// 绘制带框的文字(打印用) /// /// 框的左上角的坐标 /// DrawingContext /// 文字 /// 字的大小 /// Typeface /// 框的宽度 /// 框的粗细 /// 文字左侧到框的距离 /// 文字顶部到框的距离 /// 框的右上角坐标 public static Point DrawTextWithRectangle(Point startPoint, DrawingContext dc, string text, double fontSize, Typeface typeFace, double rectangleWidth, double rectangleThickness, double textLeftMargin, double lineDistance) { double nextLineY; return DrawTextWithRectangle(startPoint, dc, text, fontSize, typeFace, rectangleWidth, rectangleThickness, textLeftMargin, lineDistance, out nextLineY); } /// /// 按指定样式格式化字符 /// public static FormattedText GetFormattedText(string text, Typeface typeFace, double fontSize) { return new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeFace, fontSize, Brushes.Black); } /// /// 获取文字的宽度 /// /// 文字个数 /// 字号 /// Typeface /// 文字的宽度 public static double GetWidth(int charCount, double fontSize, Typeface typeFace) { string text = string.Empty; for (int i = 0; i < charCount; i++) text += "字"; var ft = GetFormattedText(text, typeFace, fontSize); return ft.Width; } } }