把内容输出到 Microsoft Office Document Image Writer 里面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
StringReader printReader;
const string printNews = "是否打印文档?";
const string affirm = "确认";
private void printDocument_PrintPage(object sender, 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; //行字符串
Font printFont = new Font("Arial", 10); //当前的打印字体
SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的行数
//逐行的循环打印一页
while (count < linesPerPage && ((line = printReader.ReadLine()) != null))
{
yPosition = topMargin + (count * printFont.GetHeight(g));
g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
}
// 如果本页打印完成而line不为空说明还有没完成的页面这将触发下一次的打印事件在下一次的打印中lineReader会自动读取上次没有打印完的内容因为lineReader是这个打印方法外的类的成员它可以记录当前读取的位置
if (line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}

private void Print_Test()
{
printReader = new StringReader("55jgfjfhgfhgdfhfdgdsgfdsfgsdfs\nhdhd\rgfdgd");
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
if (MessageBox.Show(printNews, affirm, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
printDocument.Print();
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Print_Test();
}
}
}文件就可以 Microsoft Office Document Image Writer ,可以保存到mdi文件里面


浙公网安备 33010602011771号