C# PrintDocument 80 热敏打印机 , 顶部空间如何缩小

private void btnPrint_Click(object sender, EventArgs e)
{
if (selectedCustomers == null)
{
this.ShowErrorTip("请选择打印的客户!");
return;
}
string documentName = selectedCustomers.Name;
string printName = IniSettings.GetPrint();
if (printName == null || string.IsNullOrEmpty(printName))
{
this.ShowErrorTip("请先在设置中,配置打印机!");
return;
}
var flag = PosPrint(printName, documentName);
if (flag)
{
printDataList.ForEach(n =>
{
PrintReadControlsRemove(n.ContentId.ToString());
PrintReadControlsRemove(n.CategoryId.ToString());
});
printDataList.Clear();
Task.Factory.StartNew(() => ActionQueryStorysByCategoryId(selectedCustomers.Id, selectedCategoryId));
this.ShowSuccessTip("打印成功");
}
else
{
this.ShowErrorTip("打印失败,请重试");
}
}
public bool PosPrint(string PrintName = "", string documentName = "", int num = 1)
{
using (PrintDocument printDocument = new PrintDocument())
{
try
{
printDocument.DocumentName = documentName;
printDocument.PrintController = new StandardPrintController();
printDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
if (!string.IsNullOrEmpty(PrintName))
{
printDocument.PrinterSettings.PrinterName = PrintName;
}
//Copies属性不生效 改用for循环执行print打印多份
//if (num > 1 && num < 32768)//超过一份才赋值,否则不是赋值,就算不赋也是默认1份,不这样判断时常会报SIZE错误
//printDocument.PrinterSettings.Copies = Convert.ToInt16(num);
printDocument.PrintPage += new PrintPageEventHandler(this.PosPrintPage);
if (printDocument.PrinterSettings.IsValid)
{
printDocument.Print();
return true;
}
return false;
}
catch (Exception e)
{
this.ShowErrorTip(e.Message);
return false;
}
}
}
private void PosPrintPage(object sender, PrintPageEventArgs e)
{
if (selectedCustomers == null)
{
this.ShowErrorTip("请选择打印的客户!");
return;
}
string documentName = selectedCustomers.Name;
int Ltop = 0;
float width = e.PageSettings.PaperSize.Width - e.PageSettings.Margins.Left - e.PageSettings.Margins.Right;//计算出有效打印区域的宽度
int TitleHeight = 0;
Graphics graphics = CreateGraphics();
string FontFamily = "微软雅黑";
// 需求 字体大小
float PrtFont = 10;
// 类型 字题大小
float TitlePrtFont = 12;
// 字体间距
int spacing = 6;
// 左边间距
int leftSpacing = 0;
// 右边间距
int rightSpacing = 0;
Font _font = new Font(FontFamily, PrtFont);
SolidBrush solidBrush = new SolidBrush(System.Drawing.Color.Black);
// 客户
string Title = documentName;
_font = new Font(FontFamily, TitlePrtFont, FontStyle.Bold);
SizeF sizeF = graphics.MeasureString(Title, _font);
TitleHeight = Convert.ToInt32(sizeF.Height) - 25;
e.Graphics.DrawString(Title, _font, solidBrush, leftSpacing, Ltop + TitleHeight); //绘制字符串
// 日期
DateTime now = DateTime.Now;
string dateFormat = now.ToString("yyyy/MM/dd");
_font = new Font(FontFamily, TitlePrtFont, FontStyle.Bold);
sizeF = graphics.MeasureString(dateFormat, _font);
TitleHeight = Convert.ToInt32(sizeF.Height) - 25;
float DateTitleWidth = e.PageSettings.PaperSize.Width - e.PageSettings.Margins.Left - e.PageSettings.Margins.Right - sizeF.Width;
e.Graphics.DrawString(dateFormat, _font, solidBrush, DateTitleWidth, Ltop + TitleHeight); //绘制字符串
string LogoPath = Application.StartupPath + "Resources\\split.png";
System.IO.FileStream fs = null;
fs = new System.IO.FileStream(LogoPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
Image Logo = System.Drawing.Image.FromStream(fs);
fs.Close();
float LogoTitleWidth = e.PageSettings.PaperSize.Width - e.PageSettings.Margins.Left - e.PageSettings.Margins.Right;
Logo = resizeImage(Logo, new Size((int)LogoTitleWidth, 20)); //调整img大小
TitleHeight = TitleHeight + 35;
e.Graphics.DrawImage(Logo, 0, TitleHeight);
if (printDataList == null)
{
e.HasMorePages = false;
return;
}
TitleHeight = TitleHeight - 17;
foreach (PrintData printData in printDataList)
{
if (printData.DataType == 0)
{
string CategoryName = printData.CategoryName;
_font = new Font(FontFamily, TitlePrtFont, FontStyle.Bold);
SizeF size = graphics.MeasureString(CategoryName, _font);
// 加35 黑色横线间隔
TitleHeight = TitleHeight + Convert.ToInt32(size.Height) + spacing;
e.Graphics.DrawString(CategoryName, _font, solidBrush, leftSpacing, Ltop + TitleHeight); //绘制字符串
}
else
{
string ContentName = printData.sequence + "、" + printData.ContentName;
_font = new Font(FontFamily, PrtFont, FontStyle.Regular);
SizeF ContentName_SizeF = graphics.MeasureString(ContentName, _font);
if (ContainsNewLine(ContentName.ToString()))
{
string newContentName = ContentName.Replace("\r\n", string.Empty);
newContentName = ContentName.Replace("\n", string.Empty);
ContentName_SizeF = graphics.MeasureString(newContentName, _font);
}
if (ContentName_SizeF.Width > width)
{
// 计算纸的有效宽度
int z_width = (int)width - leftSpacing - rightSpacing - 22;
// 新行
StringBuilder new_row = new StringBuilder();
foreach (char c in ContentName)
{
new_row.Append(c);
SizeF new_row_SizeF = graphics.MeasureString(new_row.ToString(), _font);
if ((int)Math.Ceiling((double)new_row_SizeF.Width) > z_width)
{
new_row.Append(Environment.NewLine);
TitleHeight = TitleHeight + Convert.ToInt32(ContentName_SizeF.Height) + spacing;
e.Graphics.DrawString(new_row.ToString(), _font, solidBrush, leftSpacing, Ltop + TitleHeight); //绘制字符串
new_row.Clear();
}
else if (ContainsNewLine(c.ToString()))
{
TitleHeight = TitleHeight + fontHeight;
new_row.Clear();
}
}
if (new_row.Length > 0)
{
TitleHeight = TitleHeight + Convert.ToInt32(ContentName_SizeF.Height) + spacing;
e.Graphics.DrawString(new_row.ToString(), _font, solidBrush, leftSpacing, Ltop + TitleHeight); //绘制字符串
}
//// "测" 一个字的宽度
//SizeF Test_SizeF = graphics.MeasureString("测", _font);
//// 每行可以放的 字数
//int wordQty = (int)Math.Ceiling((double)width / Test_SizeF.Width);
//if (width == 284)
//{
// wordQty = 17;
//}
//// 行数
//int row = (int)Math.Ceiling((double)ContentName .Length / wordQty);
//int length = wordQty;
//int startIndex = 0;
//for (int i = 0; i < row; i++)
//{
// startIndex = i * wordQty;
// if (ContentName.Length - startIndex <= length)
// {
// length = ContentName.Length - startIndex;
// }
// string newContentName = ContentName.Substring(startIndex, length);
// _font = new Font(FontFamily, PrtFont, FontStyle.Regular);
// SizeF newContentName_SizeF = graphics.MeasureString(newContentName, _font);
// TitleHeight = TitleHeight + Convert.ToInt32(newContentName_SizeF.Height) + spacing;
// e.Graphics.DrawString(newContentName, _font, solidBrush, leftSpacing, Ltop + TitleHeight); //绘制字符串
//}
}
else
{
TitleHeight = TitleHeight + Convert.ToInt32(ContentName_SizeF.Height) + spacing;
e.Graphics.DrawString(ContentName, _font, solidBrush, leftSpacing, Ltop + TitleHeight); //绘制字符串
}
}
}
// 增加票尾空行,方便切纸
//int FootEmptyLine = 3;
//if (FootEmptyLine > 0)
//{
// string EndRow = "";
// SizeF Test_SizeF = graphics.MeasureString("测", _font);
// for (int a = 0; a < FootEmptyLine; a++)
// {
// if (a == FootEmptyLine - 1)
// EndRow += (". \r\n");
// else
// EndRow += (" \r\n");
// }
// e.Graphics.DrawString(EndRow, _font, solidBrush, 0, Ltop + TitleHeight); //绘制字符串
//}
e.HasMorePages = false;
}