C# 中打印、预览、打印机设置和打印属性的方法

 private void Form1_Load(object sender, System.EventArgs e)
  {
   //获取或设置一个值,该值指示是否发送到文件或端口
   printDocument1.PrinterSettings.PrintToFile = true;
   //设置打印时横向还是纵向
   printDocument1.DefaultPageSettings.Landscape = true;
  }
  private void fileOpenMenuItem_Click(object sender, System.EventArgs e)
  {
   OpenFile();
  }
  private void OpenFile()
  {
   openFileDialog1.Filter = "Text Files (*.txt)|*.txt";//打开文本的类型
   //获取文件对话框的初始目录(StartupPath)获得bin文件下的文件
   openFileDialog1.InitialDirectory = System.Windows.Forms.Application.StartupPath;
   DialogResult userResponse = openFileDialog1.ShowDialog();
   //MessageBox.Show(userResponse.ToString());
   if (userResponse==DialogResult.OK)
   {
    filePath = openFileDialog1.FileName.ToString();//转换文件路径
   }
  }
  private void MyPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
   //充分利用e
  {
   
   int topMargin = printDocument1.DefaultPageSettings.Margins.Top;//上边距
   int leftMargin = printDocument1.DefaultPageSettings.Margins.Left;//左边距
   float linesPerPage = 0;//页面行号
   float verticalPosition = 0;//绘制字符串的纵向位置
   float horizontalPosition=leftMargin;//左边距
   string textLine = null;//行字符串
   currentLine = 0;//行计数器
//   float Xline=0;
   //int line=0;
   // Calculate the number of lines per page.
   linesPerPage = e.MarginBounds.Height / myFont.GetHeight(e.Graphics);
//   Xline=e.MarginBounds.Width/myFont.GetHeight();
   
            // for each text line that will fit on the page, read a new line from the document
   while (currentLine < linesPerPage )
   {
    textLine = streamToPrint.ReadLine();
    if(textLine == null)
    {
     break;
    }
    // 求出已经打印的范围
    
    verticalPosition = topMargin + currentLine * myFont.GetHeight(e.Graphics);
    // 设置页面的属性
    e.Graphics.DrawString(textLine, myFont, myBrush, horizontalPosition, verticalPosition);
    // 增加行数
    currentLine ++;
    
   }
   // If more lines of text exist in the file, print another page.
   if (textLine != null)
   {
    e.HasMorePages = true;
   }
   else
   {
    e.HasMorePages = false;
   }
  }
  private void printPreviewButton_Click(object sender, System.EventArgs e)
  {
   try
   {
    streamToPrint = new StreamReader(filePath);
    try
    {
     PrintPreview();
    }
  

转载于:https://www.cnblogs.com/zengjiliang/archive/2011/10/08/2202091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值