The DocumentName Property and Marginal Printing in VB.NET

In this article I will explain about the DocumentName Property and Marginal Printing in GDI+.
  • 3474

The DocumentName Property

If you want to display the name of the document you're printing, you can use the DocumentName property of the PrintDocument object:


pd.DocumentName = "A Text Document"

The new result is shown in Figure 11.26.

We have seen that using the DocumentPrintPreview class is fairly straightforward. In reality, all that's happening is that this control is passed a graphics class representing each page in a printout.

Figure2011_25.gif


FIGURE 11.25: Print preview of multiple pages

Figure2011_26.gif

FIGURE 11.26: Setting a document name

Marginal Printing: A Caution

Although it's exciting to be able to draw graphics on a printout, keep in mind that printers have limits. Never try to print at the extreme edges page because you cannot be sure that a printer will print in exactly the same place. You could have two printers of the same model and manufacturer and yet when you print you may notice they print in different places. Some printers are more accurate than others, but usually a sheet of paper will move slightly as it moves through the printer. Laser printers tend to be able to print closer to the edges of the paper than inkjet printers because of the mechanism that is used to transport the sheet of paper thought the printer.

To see a marginal-printing sample, let's create a Windows application. We add two buttons to the form. The final form is shown in Figure 11.27.

Now we add code for the Normal Printing and Marginal Printing button click event handlers, as in Listing 11.47. Each handler creates a PrintDocument object, adds a PrintPage event handler, and calls the Print method. The PrintPage event handlers for Normal Printing and Marginal Printing are NormalPrinting and MarginPrinting, respectively.

Figure2011_27.gif

FIGURE 11.27: Marginal-printing test application

LISTING 11.47: The Normal Printing and Marginal Printing button event handlers


    Private Sub NormalBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'Create a PrintDocument object
        Dim pd As New PrintDocument()

        'Add PrintPage event handler
        pd.PrintPage += New PrintPageEventHandler(NormalPrinting)

        'Print
        pd.Print()
    End Sub

    Private Sub MarginalBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'Create a PrintDocument object
        Dim pd As New PrintDocument()

        'Add PrintPage event handler
        pd.PrintPage += New PrintPageEVentHandler(MarginPrinting)

        'Print
        pd.Print()
    End Sub

Now let's look at the NormalPrinting handler (see Listing 11.48). We start with the top location of the text as unit 1. Then we calculated the nest line's position using the height of the font and draw lines with the values of the top, left, bottom, and right margins. In the end we draw a rectangle with the default bounds of the page.

LISTING 11.48: The NormalPrinting event handler


    Public Sub NormalPrinting(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        'Set the top position as 1
        Dim ypos As Single = 1

        'Get the default left margin
        Dim leftMargin As Single = ev.MarginBounds.Left

        'Create a font
        Dim font As New Font("Arial", 16)

        'Get the font's height
        Dim fontheight As Single = font.GetHeight(ev.Graphics)

        'Draw four strings
        ev.Graphics.DrawString("Top Margin = " & ev.MarginBounds.Top.ToString(), font, Brushes.Black, leftMargin, ypose)
        ypos = ypos + fontheight
        ev.Graphics.DrawString("Bottom Margin = " & ev.MarginBounds.Bottom.ToString(), font, Brushes.Black, leftMargin, ypose)
        ypos = ypos + fontheight
        ev.Graphics.DrawString("Left Margin = " & ev.MarginBounds.Left.ToString(), font, Brushes.Black, leftMargin, ypose)
        ypos = ypos + fontheight
        ev.Graphics.DrawString("Right Margin = " & ev.MarginBounds.Right.ToString(), font, Brushes.Black, leftMargin, ypose)
        ypos = ypos + fontheight

        'Draw a rectangle with defualt margins
        ev.Graphics.DrawRectangle(New Pen(Color.Black), ev.MarginBounds.X, ev.MarginBounds.Y, ev.MarginBounds.Width, ev.MarginBounds.Height)
    End Sub

If we run the application, we will see text describing the four margins values printed outside the rectangle.

Next comes code for the MarginPrinting event handler (see Listing 11.49). We use the default margin of the page as the top location for the first text. Everything else is the same as in Listing 11.48.

LISTING 11.49: The MarginPrinting event handler


    Public Sub MarginPrinting(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        'Set the top position as the default margin
        Dim ypos As Single = ev.MarginBounds.Top

        'Get the default left margin
        Dim leftMArgin__1 As Single = ev.MarginBounds.Left

        'Create a font
        Dim font As New Font("Arial", 16)

        'Get the font's height
        Dim fontheight As Single = font.GetHeight(ev.Graphics)

        'Draw four strings
        ev.Graphics.DrawString("Top Margin = " & ev.MarginBounds.Top.ToString(), font, Brushes.Black, leftMargin, ypose)
        ypos = ypos + fontheight
        ev.Graphics.DrawString("Bottom Margin = " & ev.MarginBounds.Bottom.ToString(), font, Brushes.Black, leftMargin, ypose)
        ypos = ypos + fontheight
        ev.Graphics.DrawString("Left Margin = " & ev.MarginBounds.Left.ToString(), font, Brushes.Black, leftMargin, ypose)
        ypos = ypos + fontheight
        ev.Graphics.DrawString("Right Margin = " & ev.MarginBounds.Right.ToString(), font, Brushes.Black, leftMargin, ypose)
        ypos = ypos + fontheight

        'Draw a rectangle with default margins
        ev.Graphics.DrawRectangle(New Pen(Color.Black), ev.MarginBounds.X, ev.MarginBounds.Y,ev.MarginBounds.Width, ev.MarginBounds.Height)
    End Sub

When we run this code, we will se text appearing inside the rectangle printed using the page margin values.

Conclusion

Hope the article would have helped you in understanding the DocumentName Property and Marginal Printing in GDI+. Read other articles on GDI+ on the website.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.