Solved

printing using the print dialogue

Posted on 2006-11-05
8
300 Views
Last Modified: 2010-04-23
I'm trying to learn how to print using the print dialogue from VB.Net 2005

My biggest and current problem is how to over-ride the margins, such as using something akin to a "page setup" feature.  I don't see that in the print dialogue.

Currently, using code like this:

    Private Sub mDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mDoc.PrintPage
        Dim xPos As Single = e.MarginBounds.Left
        Dim yPos As Single = e.MarginBounds.Top
        Dim lineHeight As Single = mFont.GetHeight(e.Graphics)
        Static l As Integer
        Dim intCt As Integer
    End Sub

(which I "borrowed" from someone here on EE   :-)  )

my documents have a 1 1/2 " margin at the top and about 1 1/4" margin to the left.
So apparently that's the default for the printer.

How can I use some kind of page setup feature to over-ride those and also, but not as important, to set font choices, etc.?

Thanks
0
Comment
Question by:g_johnson
  • 3
  • 2
8 Comments
 
LVL 5

Assisted Solution

by:maralans
maralans earned 100 total points
ID: 17879291


Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click
        Dim psd As New PageSetupDialog()
        With psd
            .Document = mdoc
            .PageSettings = mdoc.DefaultPageSettings
        End With

        If psd.ShowDialog = Windows.Forms.DialogResult.OK Then
            mdoc.DefaultPageSettings = psd.PageSettings
        End If
    End Sub
0
 
LVL 5

Accepted Solution

by:
DEEPESH earned 400 total points
ID: 17879365
These are some points i jotted down from Microsft Book, see if it may help you
The PrintPage event is the main event involved in printing documents. To actually send content to the printer, you must handle this event and provide code to render the content in the PrintPage event handler

mainly there are two properties of PrintPageEventArgs

MarginBounds which Gets the Rectangle object that represents the portion of the page within the margins
PageSettings which Gets or sets the PageSettings object for the current page

The MarginBounds and PageBounds properties represent areas of the page surface. You can specify printing to occur inside the margin bounds of the page by calculating printing coordinates based on the MarginBounds rectangle. Printing that is to take place outside the margin bounds, such as headers or footers, can be specified by calculating the printing coordinates based on the PageBounds rectangle. As with painting to the screen, print coordinates are in pixels by default.

Sample code
Dim ArrayCounter As Integer = 0
' This method handles a PrintDocument.PrintPage event. It assumes
' an array of strings called myStrings() has been declared and
' populated elsewhere in the application. It also assumes a font
' for printing has been initialized and called myFont
Private Sub PrintStrings(sender As Object, e As _
   PrintPageEventArgs)
   ' Declares the variables that will be used to keep track of
   ' spacing and paging
   Dim LeftMargin As Single = e.MarginBounds.Left
   Dim TopMargin As Single = e.MarginBounds.Top
   Dim MyLines As Single = 0
   Dim YPosition As Single = 0
   Dim Counter As Integer = 0
   Dim CurrentLine As String
   ' Calculate the number of lines per page.
   MyLines = e.MarginBounds.Height / _
      myFont.GetHeight(e.Graphics)
   ' Prints each line of the file, but stops at the end of a page
   While Counter < MyLines And ArrayCounter <= _
      myStrings.GetUpperBound(0)
      CurrentLine = myStrings(ArrayCounter)
      YPosition = TopMargin + Counter * _
         myFont.GetHeight(e.Graphics)
      e.Graphics.DrawString(CurrentLine, myFont, Brushes.Black, _
         LeftMargin, YPosition, New StringFormat())
      Counter += 1
      ArrayCounter += 1
   End While
   
   ' If more lines exist, print another page.
   If Not (ArrayCounter = myStrings.GetUpperBound(0)) Then
      e.HasMorePages = True
   Else
      e.HasMorePages = False
   End If
End Sub


0
 
LVL 5

Expert Comment

by:DEEPESH
ID: 17987537
Any comment on it g_johnson
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 4

Author Comment

by:g_johnson
ID: 17991933
Hi, Deepesh.  Unfortunately, as so often happens, I haven't had a chance to test yet.  Now the computer involved is "down."  I will get to it, though.  I'm sure the comments here are valid but I need to do a comprehensive test.  Thanks for your patience.
0
 
LVL 4

Author Comment

by:g_johnson
ID: 18143894
Please leave this open if you can.  I am juggling many projects right now and have not had a chance to try these solutions.
0
 
LVL 4

Author Comment

by:g_johnson
ID: 18384983
sorry it took so long -- i had to force myself to test things last night.
Thanks -- I have a much better understanding of it now.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question