如何打印多页的整个表格? [英] How do you print Entire Form with multiple pages?

查看:176
本文介绍了如何打印多页的整个表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的表格需要打印。表单本身可以大于屏幕上显示的内容(在将数据加载到表单中之前,高度是未知的)。我尝试了几种方法,但是都没有用。



以下是我尝试完成的最接近的方法。以下方法的问题在于它仅打印屏幕的一部分。我也不确定它是捕获完整表单还是捕获用户屏幕上可见的全部内容。

  private void print_Click(对象发送者,EventArgs e)
{
PrintScreen();
DialogResult结果= printDialog1.ShowDialog();
if(结果== 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 =新的位图(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控件。您无法像屏幕一样调整大小或滚动纸张。您会发现在屏幕上看起来不错的东西在纸上看起来不好;字体是像素化的,渐变是抖动的,文本将被截断,在屏幕上看起来不错的布局将打印在纸上的疯狂位置。



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



除了最简单的输出以外,任何其他操作都将需要大量工作。使用诸如报表查看器 nReports 生成PDF或RDL输出并将其发送到打印机。 p>

I have a long form that needs to be printed. The form itself can be larger than what is displayed on screen (the height is unknown until data is loaded into the form). I have tried several methods but none has worked.

Below is the closest method to what I'm trying to accomplish. The problem with the method below is that it only prints the part of the screen. I'm also not sure if it captures the full form or just what is visible on the user's screen.

    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);
        }

Please help.

解决方案

If you are trying to print the contents of a WinForm to a printer, drawing UI controls to the printer is not going to work. You can't resize or scroll a piece of paper like you can a screen. You will find that what looks good on screen does not look good on paper; fonts are pixelated, gradients are dithered, text gets cut off, layout that looks good on screen will print at crazy locations on paper.

You will want to use the System.Drawing.Printing classes to determine the printer metrics like page size, color, margins and so on. Then, divide your output into text paragraphs or graphic tiles that fit into the page. If a paragraph or tile overflows a page, decide how to split it and print a section on multiple pages. The PrintDocument documentation has an example of printing over multiple pages.

For anything but the simplest output this is going to be a lot of work. You may be better off using a reporting engine like Report Viewer or nReports to generate PDF or RDL output and send that to a printer.

这篇关于如何打印多页的整个表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