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

  • Thread starter Thread starter Tasipeka
  • Start date Start date
T

Tasipeka

Guest
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
PrintDocument1.Print()
End Sub

Continue reading...
 
Back
Top