Printing a panel

Android

Active member
Joined
Mar 6, 2007
Messages
35
Programming Experience
1-3
I already have the code to print the contents of a panel, but if the panel to too big to fit on one page then it just cuts it off. Is there anyway that I cant make it so that if it is too big then it will go onto multiple pages?

The size of the panel varies depending on how much content is in it which could be anything from a couple of lines to several pages, however whenever there is more than one page anything below that is cut off from the printout.
 
Tell more. How do you print panel - image of panel or text and indidual parts to compose page?
 
Here is the code that I found for printing the panel. Is there a better way of doing it?

VB.NET:
Private Function GetControlImage(ByVal ctlIn As Control) As Image
        Dim Imageproperty As System.Reflection.PropertyInfo
        Imageproperty = ctlIn.GetType.GetProperty("Image", GetType(Image))
        If Imageproperty Is Nothing Then
            Dim imgNew As New Bitmap(ctlIn.Width, ctlIn.Height)
            ctlIn.DrawToBitmap(imgNew, ctlIn.ClientRectangle)
            Return imgNew
        Else
            Return CType(Imageproperty.GetValue(ctlIn, Nothing), Image)
        End If
    End Function

    Private Sub PrintPageHandler(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        Dim canvas As Graphics = e.Graphics
        Dim img As Image

        img = GetControlImage(Panel1)
        canvas.DrawImage(img, 0, 0)
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        'Start a new printdocument and printdialog
        Dim doc As PrintDocument = New PrintDocument
        Dim printer As PrintDialog = New PrintDialog
        'Set the document name
        doc.DocumentName = "Job Sheet"
        'Set the printed document
        printer.Document = doc
        'If the printer dialog returns ok
        If printer.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'Print the page
            AddHandler doc.PrintPage, AddressOf PrintPageHandler
            doc.Print()
        End If
    End Sub
 
Ok, you have a single image and it don't fit the page.

You can choose to shrink the image to fit, but I guess you would have asked to do this if that was what you wanted, so scratch that.

PrintPage parameter e.MarginBounds is the page rectangle. Image Size property or GetBbounds method will tell you how large image is. You have to calculate these two and see how much room there is. For example page height is 400 and image is height 800 means this makes 2 pages in height.

You basically have two options to draw "the 2 pages image", either chop it up in pieces and make PrintPage method draw one page for each piece, or dynamically draw the parts to each page from the same source image. This is more or less just different ways of organizing the code, methods used will be the same.

Either way you need to learn about e.HasMorePages event property of PrintPage. If you according to your logic set this True the PrintPage method is called again. If you set this False (or don't set it) the print job is finished. When drawing multiple pages with PrintPage method it is common to use some counter variable declared outside the method to keep track of where in the job you are.

The Graphics.DrawImage method is overloaded, meaning you can call many different versions of this method with different parameters. To draw another part of source image you must use a DrawImage method where you can specify source rectangle. The documentation describe all 30 versions of DrawImage method, look it up.

What I have described is all the programming pieces of this puzzle, it is what has to be put together to print one image over several pages. Try and explore these and ask again if any of them is uncertain to you.
 
Thanks for all of the help, I have this working now the only problem is that some lines of text get cut in half at a page break. Is their no better way to print than this?

Thanks
 
There is, instead if printing an image of what is displayed on screen you should print the data that is displayed according to how you want is formatted in report format. Other than DrawImage method you also have lots of other methods available from Graphics class, for example DrawString. It is some more work, especially getting formatting nice, but if you do this you have in effect created a printing template that you can use again and again, also with every other application you use same type data. You may also be able to find free public classes written by other people that let you print standard reports for standard data sources, for example for any data in a DataTable or DataSet. More Database related you have for example Crystal Reports. Sometimes it is even easier to export data to some file and let some other application print it, like some people find it easier to export pure data to Xml and create a reporting template with Xsl which together is transformed to Html that can be displayed/printed with any browser.
 
Thanks, you've been a great help, used both drawimage like you said above for one part of my program and was able to adapt it and use drawstring for another part that printed only text. I made it print 85 lines on a page so there is no chopping up of lines.

Thanks again :)
 
Great! With the Graphics.MeasureString method you can also calculate how much space a string will take given a Font and for example limited width. Checking available space (MarginBounds) lets you dynamically print variable number of lines each page. Remember users normally have the printer options available and use different types of paper and margins and paper layout (portrait/landscape).
 
Back
Top