日期:2012-03-07  浏览次数:20411 次

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Collections;
using DataLibrary;

namespace ControlLibrary
{
/// <summary>
/// DataGrid打印
/// </summary>
public class DataGridPrinter
{
private PrintDocument ThePrintDocument;
private DataTable TheTable;
private DataGrid TheDataGrid;
public int RowCount = 0;
private const int kVerticalCellLeeway = 10;
public int PageNumber = 1;
public ArrayList Lines = new ArrayList();
public int header=0;
public int footer=0;
DataCatena DataCatena1;
public int PageWidth;
public int PageHeight;
public int TopMargin;
public int BottomMargin;
int GridWidth = 0;
public int LeftMargin;
public int RightMargin;
public string PaperName;
public int pagecount;
private int rows_page;
public bool showfooter;

public int bodylength
{
get
{
if(this.PageNumber<this.pagecount)
return (1+this.rows_page) * ((int)(TheDataGrid.Font.SizeInPoints) + kVerticalCellLeeway);
else
return (this.TheTable.Rows.Count + 1 -(this.rows_page*(this.PageNumber-1))) * ( (int)(TheDataGrid.Font.SizeInPoints) + kVerticalCellLeeway);
}
}

public DataGridPrinter(DataGrid aGrid, PrintDocument aPrintDocument,string theform,int Header,int Footer)
{
//
// TODO: Add constructor logic here
//
this.header=Header;
this.footer=Footer;
this.showfooter=true;
TheDataGrid = aGrid;
ThePrintDocument = aPrintDocument;
if(theform!="")
{
DataCatena1=new DataCatena();
DataView myDV=DataCatena1.GetDataView("select * from yh_bbdy where bm='"+theform +"'");

this.PaperName=myDV[0]["zm"].ToString();

PaperSize pkSize;

if(myDV[0]["fx"].ToString()=="True")
this.ThePrintDocument.DefaultPageSettings.Landscape=true;
else
this.ThePrintDocument.DefaultPageSettings.Landscape=false;

int found=0;
for (int i = 0; i < this.ThePrintDocument.PrinterSettings.PaperSizes.Count; i++)
{
pkSize = this.ThePrintDocument.PrinterSettings.PaperSizes[i];
if(pkSize.PaperName==this.PaperName)
{
this.ThePrintDocument.DefaultPageSettings.PaperSize=pkSize;
found=1;
i=this.ThePrintDocument.PrinterSettings.PaperSizes.Count;
if(this.ThePrintDocument.DefaultPageSettings.Landscape)
{
PageHeight = pkSize.Width;
PageWidth = pkSize.Height;
}
else
{
PageWidth = pkSize.Width;
PageHeight = pkSize.Height;
}
}
}

if(found==0)
{
if(this.ThePrintDocument.DefaultPageSettings.Landscape)
{
PageHeight = Int32.Parse(myDV[0]["zk"].ToString());
PageWidth = Int32.Parse(myDV[0]["zc"].ToString());
}
else
{
PageWidth = Int32.Parse(myDV[0]["zk"].ToString());
PageHeight = Int32.Parse(myDV[0]["zc"].ToString());
}
}

TopMargin = Int32.Parse(myDV[0]["sk"].ToString());
BottomMargin = Int32.Parse(myDV[0]["xk"].ToString());
RightMargin = Int32.Parse(myDV[0]["rightk"].ToString());
LeftMargin = Int32.Parse(myDV[0]["leftk"].ToString());
}
else
{
PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
BottomMa