SIGN UP MEMBER LOGIN:    
ARTICLE

The DocumentName Property and Marginal Printing in GDI+

Posted by Dinesh Beniwal Articles | GDI+ & Graphics June 03, 2010
In this article I will explain about the DocumentName Property and Marginal Printing in GDI+.
Reader Level:

This article has been excerpted from book "Graphics Programming with GDI+".

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

The DocumentName Property and Marginal Printing in GDI+.

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.

Figure 11.25.gif


FIGURE 11.25: Print preview of multiple pages

Figure 11.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.

Figure 11.27.gif

FIGURE 11.27: Marginal-printing test application

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


        private void NormalBtn_Click(object sender, System.EVentArgs e)
        {
            //Create a PrintDocument object
            PrintDocument pd = new PrintDocument();

            //Add PrintPage event handler
            pd.PrintPage += new PrintPageEventHandler(NormalPrinting);

            //Print
            pd.Print();
        }

        private void MarginalBtn_Click(object sender, System.EventArgs e)
        {
            //Create a PrintDocument object
            PrintDocument pd = new PrintDocument();

            //Add PrintPage event handler
            pd.PrintPage += new PrintPageEVentHandler(MarginPrinting);

            //Print
            pd.Print();
        }


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 void NormalPrinting(object sender, PrintPageEventArgs ev)
        {
            //Set the top position as 1
            float ypos = 1;

            //Get the default left margin
            float leftMargin = ev.MarginBounds.Left;

            //Create a font
            Font font = new Font("Arial", 16);

            //Get the font's height
            float fontheight = 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);
        }


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 void MarginPrinting(object sender, PrintPageEventArgs ev)
        {
            //Set the top position as the default margin
            float ypos = ev.MarginBounds.Top;

            //Get the default left margin
            float leftMArgin = ev.MarginBounds.Left;

            //Create a font
            Font font = new Font("Arial", 16);

            //Get the font's height
            float fontheight = 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);
        }


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.

book.gif


.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor