Click here to Skip to main content
15,891,184 members
Articles / Web Development / ASP.NET
Article

Printing .NET BusinessInfoObject Class Without the Print Dialog

Rate me:
Please Sign up or sign in to vote.
4.20/5 (2 votes)
5 Mar 2007CPOL2 min read 37.8K   511   15   3
An article on how to print a .NET businessinfo object class containing simple properties and collections within the .NET application without the print dialog

Introduction

I was looking for code that can customise printing of .NET businessinfo objects from my .NET application. A businessinfo object might be an order object which contains customer name, delivery address which I wanted to print as header on each page. The order will also have properties which are collections that have to be printed after the header on each page. This code gives us the flexibility to determine exactly what went on each printed page.

Background

I have used the GDI+ Architect to lay down the properties of the businessinfo object in the print document class where I want them to be. It's a very useful tool in designing your print document class.

Using the Code

First we need to place the properties of the businessinfo object in the PrintDocument class using the GDI+ Architect tool. The key to the solution is laying down the items of the collection(DataTable) in pages properly so that they don't overlap and all of the items from the collection are printed correctly.To do this, we need to calculate how many items can be printed on each page in the space left after printing the header and also considering different page sizes and margins.

The WriteHeader() writes the header on top of each page and the WriteLineItem() writes the line items from the collection.

The _PrintGraphics() method first draws the header on each page and then draws the line items of the collection depending on how much space is left on the page.

C#
//
// private void _PrintGraphics(System.Drawing.Graphics g, PrintPageEventArgs e)
      {
          //Set rendering properties
          g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
          g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

          //Set the Page numbering variable
          txtDocumentPageNumberText = "Page " + currentPage + " of " + totalPages;

          //Startbuilding the first page by adding a header
          if ((this.currentPage == 1))
          {
              //Write a header to the current page update space remaining
              SpaceRemaningFirstPage = 
                  WriteHeader(ref g, ref CurrentY, SpaceRemaningFirstPage);
          }

          //If there are more items, then we start writing them in the 
          //remaining space
          if (dt.Rows.Count > 0)
          {
              for (int i = currentCount; i < dt.Rows.Count; i++)
              {
                  //Finish building the first page
                  if (SpaceRemaningFirstPage >= HeightRequiredByEachRowDt)
                  {
                      //If adding another item to the first page
                      if (currentPage == 1)
                      {
                          //Write out a line item
                          SpaceRemaningFirstPage = WriteLineItem(ref g, ref CurrentY, 
                                       dt.Rows[i].ItemArray, SpaceRemaningFirstPage);
                          currentCount++;
                      }
                  }
                  else
                  {
                      if (SpaceRemainingInPage >= HeightRequiredByEachRowDt)
                      {
                          if (currentPage == CurrentPrintPage)
                          {
                              //Draw the Header onto the page
                              if (StartNewPage == true)
                              {
                                  //Code to add header on every page
                                  StartNewPage = false;

                                  //Set the starting position for the page
                                  CurrentY = 0;

                                  //Write the header
                                  SpaceRemainingInPage = WriteHeader(ref g, 
                                               ref CurrentY, SpaceRemainingInPage);
                              }

                              //Write out a line item
                              SpaceRemainingInPage = WriteLineItem(ref g, ref CurrentY,
                                           dt.Rows[i].ItemArray, SpaceRemainingInPage);
                              currentCount++;
                          }
                      }
                  }
               }
            }
      }
//

The PageCreate() adds the header data and the data for the DataTable from the businessinfo object.The printer name and the margin information are also set inside this method.

C#
//
////Set the printer name
                this.PrinterSettings.PrinterName = printerName;

                //Set the margin variables and figure out how many pages 
                //we are going to need
                bottomMargin = this.DefaultPageSettings.Margins.Bottom;
                topMargin = this.DefaultPageSettings.Margins.Top;
                paperHeight = this.DefaultPageSettings.PaperSize.Height;
//

The PagePrint() method is called by the PrintPageEventHandler when the PrintPage event is called on the PrintDocument class.

C#
//
//private void PagePrint(object sender, PrintPageEventArgs e)
        {
            System.Drawing.Graphics g = e.Graphics;
            this._PrintGraphics(g, e);
            if ((this.currentPage < totalPages))
            {
                e.HasMorePages = true;
                this.currentPage = (this.currentPage + 1);
            }
            else
            {
                e.HasMorePages = false;
            }
            CurrentPrintPage = currentPage;
            //calculate the space remaining in each page
            SpaceRemainingInPage = paperHeight - bottomMargin; 
            StartNewPage = true;
        }
//

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Senior .Net Developer living and working in London,UK.

Comments and Discussions

 
NewsFreshReports [modified] Pin
Artem Smirnov5-Apr-07 5:29
professionalArtem Smirnov5-Apr-07 5:29 
GeneralMore info, please Pin
gxdata28-Feb-07 21:02
gxdata28-Feb-07 21:02 
Can you flesh this out with a VS solution and sample or demo?
ALso, I was interested to see that you used http://www.mrgsoft.com/products/GDIPlusArchitect/, and it would be nice to discuss this a little.
GeneralRe: More info, please Pin
Simana5-Mar-07 3:20
Simana5-Mar-07 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.