none
customized print paper size RRS feed

  • Question

  • hello guys can you help me with my code I'm trying to change the paper size from short to legal here is my code


        Private Sub printpreview_Click_1(sender As Object, e As EventArgs) Handles printpreview.Click
          
               PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        End Sub

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

                   Dim b As New Bitmap(Panel1.Width, Panel1.Height)
            Panel1.DrawToBitmap(b, Panel1.ClientRectangle)
            e.Graphics.DrawImage(b, New Point(0, 0))
        End Sub

    my code is working but I just wan to change my paper printing size



    • Edited by no[one] Thursday, September 27, 2018 10:26 AM
    Thursday, September 27, 2018 10:22 AM

Answers

  • First you need the paper and source name which this shows how to get and then set.

    'set custom paper size and source comboboxes
    'based on 'https://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.papersize%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
    Imports System.Drawing.Printing
    
    Public Class PrintSelectSource
        Private WithEvents comboPaperSize As New ComboBox With {.Parent = Me, .Location = New Point(10, 20), .Size = New Size(200, 20)}
        Private WithEvents comboPaperSource As New ComboBox With {.Parent = Me, .Location = New Point(10, 50), .Size = New Size(200, 20)}
        Private WithEvents buttonPrint As New Button With {.Parent = Me, .Location = New Point(10, 90), .Text = "Print"}
        Public WithEvents printdoc As PrintDocument = New PrintDocument
        Public PrintPreviewDialog1 As New PrintPreviewDialog
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' Add list of supported paper sizes found on the printer. 
            ' The DisplayMember property is used to identify the property that will provide the display string.
            comboPaperSize.DisplayMember = "PaperName"
    
            Dim pkSize As PaperSize
            For i As Integer = 0 To printdoc.PrinterSettings.PaperSizes.Count - 1
                pkSize = printdoc.PrinterSettings.PaperSizes.Item(i)
                comboPaperSize.Items.Add(pkSize)
            Next
    
            ' Create a PaperSize and specify the custom paper size through the constructor and add to combobox.
            Dim pkCustomSize1 As New PaperSize("Custom Paper Size", 300, 800)
            comboPaperSize.Items.Add(pkCustomSize1)
            comboPaperSize.SelectedIndex = 0
    
            ' Add list of paper sources found on the printer to the combo box.
            ' The DisplayMember property is used to identify the property that will provide the display string.
            comboPaperSource.DisplayMember = "SourceName"
    
            Dim pkSource As PaperSource
            For i As Integer = 0 To printdoc.PrinterSettings.PaperSources.Count - 1
                pkSource = printdoc.PrinterSettings.PaperSources.Item(i)
                comboPaperSource.Items.Add(pkSource)
            Next
    
            If comboPaperSource.Items.Count > 0 Then comboPaperSource.SelectedIndex = 0
    
        End Sub
    
        Private Sub buttonPrint_Click(sender As Object, e As EventArgs) Handles buttonPrint.Click
    
            ' Set the paper size based upon the selection in the combo box.
            If comboPaperSize.SelectedIndex <> -1 Then
                printdoc.DefaultPageSettings.PaperSize = CType(comboPaperSize.SelectedItem, PaperSize)
            End If
    
            ' Set the paper source based upon the selection in the combo box.
            If comboPaperSource.SelectedIndex <> -1 Then
                printdoc.DefaultPageSettings.PaperSource = CType(comboPaperSource.SelectedItem, PaperSource)
            End If
    
            PrintPreviewDialog1.Document = printdoc
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub printdoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles printdoc.PrintPage
    
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top,
                    e.MarginBounds.Width, e.MarginBounds.Height)
    
            e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString,
                    New Font("Arial", 11), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
    
        End Sub
    End Class

    • Marked as answer by no[one] Friday, September 28, 2018 8:25 AM
    Thursday, September 27, 2018 12:14 PM

