使用GDI+的DrawString
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("SampleText",
new Font("Arial", 80, FontStyle.Bold), Brushes.Black, 150, 125);
}
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("SampleText",
new Font("Arial", 80, FontStyle.Bold), Brushes.Black, 150, 125);
}
namespace PrintExample
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO; /// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components;
private System.Windows.Forms.MenuItem HelpAbout;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.PrintDialog printDialog1;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem PrinterSettingsFile;
private System.Windows.Forms.MenuItem PreviewFile;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Drawing.Printing.PrintDocument ThePrintDocument;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem ExitFile;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.MenuItem PrintFile;
private System.Windows.Forms.MenuItem SaveFile;
private System.Windows.Forms.MenuItem OpenFile;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.RichTextBox richTextBox1;
private StringReader myReader; public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.SaveFile = new System.Windows.Forms.MenuItem();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.HelpAbout = new System.Windows.Forms.MenuItem();
this.ExitFile = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.OpenFile = new System.Windows.Forms.MenuItem();
this.PrinterSettingsFile = new System.Windows.Forms.MenuItem();
this.PrintFile = new System.Windows.Forms.MenuItem();
this.PreviewFile = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.ThePrintDocument = new System.Drawing.Printing.PrintDocument();
this.SuspendLayout();
//
// SaveFile
//
this.SaveFile.Index = 1;
this.SaveFile.Text = "Save...";
this.SaveFile.Click += new System.EventHandler(this.SaveFile_Click);
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(792, 677);
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Location = new System.Drawing.Point(664, 15);
this.printPreviewDialog1.MaximizeBox = false;
this.printPreviewDialog1.MaximumSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.Opacity = 1;
this.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty;
this.printPreviewDialog1.Visible = false;
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.HelpAbout});
this.menuItem2.Text = "Help";
this.HelpAbout.Text = "About...";
this.HelpAbout.Click += new System.EventHandler(this.HelpAbout_Click);
//
// ExitFile
//
this.ExitFile.Index = 7;
this.ExitFile.Text = "Exit";
this.ExitFile.Click += new System.EventHandler(this.ExitFile_Click);
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "-";
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.OpenFile,
this.SaveFile,
this.menuItem3,
this.PrinterSettingsFile,
this.PrintFile,
this.PreviewFile,
this.menuItem5,
this.ExitFile});
this.menuItem1.Text = "File";
//
// OpenFile
//
this.OpenFile.Index = 0;
this.OpenFile.Text = "Open...";
this.OpenFile.Click += new System.EventHandler(this.OpenFile_Click);
//
// PrinterSettingsFile
//
this.PrinterSettingsFile.Index = 3;
this.PrinterSettingsFile.Text = "Print Settings...";
this.PrinterSettingsFile.Click += new System.EventHandler(this.PrinterSettingsFile_Click);
//
// PrintFile
//
this.PrintFile.Index = 4;
this.PrintFile.Text = "Print...";
this.PrintFile.Click += new System.EventHandler(this.PrintFile_Click);
//
// PreviewFile
//
this.PreviewFile.Index = 5;
this.PreviewFile.Text = "Print Preview...";
this.PreviewFile.Click += new System.EventHandler(this.PreviewFile_Click);
//
// menuItem5
//
this.menuItem5.Index = 6;
this.menuItem5.Text = "-";
//
// saveFileDialog1
//
this.saveFileDialog1.DefaultExt = "*.txt";
this.saveFileDialog1.FileName = "myfile.txt";
this.saveFileDialog1.InitialDirectory = "c:\\";
this.saveFileDialog1.Title = "Save Text File";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// openFileDialog1
//
this.openFileDialog1.DefaultExt = "*.txt";
this.openFileDialog1.FileName = "myfile.txt";
this.openFileDialog1.InitialDirectory = "c:\\";
this.openFileDialog1.Title = "Open a Text File";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(24, 24);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(304, 256);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// ThePrintDocument
//
this.ThePrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.ThePrintDocument_PrintPage);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(352, 301);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox1});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Text Editor";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.ResumeLayout(false); } protected void HelpAbout_Click (object sender, System.EventArgs e)
{
try
{
AboutBox myDialog = new AboutBox();
myDialog.ShowDialog();
if (myDialog.DialogResult == DialogResult.OK)
{
myDialog.Dispose();
}
}
catch(Exception ex)
{
System.Console.WriteLine(ex.Message.ToString());
}
} protected void PrinterSettingsFile_Click (object sender, System.EventArgs e)
{
this.printDialog1.Document = this.ThePrintDocument;
printDialog1.ShowDialog();
} protected void Form1_Resize (object sender, System.EventArgs e)
{
this.richTextBox1.SetBounds(0, 20, this.ClientRectangle.Width - 10, this.ClientRectangle.Height - 30);
} protected void PreviewFile_Click (object sender, System.EventArgs e)
{
try
{
string strText = this.richTextBox1.Text;
myReader = new StringReader(strText);
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = this.ThePrintDocument ;
printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D ;
printPreviewDialog1.ShowDialog();
}
catch(Exception exp)
{
System.Console.WriteLine(exp.Message.ToString());
} } protected void ThePrintDocument_PrintPage (object sender, System.Drawing.Printing.PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPosition = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
Font printFont = this.richTextBox1.Font;
SolidBrush myBrush = new SolidBrush(Color.Black); // Work out the number of lines per page, using the MarginBounds.
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Iterate over the string using the StringReader, printing each line.
while(count < linesPerPage && ((line=myReader.ReadLine()) != null))
{
// calculate the next line position based on
// the height of the font according to the printing device
yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics)); // draw the next line in the rich edit control
ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
} // If there are more lines, print another page.
if(line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false; myBrush.Dispose();
} protected void PrintFile_Click (object sender, System.EventArgs e)
{
printDialog1.Document = ThePrintDocument;
string strText = this.richTextBox1.Text;
myReader = new StringReader(strText);
if (printDialog1.ShowDialog() == DialogResult.OK)
{
this.ThePrintDocument.Print();
}
} protected void ExitFile_Click (object sender, System.EventArgs e)
{
Application.Exit();
}
{
try
{
// get the file name to save the list view information in from the standard save dialog
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
// open a stream for writing and create a StreamWriter to use to implement the stream
FileStream fs = new FileStream(@saveFileDialog1.FileName , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.Flush(); // Write to the file using StreamWriter class
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin); // write the rich edit control
m_streamWriter.Write(this.richTextBox1.Text); // Close the file
m_streamWriter.Flush();
m_streamWriter.Close();
}
}
catch(Exception em)
{ } } protected void OpenFile_Click (object sender, System.EventArgs e)
{
try
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(openFileDialog1.FileName , FileMode.Open, FileAccess.Read);
StreamReader m_streamReader = new StreamReader(fs);
// Read to the file using StreamReader class m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
// Read each line of the stream and parse until last line is reached
this.richTextBox1.Text = "";
string strLine = m_streamReader.ReadLine();
while (strLine != null)
{
this.richTextBox1.Text += strLine + "\n";
strLine = m_streamReader.ReadLine();
}
// Close the stream m_streamReader.Close();
}
}
catch(Exception em)
{
System.Console.WriteLine(em.Message.ToString());
}
} /// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args)
{
Application.Run(new Form1());
}
}
}
是个完整的列子 和你的要求一样