Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application I have a number of window forms that need to printed.
The end users all have different size screens the lowest being 1366 x 768.
I tried using PrintForm but the program only prints what is on the screen so the forms have to be small and therefore would only fill part of an A4 sheet.
Therefore I want to be able to capture the form and scale it up to A4 for printing.
I copied some code from the internet and modified it for my use but it does not seem to scale up the form although it does print it.
Has any one any ideas how I can do this.

Please be aware I am a total amateur at this. Producing the software for the Seniors Section of a golf club.

What I have tried:

The code am using is as follows

Public Class TeamSelect12
    'Define your print object
    Private PrintDoc1 As PrintDocument = New PrintDocument
    Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog
    Private PageSetupDialog1 As PageSetupDialog = New PageSetupDialog

    'Print Button Code
    Private Sub PrtTSBut_Click(sender As System.Object, e As System.EventArgs) Handles PrtTSBut.Click

        PDFTeamBut.Visible = False 'Hides button
        PrtTSBut.Visible = False 'Hides button
        Me.Refresh()

        With PageSetupDialog1
            'Assign the document to use
            .Document = PrintDoc1
            'Enable printer button
            .AllowPrinter = True
            .EnableMetric = True
            ' Initialize the dialog's PrinterSettings property to hold user
            ' defined page settings.
            .PageSettings = New System.Drawing.Printing.PageSettings
            ' Initialize dialog's PrinterSettings property to hold user
            ' set printer settings.
            .PrinterSettings = New System.Drawing.Printing.PrinterSettings
        End With

        PrintPreviewDialog1.Document = PrintDoc1
        AddHandler PrintDoc1.PrintPage, AddressOf PDoc_PrintPage
        Dim margins As New Margins(20, 20, 20, 40)
        PrintDoc1.DefaultPageSettings.Margins = margins
        PrintPreviewDialog1.ShowDialog()

        'Replace buttons back on form
        PrtTSBut.Visible = True
        PDFTeamBut.Visible = True
        'Hide form
        Me.Hide()
        InputForm.Show()  ' Return to input form
    End Sub

    'Print Handler And Graphics print method With scaling
    Private Sub PDoc_PrintPage(sender As Object, e As PrintPageEventArgs)
        PrintToGraphics(e.Graphics, e.MarginBounds)
    End Sub
    Public Sub PrintToGraphics(Graphics As Graphics, bounds As Rectangle)
        'Print the control's view to a Graphics object.
        '<param name="graphics">Graphics object to draw on.</param>
        '<param name="bounds">Rectangle to print in.</param>

        'Draw the control and contents to a bitmap 
        Dim Bitmap As Bitmap = New Bitmap(Me.Width, Me.Height)
        Me.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Bitmap.Height))

        'Assign Print Bounds to target rectangle
        Dim PrtWidth, PrtHeight, PrtLeft, PrtTop As Integer
        PrtWidth = bounds.Width
        PrtHeight = bounds.Height
        PrtLeft = bounds.Left
        PrtTop = bounds.Top
        Dim target As Rectangle = New Rectangle(PrtLeft, PrtTop, PrtWidth, PrtHeight)
        'Scale bitmap to fit target
        Dim xScale As Double = Bitmap.Width / PrtWidth
        Dim yScale As Double = Bitmap.Height / PrtHeight
        If xScale < yScale Then
            target.Width = Int(xScale * target.Width / yScale)
        Else
            target.Height = Int(yScale * target.Height / xScale)
        End If
        'Draw the bitmap
        Graphics.PageUnit = GraphicsUnit.Display
        Graphics.DrawImage(Bitmap, target)
    End Sub
Posted
Updated 18-Nov-18 10:42am

Don't print the form at all: you can do it by capturing the screen as an Image, and drawing that image at A4, but it will look like rubbish on some screens, and unreadable on others.

Instead, draw the information the screen is supposed to be displaying onto a PrintDocument: that way, you can make the output look the same regardless of which user printed it as you have complete control of the font size and position of everything. Plus, if any of your form controls are scrollable, the capture process will only capture the visible bit, so anything scrolled out of sight will no be printed - by doing the job properly, you get to decide exactly what gets printed, what size it is printed at, and where it goes.

The MS documentation for PrintDocument shows the basics of printing text: PrintDocument Class (System.Drawing.Printing)[^]
 
Share this answer
 
 
Share this answer
 
Saved the data to excel and printed from excel
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900