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

[复制链接]
查看11 | 回复2 | 2009-8-26 21:29:17 | 显示全部楼层 |阅读模式
安装第3方报表控件,推荐FastReport for .net调用fastreport.PreviewReport方法即可
回复

使用道具 举报

千问 | 2009-8-26 21:29:17 | 显示全部楼层
加入两个控件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{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.printPreviewDialog1 = new PrintPreviewDialog();
}
int page;
int maxPage;
String path;
String[] Lines;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
using (Font font = new Font("Lucda Console", 12))
{
int index = 0;
while ((page * 20 + index) < Lines.Length && index != pageRows)
{
g.DrawString(Lines[page * 20 + index], font, Brushes.Black, 20, 20 + 30 * index);
index++;
}
++page;
e.HasMorePages = (page <= maxPage);
}
}
public const int pageRows = 37;
public const int rowNumber = 42;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt|*.txt";
if (open.ShowDialog() == DialogResult.OK)
{
page = 0;
path = open.FileName;
StringArray sa = new StringArray(0);
try
{
Lines = File.ReadAllLines(path, Encoding.GetEncoding(0, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback));
}
catch { Lines = File.ReadAllLines(path, Encoding.UTF8); }
for (int i = 0; i < Lines.Length; i++)
{
int length = Lines.Length;
for (int j = 0; j < length / rowNumber; j++)
{
sa.Add(Lines.Substring(j * rowNumber, rowNumber));
}
sa.Add(Lines.Substring((length / rowNumber) * rowNumber));
}
Lines = sa.Lines;
maxPage = Lines.Length / pageRows + 1;
printDocument1.DocumentName = path;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
else
{
return;
}
}
}}
回复

使用道具 举报

千问 | 2009-8-26 21:29:17 | 显示全部楼层
刚看下书……使用PageSetupDialog窗体……找一找吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行