All replies

  • First you need the paper and source name which this shows how to get and then set.

    'set custom paper size and source comboboxes
    'based on 'https://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.papersize%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
    Imports System.Drawing.Printing
    
    Public Class PrintSelectSource
        Private WithEvents comboPaperSize As New ComboBox With {.Parent = Me, .Location = New Point(10, 20), .Size = New Size(200, 20)}
        Private WithEvents comboPaperSource As New ComboBox With {.Parent = Me, .Location = New Point(10, 50), .Size = New Size(200, 20)}
        Private WithEvents buttonPrint As New Button With {.Parent = Me, .Location = New Point(10, 90), .Text = "Print"}
        Public WithEvents printdoc As PrintDocument = New PrintDocument
        Public PrintPreviewDialog1 As New PrintPreviewDialog
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' Add list of supported paper sizes found on the printer. 
            ' The DisplayMember property is used to identify the property that will provide the display string.
            comboPaperSize.DisplayMember = "PaperName"
    
            Dim pkSize As PaperSize
            For i As Integer = 0 To printdoc.PrinterSettings.PaperSizes.Count - 1
                pkSize = printdoc.PrinterSettings.PaperSizes.Item(i)
                comboPaperSize.Items.Add(pkSize)
            Next
    
            ' Create a PaperSize and specify the custom paper size through the constructor and add to combobox.
            Dim pkCustomSize1 As New PaperSize("Custom Paper Size", 300, 800)
            comboPaperSize.Items.Add(pkCustomSize1)
            comboPaperSize.SelectedIndex = 0
    
            ' Add list of paper sources found on the printer to the combo box.
            ' The DisplayMember property is used to identify the property that will provide the display string.
            comboPaperSource.DisplayMember = "SourceName"
    
            Dim pkSource As PaperSource
            For i As Integer = 0 To printdoc.PrinterSettings.PaperSources.Count - 1
                pkSource = printdoc.PrinterSettings.PaperSources.Item(i)
                comboPaperSource.Items.Add(pkSource)
            Next
    
            If comboPaperSource.Items.Count > 0 Then comboPaperSource.SelectedIndex = 0
    
        End Sub
    
        Private Sub buttonPrint_Click(sender As Object, e As EventArgs) Handles buttonPrint.Click
    
            ' Set the paper size based upon the selection in the combo box.
            If comboPaperSize.SelectedIndex <> -1 Then
                printdoc.DefaultPageSettings.PaperSize = CType(comboPaperSize.SelectedItem, PaperSize)
            End If
    
            ' Set the paper source based upon the selection in the combo box.
            If comboPaperSource.SelectedIndex <> -1 Then
                printdoc.DefaultPageSettings.PaperSource = CType(comboPaperSource.SelectedItem, PaperSource)
            End If
    
            PrintPreviewDialog1.Document = printdoc
            PrintPreviewDialog1.ShowDialog()
    
        End Sub
    
        Private Sub printdoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles printdoc.PrintPage
    
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds.Left, e.MarginBounds.Top,
                    e.MarginBounds.Width, e.MarginBounds.Height)
    
            e.Graphics.DrawString("Page Width: " & e.PageBounds.Width.ToString,
                    New Font("Arial", 11), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
    
        End Sub
    End Class

    • Marked as answer by no[one] Friday, September 28, 2018 8:25 AM
    Thursday, September 27, 2018 12:14 PM
  • PS You can also call the page setup dialog etc.

        Public PageSetupDialog1 As New PageSetupDialog
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            'call the page setup dialog
            Try
                'copy the printdocument page settings to the dialog
                'do not copy the printer settings to pd here 
                PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
                PageSetupDialog1.PrinterSettings = PrintDocument1.PrinterSettings
    
                If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                    'copy the dialog settings to the printdocument
                    PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
                    PrintDocument1.PrinterSettings = PageSetupDialog1.PrinterSettings
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    

    Thursday, September 27, 2018 1:34 PM
  • thank you, it helps me a lot
    Friday, September 28, 2018 8:25 AM