שתף באמצעות


PowerPacks in VB: PrintForm Scaling and full page printing

Question

Sunday, October 11, 2015 10:27 PM

Hello.

I'm having a problem with properly scaling a Visual Basic form. I'm using the PowerPacks PrintForm component, and I would like to be able to print the whole entire form on original paper size (8.5 in x 11 in). Visual Studio doesn't allow me to raise the form size above what the screen can show. In all cases, even with scrollable forms when the print action is set to Scrollable, the bottom quarter of the page is cut off because PrintForm only wants to print what is visible on the screen.

Help with this matter is appreciated.

Thanks.

Bgeo25

All replies (23)

Monday, October 12, 2015 1:30 AM ✅Answered

Well,  you could do it this way,  however,  you would need to redo a bit of your code.  Set the form`s AutoScroll property to True,  place a Panel on the form and put your Labels in the Panel.  Then you can use the Panel.DrawToBitmap method to draw the entire panel to a Bitmap image.

 It is not clear if the Client area is not long enough to reach the bottom and that is why you need space so that the date appears at the bottom or what but,  if so,  you can make the Panel longer or you can just make the Bitmap longer than the Panel and draw the Panel as shown.

 You would need to use a printing method similar to what Tom has shown to print the Bitmap.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AutoScroll = True
        Panel1.Height = 2000 'my screen hight is only 768 (1024x768)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim r As New Rectangle(0, 0, Panel1.Width, Panel1.Height) 'make the bitmap hight larger if you need space at the bottom
        Using bm As New Bitmap(Panel1.Width, Panel1.Height)
            Panel1.DrawToBitmap(bm, r)

            'you can print the image instead of saving it to the hard drive. this was for testing
            bm.Save("C:\testfolder\pnlimage.png", Imaging.ImageFormat.Png)
        End Using
    End Sub
End Class

 

 Here is the image of the whole panel that is 2000 pixels in height.  I scaled it down to show on the forum but,  you can see a label i had at the top and a button i had at the very bottom of the panel.

If you say it can`t be done then i`ll try it


Sunday, October 11, 2015 10:51 PM

"I'm using the PowerPacks PrintForm component"

Well there's your problem.

But if you show some code maybe someone can help more.


Sunday, October 11, 2015 11:09 PM

"I'm using the PowerPacks PrintForm component"

Well there's your problem.

But if you show some code maybe someone can help more.

This is pretty much all there is to it. Any other code I have doesn't pertain to printing the form. The form is 798 x 772.

    Private Sub PrintPreviewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintPreviewToolStripMenuItem.Click
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
    End Sub

Bgeo25


Sunday, October 11, 2015 11:18 PM

"I'm using the PowerPacks PrintForm component"

Well there's your problem.

But if you show some code maybe someone can help more.

This is pretty much all there is to it. Any other code I have doesn't pertain to printing the form. The form is 798 x 772.

    Private Sub PrintPreviewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintPreviewToolStripMenuItem.Click
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
    End Sub

Bgeo25

I see. Well does the power pack only allow you to print a form?

BTW, I have never used the pp printform so I am not much help. Maybe someone else knows how to do it?

But, if you don't get it figured, if you want to try using the real printing in vb I think we can show you how to print whatever you want whatever size you want.


Sunday, October 11, 2015 11:30 PM

"I'm using the PowerPacks PrintForm component"

Well there's your problem.

But if you show some code maybe someone can help more.

This is pretty much all there is to it. Any other code I have doesn't pertain to printing the form. The form is 798 x 772.

    Private Sub PrintPreviewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintPreviewToolStripMenuItem.Click
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
    End Sub

Bgeo25

I see. Well does the power pack only allow you to print a form?

BTW, I have never used the pp printform so I am not much help. Maybe someone else knows how to do it?

But, if you don't get it figured, if you want to try using the real printing in vb I think we can show you how to print whatever you want whatever size you want.

While researching the problem, I did see something about document creation as opposed to printing the form. The real printing you mention, I assume, would also produce much better quality prints.

The problem is..any tutorial/info I've found having to do with printing the normal way is tedious and involves coding where all the text is placed. If this is the case, then it's way easier to use the designer.

Anyway, since I've had no success, I want to try printing a different way like you mentioned.

Bgeo25


Sunday, October 11, 2015 11:38 PM

Well there is a lot to it and it depends on exctly what you want to print and exactly the results you want. Here is an example that prints the form. This fits the form to the page and centers it to the page.

When you capture the form from the screen and print it you get into the problem of enlarging the bitmap. As you blow up the digital image it quickly looses quality. Is that the problem you are trying to solve?

Show us your form and what you want to print. There are many ways to print.

To make this example start a new form or project and cut and paste this (change form name as required). You need to add two buttons to the form.

Here is the screen shot of the example in preview:

PS to print to paper click the print button on the right side of the preview toolbar.

This is the printout in a pdf format.

Imports System.Drawing.Printing
'print form image fit to page margins
Public Class PrintFormPreview
    Public WithEvents PrintDocument1 As PrintDocument = New PrintDocument
    Private WithEvents PrintPreviewDialog1 As New PrintPreviewDialog
    Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
        Dim bmp As New Bitmap(rect.Width, rect.Height)
        Me.DrawToBitmap(bmp, rect)

        Dim l, t, w, h As Integer
        Dim ratio As Single = bmp.Width / bmp.Height

        'size to fit window
        If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
            w = e.MarginBounds.Width
            h = w / ratio
            t = (e.MarginBounds.Height / 2) - (h / 2)
        Else
            h = e.MarginBounds.Height
            w = h * ratio
            l = (e.MarginBounds.Width / 2) - (w / 2)
        End If

        'draw the image on the graphics
        e.Graphics.DrawImage(bmp, e.MarginBounds.Left + l, e.MarginBounds.Top + t, w, h)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'pd.DefaultPageSettings.Landscape = True

        'Show the Print Preview Dialog.
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
        PrintPreviewDialog1.ShowDialog()
    End Sub

End Class

Monday, October 12, 2015 12:04 AM

tommytwotrain:Hi and thanks for the response (Quoting your reply would make this page so long so I didn't bother).

Your code seems to work with small windows but if I use a scrollable form or a height higher than the screen supports (I increase it at runtime) then it shows a blank page in the preview. Nevertheless, I should've shown you my form beforehand because it's far from small. Also, I'm aiming to print the client area only. Sorry I didn't mention that before :(

Basically, if what you just showed me worked for a large form like the one shown below and can display the entire client area, so that my footer text comes on the page, then that would be perfect.

Bgeo25


Monday, October 12, 2015 12:17 AM

Bg,

The example prints a bitmap image and fits the image to the page. So you can print whatever image this way where ever it comes from. This example uses the form image. It will print any size form. It does not include scrollable areas.

But you have a text document so you want to print text for good quality.

How do you produce the form image you show? What is your original document? A word or excel doc or a plain text file or is that screen generated from a database or a report generator etc?

You should use the method to create the image you show but send it to the printer?

It can get quite involved to produce a nice document like this so it depends what you have and your ability.

If you can get the power pack print form to do it then it may be much easier.


Monday, October 12, 2015 12:25 AM

Well, with one quick search,  i found this about printing the scrollable client area of a form using the PrintForm control.

How to: Print a Scrollable Form (Visual Basic)

If you say it can`t be done then i`ll try it


Monday, October 12, 2015 12:39 AM

Well, with one quick search,  i found this about printing the scrollable client area of a form using the PrintForm control.

How to: Print a Scrollable Form (Visual Basic)

If you say it can`t be done then i`ll try it

I had tried this before. Making it Scrollable produces the same result as ClientAreaOnly. Unfortunately, it doesn't help.

Bgeo25


Monday, October 12, 2015 12:43 AM

That is strange,  it seems to print the entire client area of a form that is set to AutoScroll on my end using this.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
    End Sub

 

 Are you using Graphics to draw all those things in the form or are they Controls or a single Control?

If you say it can`t be done then i`ll try it


Monday, October 12, 2015 12:47 AM

Bg,

The example prints a bitmap image and fits the image to the page. So you can print whatever image this way where ever it comes from. This example uses the form image. It will print any size form. It does not include scrollable areas.

But you have a text document so you want to print text for good quality.

How do you produce the form image you show? What is your original document? A word or excel doc or a plain text file or is that screen generated from a database or a report generator etc?

You should use the method to create the image you show but send it to the printer?

It can get quite involved to produce a nice document like this so it depends what you have and your ability.

If you can get the power pack print form to do it then it may be much easier.

The form shown in the picture includes only labels on a white background. Most labels are set at runtime by the user. This is the result of your code when the form height goes beyond the what the screen normally supports:

Bgeo25


Monday, October 12, 2015 1:01 AM

That is strange,  it seems to print the entire client area of a form that is set to AutoScroll on my end using this.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
    End Sub

 

 Are you using Graphics to draw all those things in the form or are they Controls or a single Control?

If you say it can`t be done then i`ll try it

They are all separate controls on a white background. They're labels.

Bgeo25


Monday, October 12, 2015 2:34 AM | 1 vote

That is strange,  it seems to print the entire client area of a form that is set to AutoScroll on my end using this.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
    End Sub

 

 Are you using Graphics to draw all those things in the form or are they Controls or a single Control?

If you say it can`t be done then i`ll try it

The power pock print form does not seem to work properly on my system either? VS 2010. Here is a screen shot of a scrollable form that has a 800 x 3200 label and you can see the print preview is just the upper left corner of the form.

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.FullWindow)
        Me.Refresh()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.AutoScroll = True
        Me.Size = New Size(800, 3200)
        Label1.Size = New Size(800, 3200)
        Label1.BackColor = Color.White
        Label1.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub Label1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        Using p As Pen = New Pen(Color.Black, 2), _
            f As New Font("arial", 12)
            e.Graphics.DrawRectangle(p, Label1.ClientRectangle)
            e.Graphics.DrawLine(p, 0, 0, Label1.ClientSize.Width, Label1.ClientSize.Height)
            e.Graphics.DrawLine(p, 0, Label1.ClientSize.Height, Label1.ClientSize.Width, 0)
            For y = 0 To Label1.ClientRectangle.Height Step 200
                e.Graphics.DrawLine(p, 0, y, Label1.ClientSize.Width, y)
                e.Graphics.DrawString(y.ToString, f, Brushes.Black, 0, y)
            Next
        End Using
    End Sub
End Class

Monday, October 12, 2015 2:48 AM

 I kind of figured it must have something to do with the newer OS`s or Frameworks you two are probably using.  That is why i suggested the Panel idea.  That is the only other way i know of that would allow you to capture everything in the client area of a scrollable form.  Well, unless you manually draw each control in some way but, that would take a lot more work than just putting the controls on a Panel and just drawing the panel.  8)

If you say it can`t be done then i`ll try it


Monday, October 12, 2015 3:09 AM

 I kind of figured it must have something to do with the newer OS`s or Frameworks you two are probably using.  That is why i suggested the Panel idea.  That is the only other way i know of that would allow you to capture everything in the client area of a scrollable form.  Well, unless you manually draw each control in some way but, that would take a lot more work than just putting the controls on a Panel and just drawing the panel.  8)

If you say it can`t be done then i`ll try it

Well the power pack printform does not seem to do it.

Bg,

How is the Printting View made that you showed? How does it get the data and format onto the form? From labels? This is the form? Its still not clear what your original data is.

You have two basic ways of printing this. Print as an image, as you are doing, or print as formatted text. Formatted text will give the best quality result. If you dont mind a little loss of quality then printing the form may work.

The image you show should just be printed on the printer instead of drawing it on a form first. So if you are capable of making that print image you should be able to print it directly instead of drawing on a form? That is not clear to me.

What is Schedule Generator? Did you write the project?

You have hundreds of labels on the form and the labels are filled by the user entering data?

Then what do you do with the data when you want to save what has been entered? Do you save the label information in a database, a file, or just print it?

If I understand, having a form full of labels awkward lets say?


Monday, October 12, 2015 1:09 PM

 I kind of figured it must have something to do with the newer OS`s or Frameworks you two are probably using.  That is why i suggested the Panel idea.  That is the only other way i know of that would allow you to capture everything in the client area of a scrollable form.  Well, unless you manually draw each control in some way but, that would take a lot more work than just putting the controls on a Panel and just drawing the panel.  8)

