Share via


Printing a form with a Custom Paper Size/Resizing a form to use an A4 size paper entirely

Question

Tuesday, June 23, 2015 11:37 AM

Hi there,

In my program I want the client to be able to print an informational form using an A4 paper size, but the print form doesn't cover all the paper, it's just placed in the middle of the page.

Here's the code:

Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        'Print the contents.
        e.Graphics.DrawImage(bitmap, 0, 0)
    End Sub


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

        Dim pd As New PrintDocument()

        'Add a Panel control.
        Dim panel As New Panel()
        Me.Controls.Add(panel)

        'Create a Bitmap of size same as that of the Form.
        Dim grp As Graphics = panel.CreateGraphics()
        Dim formSize As Size = Me.ClientSize
        bitmap = New Bitmap(750, 830, grp)                                          
        grp = Graphics.FromImage(bitmap)

        'Copy screen area that that the Panel covers.
        Dim panelLocation As Point = PointToScreen(panel.Location)
        grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 100, 150, formSize)    
        pd.DefaultPageSettings.Landscape = True

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

(I searched the web and I found this code, that I'm currently using. It doesn't applies entirely to my objective.)

I want to be able to have the printed form covering all the A4 page.

Thanks in advance,

Diogo

(sorry for my bad English)

All replies (7)

Tuesday, June 23, 2015 12:12 PM âś…Answered | 2 votes

You can scale the size in the DrawImage method.

This seems to work ok:

Imports System.Drawing.Printing
Public Class Form1
   Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
      SetClientSizeCore(1188, 840)   ' aspect ratio of A4 paper
   End Sub
   Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
      e.Graphics.DrawImage(Bmp, e.MarginBounds, New Rectangle(0, 0, Bmp.Width, Bmp.Height), GraphicsUnit.Pixel)
   End Sub
   Dim Bmp As Bitmap
   Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      Bmp = New Bitmap(ClientRectangle.Width, ClientRectangle.Height)
      Using g As Graphics = Graphics.FromImage(Bmp)
         g.CopyFromScreen(PointToScreen(New Point(0, 0)), New Point(0, 0), ClientRectangle.Size)
      End Using
      PrintDocument1.DefaultPageSettings.Landscape = True
      PrintPreviewDialog1.Document = PrintDocument1
      PrintPreviewDialog1.ShowDialog()
   End Sub
End Class

Tuesday, June 23, 2015 11:46 AM | 1 vote

All about PrintForm component 1.0

"To zoom the form to fill the printed page, setting the AutoFit property to one of these values controls this behavior: PageElement.None, PageElement.Body, PageElement.All."

La vida loca


Tuesday, June 23, 2015 11:52 AM

"To zoom the form to fill the printed page, setting the AutoFit property to one of these values controls this behavior: PageElement.None, PageElement.Body, PageElement.All."

La vida loca

Where can I find that autofit property in visual studio 2013?


Tuesday, June 23, 2015 12:54 PM

Here is another example that fits the form to the margins.

PS this does not use the PrintForm from the powerpack if that is what you are doing. You don't need it.

And it uses DrawToBitmap to capture the form image instead of trying to use a panel and capture the screen.

Imports System.Drawing.Printing

Public Class Form4
    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

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

Tuesday, June 23, 2015 1:41 PM

Tommytwotrain, this leads to my original problem: The form is placed in the middle of the paper.

But hey, thanks anyway


Tuesday, June 23, 2015 1:43 PM

Thank you so much Dave299, now I just need to modify some of the values but after some days of research you gave me the answer.

Thanks one more time Dave


Tuesday, June 23, 2015 2:31 PM | 1 vote

Tommytwotrain, this leads to my original problem: The form is placed in the middle of the paper.

But hey, thanks anyway

What it does is maintain the aspect ratio so you don't get a distorted mess. It fits that to the smallest size of the margins. However if you resize the form to the size of the print margins first that also works. But this way you can use any size form and then change it to any size paper or landscape and print it without distortion.