Resolved VS2008 Printing an Envelope, orientation problem

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I have my code correctly generating an envelope using a PrintDocument:
VB.NET:
    Private Sub EnvelopePrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles EnvelopePrintDocument.PrintPage
        With e.Graphics
            If Not m_OmitReturnAddress Then
                Using ReturnFnt As New Font("Arial", 10.0!, FontStyle.Regular)
                    Dim ReturnName As String = String.Format("{0} {1}", g_Settings.FirstName, g_Settings.LastName).Trim()
                    Dim RtnNameHeight As Integer = 0I
                    If ReturnName <> String.Empty Then
                        .DrawString(ReturnName, ReturnFnt, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)
                        RtnNameHeight = CInt(.MeasureString(ReturnName, ReturnFnt).Height)
                    End If
                    .DrawString(g_Settings.Addresses(m_ReturnAddressIndex).ToString, ReturnFnt, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + RtnNameHeight)
                End Using
            End If
            Using DeliverFnt As New Font("Arial", 12.0!, FontStyle.Regular)
                Dim DestAddr As String = String.Format("{0}{1}{2}", CType(CardsListBox.SelectedItem, CardInfo).DisplayName, Environment.NewLine, AddressesMultilineListbox.SelectedItem.ToString)
                Dim AddrSize As SizeF = .MeasureString(DestAddr, DeliverFnt)
                .DrawString(DestAddr, DeliverFnt, Brushes.Black, CSng(e.MarginBounds.Right / 2.0!) - CSng(AddrSize.Width / 2.0!), CSng(e.MarginBounds.Bottom * (2.0! / 3.0!)) - CSng(AddrSize.Height / 2.0!))
            End Using
        End With
        e.HasMorePages = False
    End Sub
Which is fine and dandy, except when it goes to the printer the whole thing (paper size and everything) needs to be rotated 90 degrees.

Right now it's all horizontal from left to right, but printing I need to send it either return address or stamp edge up (the text will need to be rotated 90 degrees so it's vertical too). How do I do that?

Here's how it currently is:
View attachment 2516
I need it to be one of these:
View attachment 2514 or View attachment 2515
 
PrintDocument .DefaultPageSettings.Landscape should do it.
 
Yep, I've gotten that part put in, I'm now modifying my code to drawstring the text vertically if the landscape is set to true

I'm using Dim drawFormat As New System.Drawing.StringFormat() With {.FormatFlags = StringFormatFlags.DirectionVertical} right now
 
I see, you need to print vertically in portrait mode. This example has Landscape = False (default) and in PrintPage handler the Graphics is rotated 90 degrees around the pages top-right corner. e.MarginBounds is not changed, so this example defines a new Rectangle with width/height swapped. From there you can draw as normal.
VB.NET:
e.Graphics.TranslateTransform(e.PageBounds.Width, 0) 'change origin to top-right page corner
e.Graphics.RotateTransform(90) 'rotate

Dim newMarginBounds As New Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Height, e.MarginBounds.Width)

e.Graphics.DrawRectangle(Pens.Red, newMarginBounds)
e.Graphics.DrawString("This is a test.", Me.Font, Brushes.Black, newMarginBounds)
 
Back
Top