none
Print Same Form multiple times on the same page. RRS feed

  • Question

  • As the title suggests, I was wondering if any of you wonderful people out there would know how I can get the form that I am printing to appear 4 times on the one page. One top of that, I also want to change the number of copies of pages to print of the same page through code.

    I wish I could attach a picture, but my account is not verified yet. :'(

    I essentially want to print 4 of the same form, one on each corner of the printed page and After having four forms on each corner, I want to control the amount of pages created in the Print Preview of the exact same page, in other words control the number of copies, through code and not let the user decide. How can I do this? Help would be much appreciated!!

    Thanks, 

    Angelo Don aka BerserkRVLTK(they wouldn't let me use my real name so I'll sign out as both!!!)

    Thursday, July 20, 2017 3:01 PM

Answers

  • Here is an example. There are lots of possibilities for exactly how the layout could be done.

    In this example the form image is fit and centered to a quarter section of the page margins.

    The example is set on landscape for the shape of the form. If the form is taller than wide use portrait.

    There may be details you still need to work out depending on exactly what you want.

    Imports System.Drawing.Printing
    
    Public Class Form4
        Public WithEvents pd As New PrintDocument
    
        Private Sub Form10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Size = New Size(500, 300)
            BackgroundImage = Image.FromFile("c:\bitmaps\rusty.jpg")
            BackgroundImageLayout = ImageLayout.Zoom
    
            pd.DefaultPageSettings.Landscape = True
            pd.DefaultPageSettings.Margins = New Printing.Margins(25, 25, 25, 25)
            pd.DefaultPageSettings.PrinterSettings.Copies = 2
    
            Dim preview As New PrintPreviewDialog
            preview.Document = pd
            preview.ShowDialog()
        End Sub
    
        Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            'capture the form to a memory bitmap sized to the form
            Using bmp As Bitmap = New Bitmap(Me.Width, Me.Height)
                'draw the form on the memory bitmap
                Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
                Me.DrawToBitmap(bmp, rect)
    
                'size the four quarter panels from the page margins
                Dim border As Integer = 50
                Dim w As Integer = CInt((e.MarginBounds.Width - border) / 2)
                Dim h As Integer = CInt((e.MarginBounds.Height - border) / 2)
    
                'draw the four form images on the page
                For x As Integer = 0 To 1
                    For y As Integer = 0 To 1
                        rect = New Rectangle(CInt(e.MarginBounds.Left + (x * (w + border))),
                                             CInt(e.MarginBounds.Top + (y * (h + border))), w, h)
                        DrawForm(bmp, rect, e.Graphics)
                    Next
                Next
            End Using
    
            Static copiesPrinted As Integer
            copiesPrinted += 1
    
            If copiesPrinted >= e.PageSettings.PrinterSettings.Copies Then
                e.HasMorePages = False
                copiesPrinted = 0
            Else
                e.HasMorePages = True
            End If
        End Sub
    
        Private Sub DrawForm(bmp As Bitmap, rect As Rectangle, g As Graphics)
            'fit the bitmap to the rectangle and center
            Dim l, t, w, h As Integer
            Dim ratio As Single = CSng(bmp.Width / bmp.Height)
    
            If ratio > rect.Width / rect.Height Then
                w = rect.Width
                h = CInt(w / ratio)
                t = CInt(rect.Top + (rect.Height / 2) - (h / 2))
                l = rect.Left
            Else
                h = rect.Height
                w = CInt(h * ratio)
                l = CInt(rect.Left + (rect.Width / 2) - (w / 2))
                t = rect.Top
            End If
    
            g.DrawImage(bmp, l, t, w, h)
    
        End Sub
    End Class

    • Proposed as answer by Frank L. Smith Thursday, July 20, 2017 9:01 PM
    • Marked as answer by BerserkRVLTK Friday, August 4, 2017 9:22 AM
    Thursday, July 20, 2017 8:54 PM

All replies

  • Here is an example. There are lots of possibilities for exactly how the layout could be done.

    In this example the form image is fit and centered to a quarter section of the page margins.

    The example is set on landscape for the shape of the form. If the form is taller than wide use portrait.

    There may be details you still need to work out depending on exactly what you want.

    Imports System.Drawing.Printing
    
    Public Class Form4
        Public WithEvents pd As New PrintDocument
    
        Private Sub Form10_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Size = New Size(500, 300)
            BackgroundImage = Image.FromFile("c:\bitmaps\rusty.jpg")
            BackgroundImageLayout = ImageLayout.Zoom
    
            pd.DefaultPageSettings.Landscape = True
            pd.DefaultPageSettings.Margins = New Printing.Margins(25, 25, 25, 25)
            pd.DefaultPageSettings.PrinterSettings.Copies = 2
    
            Dim preview As New PrintPreviewDialog
            preview.Document = pd
            preview.ShowDialog()
        End Sub
    
        Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
            'capture the form to a memory bitmap sized to the form
            Using bmp As Bitmap = New Bitmap(Me.Width, Me.Height)
                'draw the form on the memory bitmap
                Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
                Me.DrawToBitmap(bmp, rect)
    
                'size the four quarter panels from the page margins
                Dim border As Integer = 50
                Dim w As Integer = CInt((e.MarginBounds.Width - border) / 2)
                Dim h As Integer = CInt((e.MarginBounds.Height - border) / 2)
    
                'draw the four form images on the page
                For x As Integer = 0 To 1
                    For y As Integer = 0 To 1
                        rect = New Rectangle(CInt(e.MarginBounds.Left + (x * (w + border))),
                                             CInt(e.MarginBounds.Top + (y * (h + border))), w, h)
                        DrawForm(bmp, rect, e.Graphics)
                    Next
                Next
            End Using
    
            Static copiesPrinted As Integer
            copiesPrinted += 1
    
            If copiesPrinted >= e.PageSettings.PrinterSettings.Copies Then
                e.HasMorePages = False
                copiesPrinted = 0
            Else
                e.HasMorePages = True
            End If
        End Sub
    
        Private Sub DrawForm(bmp As Bitmap, rect As Rectangle, g As Graphics)
            'fit the bitmap to the rectangle and center
            Dim l, t, w, h As Integer
            Dim ratio As Single = CSng(bmp.Width / bmp.Height)
    
            If ratio > rect.Width / rect.Height Then
                w = rect.Width
                h = CInt(w / ratio)
                t = CInt(rect.Top + (rect.Height / 2) - (h / 2))
                l = rect.Left
            Else
                h = rect.Height
                w = CInt(h * ratio)
                l = CInt(rect.Left + (rect.Width / 2) - (w / 2))
                t = rect.Top
            End If
    
            g.DrawImage(bmp, l, t, w, h)
    
        End Sub
    End Class

    • Proposed as answer by Frank L. Smith Thursday, July 20, 2017 9:01 PM
    • Marked as answer by BerserkRVLTK Friday, August 4, 2017 9:22 AM
    Thursday, July 20, 2017 8:54 PM
  • Thanks a lot man. You are literally my lifesaver!! I will let you know when I get this thing working.

    Again, thanks a lot!

    Angelo

    Friday, July 21, 2017 6:24 AM
  • Hi BerserkRVLTK,

    Have you resolved your issue now, if yes, please remember to close your thread by marking helpful post as answer, it is very beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 1, 2017 4:25 AM
    Moderator
  • Been a while, but I just wanted to let you know that it worked, and I am giving you a shout out in my program!
    Friday, August 4, 2017 9:23 AM