打印功能探求之路二

 

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 lineReader= new StringReader(this.richTextBox1 .Text);

Font printFont = this.richTextBox1.Font;

SolidBrush myBrush = new SolidBrush(Color.Black);

linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);

while(count < linesPerPage && ((line=lineReader.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;

 

把上述代放到控件 printDocument1 PrintPage事件中

然后可以利用一个ButtonClick 事件中写上

    this.PrintDocument.Print()方法来用它 即可

上述代功能实现的是 控件richTextBox1中的文本内容行御打印

其他的就是GUI 程来限制 色等

同理我可以一反三  利用上取一个固定文本内

 

 

可以使用下列代打印一个固定的txt 文档内容(msdn例子http://msdn.microsoft.com/zh-cn/developercenters/ms404294(VS.85).aspx

前提要把PrintPreviewDialog 控件的 Document 属性窗体上的 PrintDocument

using System;

using System.Drawing;

using System.IO;

using System.Drawing.Printing;

using System.Windows.Forms;

 

namespace PrintPreviewApp

{

    public partial class Form1 : Form

    {

        private Button printPreviewButton;

 

        private PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();

        private PrintDocument printDocument1 = new PrintDocument();

 

        // Declare a string to hold the entire document contents.

        private string documentContents;

 

        // Declare a variable to hold the portion of the document that

        // is not printed.

        private string stringToPrint;

 

        public Form1()

        {

            this.printPreviewButton = new System.Windows.Forms.Button();

            this.printPreviewButton.Location = new System.Drawing.Point(12, 12);

            this.printPreviewButton.Size = new System.Drawing.Size(125, 23);

            this.printPreviewButton.Text = "Print Preview";

            this.printPreviewButton.Click += new System.EventHandler(this.printPreviewButton_Click);

            this.ClientSize = new System.Drawing.Size(292, 266);

            this.Controls.Add(this.printPreviewButton);

            printDocument1.PrintPage +=

                new PrintPageEventHandler(printDocument1_PrintPage);

        }

        private void ReadDocument()

        {

            string docName = "testPage.txt";

            string docPath = @"c:/";

            printDocument1.DocumentName = docName;

            using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))

            using (StreamReader reader = new StreamReader(stream))

            {

                documentContents = reader.ReadToEnd();

            }

            stringToPrint = documentContents;

        }

 

        void printDocument1_PrintPage(object sender, PrintPageEventArgs e)

        {

            int charactersOnPage = 0;

            int linesPerPage = 0;

 

            // Sets the value of charactersOnPage to the number of characters

            // of stringToPrint that will fit within the bounds of the page.

            e.Graphics.MeasureString(stringToPrint, this.Font,

                e.MarginBounds.Size, StringFormat.GenericTypographic,

                out charactersOnPage, out linesPerPage);

 

            // Draws the string within the bounds of the page.

            e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,

            e.MarginBounds, StringFormat.GenericTypographic);

 

            // Remove the portion of the string that has been printed.

            stringToPrint = stringToPrint.Substring(charactersOnPage);

 

            // Check to see if more pages are to be printed.

            e.HasMorePages = (stringToPrint.Length > 0);

 

            // If there are no more pages, reset the string to be printed.

            if (!e.HasMorePages)

                stringToPrint = documentContents;

        }

        private void printPreviewButton_Click(object sender, EventArgs e)

        {

            ReadDocument();

            printPreviewDialog1.Document = printDocument1;

        printPreviewDialog1.ShowDialog();

           

        }

 

        [STAThread]

        static void Main ()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());

        }

    }

}

提示:既然我们可以利用上述代码求取某个固定文本的内容 那我们同样可以利用我们学习过的读取本地磁盘或文件代码来实现

 

你可能感兴趣的:(String,Stream,object,null,float,button)