vb.net 打印可滚动表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15275563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing scrollable form
提问by kfirba
I have a scroll-able form which I would like to print entirely.
我有一个可滚动的表格,我想完全打印它。
I have already tried using this code to print it:
我已经尝试使用此代码来打印它:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Me.PrintForm.PrintAction = Printing.PrintAction.PrintToPreview
Me.PrintForm.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
End Sub
And the result isn't accurate at all.
而且结果根本不准确。
To demonstrate my issue, here are some photos:
为了证明我的问题,这里有一些照片:
This is the result I want (Of course I want it to ALSO print all of the scroll-able content)
这是我想要的结果(当然我希望它也打印所有可滚动的内容)
As you can see, This image contains all of the width I need for the image, but because it's a print screen image, it doesn't contain the scroll-able area which I would like to have in my printable version of the form.

如您所见,此图像包含我需要的所有宽度图像,但由于它是打印屏幕图像,它不包含我希望在表单的可打印版本中具有的可滚动区域。

and this is what I get from my code:
这是我从我的代码中得到的:
As you can see here, I only get about 60% of the form width, and 50% of the height, and clearly, I don't get the scroll-able area.
正如你在这里看到的,我只得到了大约 60% 的表单宽度和 50% 的高度,很明显,我没有得到可滚动区域。


I don't really care about the quality I just want it to print the whole form including the Scroll-able area.
我并不真正关心质量,我只是希望它打印整个表单,包括可滚动区域。
回答by Neolisk
In this line:
在这一行:
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
It looks like PrintOption.Scrollableis only going to work, if you have a scrollable form. You have a scrollable controlhere (probably a Panel), inside a form. In this case its area will not be expanded onto the printer. Compare:
PrintOption.Scrollable如果您有可滚动的表单,它看起来只会起作用。您在这里有一个可滚动控件(可能是一个Panel),在一个表单中。在这种情况下,它的区域不会扩展到打印机上。相比:
Scrollable control:
可滚动控件:


prints as:
打印为:


Scrollable form:
可滚动形式:


prints as:
打印为:


According to this official answer from Microsoft, capturing a scrollable control is not possible with PrintForm. If you use a PrintDocument and some custom coding, you can do it for simple cases, such as a scrollable TextBox. In your case you may need even more custom coding to be done. Handling PrintDocument1.PrintPagelooks like the best place to start, if you are up to it.
根据微软的这个官方回答,使用PrintForm. 如果您使用PrintDocument 和一些自定义编码,则可以针对简单情况执行此操作,例如可滚动的 TextBox。在您的情况下,您可能需要完成更多的自定义编码。PrintDocument1.PrintPage如果您愿意,处理看起来是最好的起点。
回答by GojiraDeMonstah
Based on what you're showing... it looks like the scrollable area is a container like a Panel. If that's the case, printing the form isn't the problem, it's printing the scrollable control.
根据您显示的内容......看起来可滚动区域是一个像面板一样的容器。如果是这种情况,打印表单不是问题,而是打印可滚动控件。
Have a look at this project to create a bitmap from a control: http://www.codeproject.com/Articles/35734/Print-a-WinForms-User-Control
看看这个项目从控件创建位图:http: //www.codeproject.com/Articles/35734/Print-a-WinForms-User-Control
EDIT: On second thought, I don't think the code at that link addresses the actual scrolling problem either.
编辑:再想一想,我认为该链接上的代码也没有解决实际的滚动问题。
I think you're going to need to do one of two things: 1) Temporarily resize the panel large enough that the scrollbars disappear then resize it back 2) Build a control (perhaps a "Printable Version" form) that doesn't have nested scrollable elements and gracefully deals with stuff like pagination.
我认为你需要做两件事之一:1) 暂时调整面板的大小,使滚动条消失,然后重新调整大小 2) 构建一个没有的控件(可能是一个“可打印版本”表单)嵌套的可滚动元素并优雅地处理分页等内容。
Option #2 might sound like a lot of work, but I think you could pretty quickly do something like: create a new panel, clone each control you want printed and add it to the panel (resizing as necessary to avoid scrolling), print the panel, then dispose of the panel.
选项 #2 听起来可能需要做很多工作,但我认为您可以很快地执行以下操作:创建一个新面板,克隆您想要打印的每个控件并将其添加到面板(根据需要调整大小以避免滚动),打印面板,然后处理面板。
回答by Mohsen Heydari
I Think your answer is in the article
All you need is to reference to FormPrinting library (or import the source to your solution).
我认为您的答案在文章中
您只需要参考 FormPrinting 库(或将源导入到您的解决方案中)。
Private Sub btnPrint_Click(object sender, EventArgs e)
{
var fp = new FormPrinting.FormPrinting(this);
fp.Print();
}
will do the printing job.
I have tested the library, having no problems with scrollable contents like images and ....
将做打印工作。
我已经测试了该库,对图像和...等可滚动内容没有问题。
回答by bstiffler582
I had a similar problem I was able to solve without any additional libraries or extensions. It is simple with the DrawToBitmap method available on most Forms controls.
我有一个类似的问题,我无需任何额外的库或扩展即可解决。大多数 Forms 控件都提供 DrawToBitmap 方法,这很简单。
Dim ctrlColl As ControlCollection
Dim i As Integer = 0
' Get collection of controls
ctrlColl = Me.Controls
' create bitmap array
Dim Bitmaps(ctrlColl.Count - 1) As Bitmap
' remove controls you have hidden before printing
For Each ctrl As Control In ctrlColl
If Not ctrl.Visible Then
ctrlColl.Remove(ctrl)
End If
Next
' Loop through controls
For Each ctrl As Control In ctrlColl
' create bitmap from control.DrawToBitmap
Bitmaps(i) = New Bitmap(ctrl.Width, ctrl.Height)
ctrl.DrawToBitmap(Bitmaps(i), ctrl.ClientRectangle)
i = i + 1
Next
' Print each bitmap in array
i = 0
For Each bmp As Bitmap In Bitmaps
e.Graphics.DrawImage(bmp, New Point(ctrlColl(i).Location.X, ctrlColl(i).Location.Y))
i = i + 1
Next
End Sub
回答by Kasnady
Check for the MSDN Forum of the code herethe code like this
检查代码的MSDN论坛这里的代码是这样的
In the Toolbox, click the Visual Basic PowerPacks tab and then drag the PrintForm component onto the form.
The PrintForm component will be added to the component tray.
In the Properties window, set the PrintAction property to PrintToPrinter.
在工具箱中,单击 Visual Basic PowerPacks 选项卡,然后将 PrintForm 组件拖到窗体上。
PrintForm 组件将添加到组件托盘中。
在“属性”窗口中,将 PrintAction 属性设置为 PrintToPrinter。
Add the following code in the appropriate event handler (for example, in the Click event handler for a Print Button).
在适当的事件处理程序中添加以下代码(例如,在打印按钮的 Click 事件处理程序中)。
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
and heregot the same question as your and have been answer.
而在这里得到了作为你同样的问题,并已回答。

