C# Windows中如何实现打印预览功能

来源:百度知道 编辑:UC知道 时间:2024/06/18 18:20:21
我用C#编写的写字板如何实现打印预览功能
C# Windows中如何实现打印预览功能

加入两个控件PrintPreviewDiaog和PrintDocument
给PrintDocument添加事件:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
using (Font font = new Font("Lucda Console", 36))
{
g.DrawString(richTextBox1.Text, font, Brushes.Black, 0, 0);//或者是从文件中读取txt;
++page;
e.HasMorePages = (page <= maxPage);
}
}
然后在打印预览按钮中添加:
page = 1;
maxPage = totalPage;//最大有多少页面就设置多少
printDocument1.DocumentName = doc;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();

//这是我编写的一个预览TXT文件的代码
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;

namespace WindowsApplication14<