NET岛

导航

实现打印

PrintDocument myPrintDocument = new PrintDocument();
Print方法初始化,引发一个或多个PrintPage事件

PrintPage事件参数:PrintPageEventArgs对象
Cancel 取消打印作业
Graphics 打印内容
HasMorePages 是否打印附加页面
MarginBounds 页边空白部分范围
PageBounds 总页面区域
PageSettings 当前页面设置

多页面处理
bool FirstPagePrinted = false;
public void PrintBigEllipse(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
  if (FirstPagePrinted == false)
  {
    FirstPagePrinted = true;
    e.HasMorePages = true;
    e.Graphics.Draw....
  }
  else
  {
    e.HasMorePages = false;
    FirstPagePrinted = false;
    e.Graphics.Draw....
  }
}

打印多行
int ArrayCounter = 0;
Private void PrintStrings(object sender, PrintPageEventArgs e)
{
  float LeftMargin = e.MarginBounds.Left;
  float TopMargin = e.MarginBounds.Top;
  float MyLines = 0;
  float YPosition = 0;
  int Counter = 0;
  string CurrentLine;

  //计算每页的行数
  MyLines = e.MarginBounds.Height / myFont.GetHeight(e.Graphics);
 
  //打印文件的每行,但在页面的末尾停止
  while (Counter < MyLines && ArrayCounter <= myStrings.GetUpperBound(0))
  {
    CurrentLine = myStrings[ArrayCounter];
    YPosition = TopMargin + Counter * myFont.GetHerght(e.Graphics);
    e.Graphics.DrawString(CurrentLine, myFont, Brushes.Black, LeftMargin,YPosition, new StringFormat());
    Counter++;
    ArrayCounter++;
  }

  //如果存在更多的行,打印另一页
  if (!(ArrayCounter == myStrings.GetUpperBound(0)))
    e.HasMorePages = true;
  else
    e.HasMorePages = false;
}

黑白和彩色打印
Brush BrushOne;
Brush BrushTwo;
if (PrintDocument1.PrinterSettings.SupportsColor == true)
{
  //彩色
  BrushOne = Brushes.Red;
  BrushTwo = Brushes.Blue;
}
else
{
  //黑白
  BrushOne = new HatchBrush(HatchStyle.DarkVertical, Color.Black);
  BrushTwo = new HatchBrush(HachStyle.DashedHorizontal, Color.Black);
}

实现预览 PrintPreviewControl
myPrintPreview.Document = myPrintDocument;
myPrintPreview.InvalidatePreview();

PrintPreviewDialog 预览对话框控件,使用方法Show  ShowDialog

配置打印
PrintDocument.PrinterSettings属性
默认 DefaultPageSettings

PrintDialog,设置PrintDocument对象的PrinterSettings属性
PrintDialog1.ShowDialog();

PageSetupDialog设置页面设置和打印机设置

运行时配置PageSetting
e.PageSettings.Landscape = true;


posted on 2005-08-25 17:33  左佩玉  阅读(476)  评论(0编辑  收藏  举报