110,580
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using System.Collections;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
namespace WindowsApplication2
{
public partial class PrintingExample : Form
{
public PrintingExample()
{
InitializeComponent();
}
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
public static float Scalt=(float)1;
private Font printFont;
private StreamReader streamToPrint;
bool flag = true;
ArrayList arraylist;
int count = 0;
string filePath;
// The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = "Text file(*.txt)|*.txt|All files(*.*)|*.*";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
filePath = openFileDialog1.FileName;
}
catch (Exception error)
{
MessageBox.Show("错误信息是: " + error.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{
if(textBox1.Text.Trim() !=null)
Scalt = float.Parse(textBox1.Text.Trim());
streamToPrint = new StreamReader
(filePath, Encoding.GetEncoding("gb2312"));
arraylist = new ArrayList();
count = 0;
flag = true;
printFont = new Font("Tahoma", 10 * Scalt);
try
{
//printFont = new Font("Tahoma", 10 * Scalt);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage);
PrintPreviewDialog dig = new PrintPreviewDialog();
dig.Document = pd;
dig.ShowDialog();
//pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
int linesPerPage = (int) (ev.MarginBounds.Height /printFont.GetHeight(ev.Graphics));
float yPos = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
string linePage=null;
if (flag)
{
int FontSize = (int)(ev.PageBounds.Width / ev.Graphics.MeasureString("啊", printFont).Width);
while (
((line = streamToPrint.ReadLine()) != null))
{
int rows =line.Length / FontSize;
int leavings = line.Length % FontSize;
if (rows > 0)
{
for (int i = 0; i <= rows;i++ )
{
if (leavings > 0 && i == rows)
{
linePage = line.Substring(i * FontSize);
}
else if (leavings ==0 && i==rows)
{
break;
}
else
{
linePage = line.Substring(i * FontSize, FontSize);
}
arraylist.Add(linePage);
}
}
else
{
arraylist.Add(line);
}
}
flag = false;
}
int page = 0;
while (
count < arraylist.Count)
{
linePage = (string)arraylist[count];
yPos = topMargin + (page*
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(linePage, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
page++;
if(((count%linesPerPage)==0)&&(count>=linesPerPage))
break;
}
if (count <arraylist.Count)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
private void PrintingExample_Load(object sender, EventArgs e)
{
this.textBox1.Text = "1";
}
private void PrintingExample_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
}