日期:2014-05-18  浏览次数:20700 次

如何定制C#中的打印预览
如题,C#中的printpreviewdialog预览时只能显示几个工具和简单按钮,如何对这几个按钮进行修改,或加入几个自己做的新按钮,或减少,如何实现,最好有代码
小生先谢谢了

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;

namespace MyCCTV
{
sealed class TextFilePrinter
{
string fileName;
Encoding theEncode;
Font theFont;
StreamReader srToPrint;
int currPage;

public TextFilePrinter(string fileName)
: this(fileName, Encoding.GetEncoding(936), new Font("MS UI Gothic", 9))
{
}

public TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
{
this.fileName = fileName;
this.theEncode = theEncode;
this.theFont = theFont;
}

public void Print()
{
using (srToPrint = new StreamReader(fileName, theEncode))
{
PrintDialog dlg = new PrintDialog();
dlg.Document = GetPrintDocument();
dlg.AllowSomePages = true;
dlg.AllowPrintToFile = false;
if (dlg.ShowDialog() == DialogResult.OK) dlg.Document.Print();
}
}

public void View()
{
using (srToPrint = new StreamReader(fileName, theEncode))
{
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = GetPrintDocument();
dlg.ShowDialog();
}
}

PrintDocument GetPrintDocument()
{
currPage = 1;
PrintDocument doc = new PrintDocument();
doc.DocumentName = fileName;
doc.PrintPage += new PrintPageEventHandler(PrintPageEvent);
return doc;
}

void PrintPageEvent(object sender, PrintPageEventArgs ev)
{
string line = null;
float linesPerPage = ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics);
bool isSomePages = ev.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages;
if (isSomePages)
{
while (currPage < ev.PageSettings.PrinterSettings.FromPage)
{
for (int count = 0; count < linesPerPage; count++)
{
line = srToPrint.ReadLine();