回复 18楼wugrid的帖子
你好,你需要的功能已经实现了。第一行、第二行以及最后一行的打印位置都是需要自己计算的,下面附件的例子里的位置都是按照页面写代码计算的。
使用我在14楼给你的Sample,并调用之前提到的PrintPage事件就可以了。
1.重写PrintDocument,并且暴露一个最后一页的Size的属性。参考代码:
- public Size LastPageSize
- {
- get { return (_images[_images.Count-1] as Image).Size; }
- }
复制代码
2.调用PrintPage事件,并且在该事件里DrawString,把你的那些输出字符串画出来,代码参考:
- void fpd_PrintPage(object sender, PrintPageEventArgs e)
- {
- StringFormat sfCenter = new StringFormat();
- sfCenter.Alignment = StringAlignment.Center;
- string firstLine = "产品入库报表";
- Font fontNr = new System.Drawing.Font("宋体", 16);
- Size fistLineSize = TextRenderer.MeasureText(firstLine, fontNr);
- string secondLineLeft = "销售单位:";
- Font fontNr2 = new System.Drawing.Font("宋体", 12);
- Size secondLineLeftSize = TextRenderer.MeasureText(secondLineLeft, fontNr2);
- string secondLineRight ="日期:"+string.Format("{0:yyyy年MM月dd日}",DateTime.Today);
- Size secondLineRightSize = TextRenderer.MeasureText(secondLineRight, fontNr2);
-
- int left = e.MarginBounds.Left;
- int right = e.MarginBounds.Left + posSize.Width - secondLineRightSize.Width;
- int center = e.MarginBounds.Left + posSize.Width / 2;
- int firstTop = e.MarginBounds.Top - fistLineSize.Height - secondLineRightSize.Height;
- int secondTop = e.MarginBounds.Top - secondLineRightSize.Height;
- e.Graphics.DrawString(firstLine, fontNr, Brushes.Black, center, firstTop, sfCenter);
- e.Graphics.DrawString(secondLineLeft, fontNr2, Brushes.Black, left, secondTop);
- e.Graphics.DrawString(secondLineRight, fontNr2, Brushes.Black, right, secondTop);
- if (!e.HasMorePages)
- {
- string lastLineLeft = "接收人:";
- string LastLineRight = "会计:";
- int lastTop = e.MarginBounds.Top + posSize.Height;
-
- e.Graphics.DrawString(lastLineLeft, fontNr2, Brushes.Black, left, lastTop);
- e.Graphics.DrawString(LastLineRight, fontNr2, Brushes.Black, right, lastTop);
- }
- }
复制代码
3.根据上述代码,你所需的Y的值就是最后一页打印的数据的高度加页面的Margin高度,即e.MarginBounds.Top + posSize.Height。
最后一页效果:
例子在附件里:
|