手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

实现报表自定义打印功能

时间:2024-10-12 11:31:56

1、首先,创建一个商品清单报表,该报表运行之后有两页内容,第二页数据没有占满整张纸,我们希望在显示时就不显示空白区域,如下样式:

实现报表自定义打印功能

2、首先,在报表的ReportEnd事件中动态设置最后一页的纸张高度。基本思路是记录每页中数据占用的实际高度,在ReportEnd事件中设置最后一页的高度:// 记录当前敌颉缪莽页实际高度public float ActualPageHeight = 0;private void SectionReport1_ReportEnd(object sender, EventArgs e){ // 在报表运行结束之后调整最后一页的高度 ActualPageHeight = ActualPageHeight + this.PageSettings.Margins.Top + this.PageSettings.Margins.Bottom + this.pageHeader.Height; this.Document.Pages[this.Document.Pages.Count - 1].Height = ActualPageHeight;}private void detail_AfterPrint(object sender, EventArgs e){ // 记录每条数据占用的高度 ActualPageHeight += this.detail.Height;}private void SectionReport1_PageStart(object sender, EventArgs e){ // 每页开始时对实际高度做清零操作 ActualPageHeight = 0;}

3、然后,我们实现连续打印操作,在打印时需要将报表内容分为两部分进行打印,代码如下:private void 连续打印ToolStripMenuItem_潮贾篡绐Click(object sender, EventArgs e){ // 连续打印报表 GrapeCity.ActiveReports.Document.Section.Page aPage = new GrapeCity.ActiveReports.Document.Section.Page(); aPage.Width = rpt.PageSettings.PaperWidth; aPage.Height = rpt.PageSettings.PaperHeight; // 创建SystemPrinter对象 GrapeCity.ActiveReports.SystemPrinter m_myARPrinter = new GrapeCity.ActiveReports.SystemPrinter(); m_myARPrinter.PrintDialog(); m_myARPrinter.PaperSize = new System.Drawing.Printing.PaperSize("Custom Size", Convert.ToInt32(aPage.Width * 100), Convert.ToInt32(aPage.Height * 100)); // 启动打印任务 m_myARPrinter.StartJob("连续打印"); System.Drawing.RectangleF rec; int nCount = rpt.Document.Pages.Count; for (int i = 0; i < nCount - 1; i++) { m_myARPrinter.StartPage(); m_myARPrinter.Graphics.PageUnit = GraphicsUnit.Pixel; aPage = rpt.Document.Pages[i]; m_myARPrinter.PaperSize = new System.Drawing.Printing.PaperSize("Custom Size", Convert.ToInt32(aPage.Width * 100), Convert.ToInt32(aPage.Height * 100)); rec = new RectangleF(0, 0, aPage.Width, aPage.Height); aPage.Draw(m_myARPrinter.Graphics, rec, 1f, 1f); m_myARPrinter.EndPage(); } m_myARPrinter.EndJob(); aPage = rpt.Document.Pages[nCount - 1]; m_myARPrinter.PaperSize = new System.Drawing.Printing.PaperSize("Custom Size", Convert.ToInt32(aPage.Width * 100), Convert.ToInt32(aPage.Height * 100)); m_myARPrinter.StartJob("连续打印"); m_myARPrinter.StartPage(); m_myARPrinter.Graphics.PageUnit = GraphicsUnit.Pixel; rec = new RectangleF(0, 0, aPage.Width, aPage.Height); aPage.Draw(m_myARPrinter.Graphics, rec, 1f, 1f); m_myARPrinter.EndPage(); m_myARPrinter.EndJob();}

4、运行效果

实现报表自定义打印功能
© 手抄报圈