C# Windows中如何实现打印预览功能 5
3个回答
展开全部
加入两个控件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[i].Length;
for (int j = 0; j < length / rowNumber; j++)
{
sa.Add(Lines[i].Substring(j * rowNumber, rowNumber));
}
sa.Add(Lines[i].Substring((length / rowNumber) * rowNumber));
}
Lines = sa.Lines;
maxPage = Lines.Length / pageRows + 1;
printDocument1.DocumentName = path;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
else
{
return;
}
}
}
}
给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[i].Length;
for (int j = 0; j < length / rowNumber; j++)
{
sa.Add(Lines[i].Substring(j * rowNumber, rowNumber));
}
sa.Add(Lines[i].Substring((length / rowNumber) * rowNumber));
}
Lines = sa.Lines;
maxPage = Lines.Length / pageRows + 1;
printDocument1.DocumentName = path;
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
else
{
return;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询