Printing scrollable form

半城伤御伤魂 提交于 2019-12-11 08:45:27

问题


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.

I don't really care about the quality I just want it to print the whole form including the Scroll-able area.


回答1:


I Think your answer is in the article
All you need is to reference to FormPrinting library (or import the source to your solution).

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 ....




回答2:


In this line:

PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

It looks like PrintOption.Scrollable is only going to work, if you have a scrollable form. You have a scrollable control here (probably a Panel), inside a form. In this case its area will not be expanded onto the printer. Compare:

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.PrintPage looks like the best place to start, if you are up to it.




回答3:


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

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.

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.




回答4:


Check for the MSDN Forum of the code here the code like this

  1. In the Toolbox, click the Visual Basic PowerPacks tab and then drag the PrintForm component onto the form.

  2. The PrintForm component will be added to the component tray.

  3. In the Properties window, set the PrintAction property to PrintToPrinter.

Add the following code in the appropriate event handler (for example, in the Click event handler for a Print Button).

PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

and here got the same question as your and have been answer.




回答5:


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.

    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


来源:https://stackoverflow.com/questions/15275563/printing-scrollable-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!