.net 使用打印

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace printApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
                printDialog1.ShowDialog();
         
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                printDocument1.Print();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"打印出錯",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void printPreviewToolStripMunuItem_Click(object sender, EventArgs e)
        {
            try
            {
                printPreviewDialog1.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void PrintDoucument1_Print(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            StringReader linenReader = new StringReader(richTextBox1.Text);
            Font PrintFont = this.richTextBox1.Font;
            SolidBrush myBrush = new SolidBrush(Color.Black);
            linesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(g);
            while (count < linesPerPage && ((line = linenReader.ReadLine()) != null))
            {
                yPosition=topMargin+(count*PrintFont.GetHeight(g));
               g.DrawString(line,PrintFont,myBrush,leftMargin,yPosition,new StringFormat());
                count++;
            }
            if (line != null)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

   

    
    }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章