如何打印多页的整个表格?

我有一个需要打印的长表。 表单本身可能比屏幕上显示的更大(在将数据加载到表单之前,高度未知)。 我尝试了几种方法,但没有一种方法有效。

下面是我想要完成的最接近的方法。 下面的方法的问题是它只打印屏幕的一部分。 我也不确定它是否捕获完整的表单或用户屏幕上显示的内容。

private void print_Click(object sender, EventArgs e) { PrintScreen(); DialogResult result = printDialog1.ShowDialog(); if (result == DialogResult.OK) { printDocument1.Print(); } } [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop); private void PrintScreen() { Graphics mygraphics = this.CreateGraphics(); Size s = this.Size; memoryImage = new Bitmap(s.Width, s.Height, mygraphics); Graphics memoryGraphics = Graphics.FromImage(memoryImage); IntPtr dc1 = mygraphics.GetHdc(); IntPtr dc2 = memoryGraphics.GetHdc(); BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376); mygraphics.ReleaseHdc(dc1); memoryGraphics.ReleaseHdc(dc2); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawImage(memoryImage, 0, 0); } 

请帮忙。

如果您尝试将WinForm的内容打印到打印机,则将UI控件绘制到打印机不起作用。 你不能像屏幕一样resize或滚动一张纸。 你会发现在屏幕上看起来不错的东西在纸上看起来不太好; 字体是像素化的,渐变是抖动的,文本被切断,屏幕上看起来很好的布局将在纸上的疯狂位置打印。

您将需要使用System.Drawing.Printing类来确定打印机指标,如页面大小,颜色,边距等。 然后,将输出划分为适合页面的文本段落或图形切片。 如果段落或图块溢出页面,请确定如何拆分它并在多个页面上打印一个部分。 PrintDocument文档中有一个打印多个页面的示例。

除了最简单的输出之外,这将是很多工作。 您最好使用报表引擎(如Report Viewer或nReports)生成PDF或RDL输出并将其发送到打印机。