Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePrint pages in portrait and landscape orientations in VB .NET
DescriptionThis example shows how to print pages in portrait and landscape orientations in VB .NET. The program catches the PrintDocument's QueryPageSettings event and sets the orientation for each page.
Keywordsprint, landscape, portrait, VB.NET
CategoriesGraphics, VB.NET
 
When you click the Print button, the propgram makes a new PrintDocument object and calls its Print method.
 
Private WithEvents m_PrintDocument As PrintDocument
Private m_NextPage As Integer

Private Sub btnPrint_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnPrint.Click
    ' Start with page 1.
    m_NextPage = 1

    ' Make a PrintDocument and print.
    m_PrintDocument = New PrintDocument
    m_PrintDocument.Print()
End Sub
 
Before it prints each page, the PrintDocument object raises its QueryPageSettings event. The program's event handler sets e.PageSettings.Landscape to True for page 1 and to False for page 2.
 
Private Sub m_PrintDocument_QueryPageSettings(ByVal sender _
    As Object, ByVal e As _
    System.Drawing.Printing.QueryPageSettingsEventArgs) _
    Handles m_PrintDocument.QueryPageSettings
    If m_NextPage = 1 Then
        ' Print in landscape.
        e.PageSettings.Landscape = True
    Else
        ' Print in portrait.
        e.PageSettings.Landscape = False
    End If
End Sub
 
When it is ready to print a page, the PrintDocument raises its PrintPage event. The program's event handler draws the string "Landscape" on page 1 and "Portrait" on page 2.
 
Private Sub m_PrintDocument_PrintPage(ByVal sender As _
    Object, ByVal e As _
    System.Drawing.Printing.PrintPageEventArgs) Handles _
    m_PrintDocument.PrintPage
    Dim txt As String
    If m_NextPage = 1 Then
        ' Print in landscape.
        txt = "Landscape"
    Else
        ' Print in portrait.
        txt = "Portrait"
    End If

    ' Print some text.
    Dim the_font As New Font("Times New Roman", _
        60, FontStyle.Bold, GraphicsUnit.Point)
    Dim layout_rect As New RectangleF( _
        e.MarginBounds.X, e.MarginBounds.Y, _
        e.MarginBounds.Width, e.MarginBounds.Height)
    Dim string_format As New StringFormat
    string_format.Alignment = StringAlignment.Center
    string_format.LineAlignment = StringAlignment.Center
    e.Graphics.DrawString(txt, the_font, _
        Brushes.Black, layout_rect, string_format)
    e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)

    ' There are two pages.
    m_NextPage += 1
    e.HasMorePages = (m_NextPage = 2)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated