I've spent the whole trying to generate a document wth PrintDocument in A4 settings. RRS feed

  • Question

  • Everytime I run the program, I have to first open a PrintSetUp Dialog and set the papersize before continuing to generate pdf document through PrintDocument. How can I hardcode the settings in PrintDocument so I dont have to open the PrintSetUp Dialog everyime I run the program. Here is my code
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PrintDocument1.DefaultPageSettings.PaperSize = (From s As PaperSize In PrintDocument1.PrinterSettings.PaperSizes.Cast(Of PaperSize)() Where s.RawKind = PaperKind.A4).FirstOrDefault
        End Sub
        Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            ' Turn on antialias for text
            e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
            ' Print a string at the origin
            ' Read margins into local variables
            Dim Lmargin, Rmargin, Tmargin, Bmargin As Integer
            With PrintDocument1.DefaultPageSettings.Margins
                Lmargin = .Left
                Rmargin = .Right
                Tmargin = .Top
                Bmargin = .Bottom
            End With
            ' Calculate the dimensions of the printable area
            Dim PrintWidth, PrintHeight As Integer
            With PrintDocument1.DefaultPageSettings.PaperSize
                PrintWidth = .Width - Lmargin - Rmargin
                PrintHeight = .Height - Tmargin - Bmargin
            End With
            Dim pFont As Font
            pFont = New Font("Times New Roman Sans MS", 14, FontStyle.Bold)
            e.Graphics.DrawString("Vehicle Information Register", pFont, Brushes.Black, 180, 40)
            ' Now print the rectangle
            Dim R As Rectangle
            R = New Rectangle(Lmargin, Tmargin, PrintWidth, PrintHeight)
            e.Graphics.DrawRectangle(Pens.Blue, R)
        End Sub
        Private Sub btnsetpage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsetpage.Click
            PageSetUpDialog1.PageSettings = PrintDocument1.DefaultPageSettings
            If PageSetUpDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then 'Then
                PrintDocument1.DefaultPageSettings = PageSetUpDialog1.PageSettings
            End If
        End Sub
        Private Sub btngenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngenerate.Click
        End Sub

    Monday, February 24, 2020 12:17 PM

All replies

  • Here is a working example.

    Public Class Form5
        Private WithEvents PrintDocument1 As New PrintDocument
        Private WithEvents PrintPreviewDialog1 As New PrintPreviewDialog
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles Me.Load
            'print as xps file to disc
            PrintDocument1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"
            'page settings
            PrintDocument1.DefaultPageSettings.PrinterSettings.PrintToFile = True
            PrintDocument1.DefaultPageSettings.PrinterSettings.PrintFileName = "C:\text\test1.xps"
            PrintDocument1.DefaultPageSettings.Landscape = True
            PrintDocument1.DefaultPageSettings.PaperSize = New PaperSize("A4", 827, 1169)
            PrintPreviewDialog1.Document = PrintDocument1
        End Sub
        Private Sub PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            e.Graphics.DrawRectangle(Pens.Black, New Rectangle(100, 100, 100, 100))
            e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)
            e.Graphics.DrawString("test print", New Font("ms sans serif", 14), Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y)
        End Sub
    End Class

    Monday, February 24, 2020 12:58 PM