65.9K
CodeProject 正在变化。 阅读更多。
Home

无需打印对话框即可打印 .NET BusinessInfoObject 类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (2投票s)

2007年2月14日

CPOL

2分钟阅读

viewsIcon

38365

downloadIcon

527

一篇关于如何在 .NET 应用程序中,无需打印对话框即可打印包含简单属性和集合的 .NET businessinfo 对象类的文章。

引言

我一直在寻找可以自定义从 .NET 应用程序打印 .NET businessinfo 对象的代码。 businessinfo 对象可能是一个 order 对象,其中包含客户姓名、送货地址,我想将其作为每页的标题打印出来。 订单也将具有必须在每页标题之后打印的集合属性。 这段代码使我们能够灵活地确定每张打印页面上的内容。

背景

我使用了 GDI+ Architect 来布局 businessinfo 对象的属性,放置在我想放置它们的打印文档类中。 它是设计打印文档类的一个非常有用的工具。

Using the Code

首先,我们需要使用 GDI+ Architect 工具将 businessinfo 对象的属性放置在 PrintDocument 类中。 该解决方案的关键是正确地在页面中放置集合 (DataTable) 的项目,以便它们不会重叠,并且集合中的所有项目都能正确打印。为此,我们需要计算在打印标题后剩余的空间中,以及考虑到不同的页面大小和边距,每页可以打印多少个项目。

WriteHeader() 将标题写入每页顶部,而 WriteLineItem() 从集合中写入行项目。

_PrintGraphics() 方法首先在每页上绘制标题,然后根据页面上剩余的空间绘制集合的行项目。

//
// 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++;
                          }
                      }
                  }
               }
            }
      }
//

PageCreate()businessinfo 对象添加标题数据和 DataTable 的数据。 打印机名称和边距信息也在此方法中设置。

//
////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;
//

当在 PrintDocument 类上调用 PrintPage 事件时,PagePrint() 方法由 PrintPageEventHandler 调用。

//
//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;
        }
//
© . All rights reserved.