If you say it can`t be done then i`ll try it

Well the power pack printform does not seem to do it.

Bg,

How is the Printting View made that you showed? How does it get the data and format onto the form? From labels? This is the form? Its still not clear what your original data is.

You have two basic ways of printing this. Print as an image, as you are doing, or print as formatted text. Formatted text will give the best quality result. If you dont mind a little loss of quality then printing the form may work.

The image you show should just be printed on the printer instead of drawing it on a form first. So if you are capable of making that print image you should be able to print it directly instead of drawing on a form? That is not clear to me.

What is Schedule Generator? Did you write the project?

You have hundreds of labels on the form and the labels are filled by the user entering data?

Then what do you do with the data when you want to save what has been entered? Do you save the label information in a database, a file, or just print it?

If I understand, having a form full of labels awkward lets say?

There are other windows/fields that allow input of the data that goes to the Printing View form. The Printing View was done in the designer. It isn't a literal "print preview." Anyway, IronRazerz's method seems to be working. The quality hasn't improved with saving the PNG and then printing it but it does fix the scaling issue. The people responsible for PowerPacks should know that "Scrollable" doesn't always work.

Thanks again for your dedication!

Bgeo25


Monday, October 12, 2015 1:14 PM

Well,  you could do it this way,  however,  you would need to redo a bit of your code.  Set the form`s AutoScroll property to True,  place a Panel on the form and put your Labels in the Panel.  Then you can use the Panel.DrawToBitmap method to draw the entire panel to a Bitmap image.

 It is not clear if the Client area is not long enough to reach the bottom and that is why you need space so that the date appears at the bottom or what but,  if so,  you can make the Panel longer or you can just make the Bitmap longer than the Panel and draw the Panel as shown.

 You would need to use a printing method similar to what Tom has shown to print the Bitmap.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.AutoScroll = True
        Panel1.Height = 2000 'my screen hight is only 768 (1024x768)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim r As New Rectangle(0, 0, Panel1.Width, Panel1.Height) 'make the bitmap hight larger if you need space at the bottom
        Using bm As New Bitmap(Panel1.Width, Panel1.Height)
            Panel1.DrawToBitmap(bm, r)

            'you can print the image instead of saving it to the hard drive. this was for testing
            bm.Save("C:\testfolder\pnlimage.png", Imaging.ImageFormat.Png)
        End Using
    End Sub
End Class

 

 Here is the image of the whole panel that is 2000 pixels in height.  I scaled it down to show on the forum but,  you can see a label i had at the top and a button i had at the very bottom of the panel.

If you say it can`t be done then i`ll try it

Your method does capture the whole form and therefore fixes the scaling problem. I adjusted the panel dimensions as well. Since the quality doesn't improve by saving the PNG and then printing it, I'll continue to experiment with this. Anyway, thanks. This is the working snippet:

    Private Sub SaveAndPrintToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveAndPrintToolStripMenuItem.Click
        Dim r As New Rectangle(0, 0, Panel1.Width, Panel1.Height)
        Using bm As New Bitmap(Panel1.Width, Panel1.Height)
            Panel1.DrawToBitmap(bm, r)

            bm.Save("C:\testIMG.png", Imaging.ImageFormat.Png)
        End Using

        'print
        PrintDocument2.Print()
    End Sub

    Private Sub PrintDocument2_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument2.PrintPage
        Dim g As Graphics = e.Graphics

        g.DrawImage(Image.FromFile("C:\testIMG.png"), 0, 0)
    End Sub

Bgeo25


Monday, October 12, 2015 2:08 PM

Bg,

As long as it works I guess.

If you just change the code I showed you to print your image it will fit it to the paper.

ie change:

   Me.DrawToBitmap(bmp, rect)

to

   Panel1.DrawToBitmap(bmp, rect)

If you have a 2000 high pixel panel and print it 1:1 on a 600 dpi printer it will be 2000/600 = 3.3 inches on paper unless you scale the image somehow like I show.

However it still seems you are going about the whole thing backwards.


Monday, October 12, 2015 4:17 PM | 1 vote

PS Here is an example that prints as an image or as real text. Which looks better (both make a full page which I have cropped the bottom in these images)?

Imports System.Drawing.Printing

Public Class Form9
    Public WithEvents PrintDocument1 As PrintDocument = New PrintDocument
    Private WithEvents PrintPreviewDialog1 As New PrintPreviewDialog
    Private PrintType As Integer

    Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AutoScroll = True
        Button1.Text = "Print Image"
        Button2.Text = "Print Text"
        Panel1.Size = New Size(800, 2000)
        Panel1.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
        Dim space As Integer = Panel1.ClientSize.Height / 60
        For i = 1 To 58
            e.Graphics.DrawString("Line " & i.ToString & "Billy Madison    Advanced Printing     Grade: A", New Font("arial", 10), Brushes.Black, 10, i * space)
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PrintType = 1
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        PrintType = 2
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()

    End Sub

    Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        'e.Graphics.PageUnit = GraphicsUnit.Pixel

        If PrintType = 1 Then
            Dim rect As New Rectangle(0, 0, Panel1.Width, Panel1.Height)
            Dim bmp As New Bitmap(rect.Width, rect.Height)
            Panel1.DrawToBitmap(bmp, rect)
            e.Graphics.DrawImage(bmp, e.MarginBounds)

        Else

            Dim space As Integer = e.MarginBounds.Height / 60

            For i = 1 To 58
                e.Graphics.DrawString("Line " & i.ToString, New Font("arial", 10), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y + (i * space))
                e.Graphics.DrawString("Billy Madison    Advanced Printing     Grade: A", New Font("arial", 10), Brushes.Black, e.MarginBounds.X + 100, e.MarginBounds.Y + (i * space))
            Next

        End If

    End Sub

End Class

Sunday, January 14, 2018 5:36 PM

KIndly help me on this one:

https://social.msdn.microsoft.com/Forums/en-US/c661f1c2-468c-4969-b1a3-938d6ac00830/print-form?forum=vstest


Sunday, January 14, 2018 6:47 PM | 1 vote

KIndly help me on this one:

https://social.msdn.microsoft.com/Forums/en-US/c661f1c2-468c-4969-b1a3-938d6ac00830/print-form?forum=vstest

If you are using vb.net you should create a new question and show the code you are using. If not then this is probably the wrong forum.

PrintForm is limited in the powerpack.

A better way using vb.net is shown here:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ea7e92c-7a61-4c65-bacb-2ef66e006717/print-a-form-but-in-lansscape?forum=vbgeneral


Sunday, January 14, 2018 7:22 PM

KIndly help me on this one:

https://social.msdn.microsoft.com/Forums/en-US/c661f1c2-468c-4969-b1a3-938d6ac00830/print-form?forum=vstest

 If you are programming a Vb.Net windows form application,  then try the examples that have been suggested above.  They would work in C# too if you convert them to C#.  If you have problems with them,  then ask a new question of your own on this forum (Vb.Net forum) and post your code and explain what does not work.  If you are using C#,  then ask a new question on the C# forum.  If you are using a different language,  then these methods would most likely not work but,  I'm sure there would be a solution if asked on the forum for the language you are using.

 You should also consider that you may be just printing a larger image than will fit on the paper size you are printing to.  In that case,  you could print to a larger paper size,  or scale your image down to fit the page,  or print the bottom part to the next page.  It may even require using a few of these options.

If you say it can`t be done then i`ll try it