问题描述
private void PrintTextBox(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(textBox1.Text, textBox1.Font, Brushes.Black, 50, 20);
}
private void printListButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintTextBox;
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pd;
ppd.ShowDialog();
}
我试过 PrintTextBox
使用 e.HasMorePages方法==真
但随后continously开始添加页面。你有任何想法如何解决呢?
I tried in PrintTextBox
method using the e.HasMorePages == true
but then it continously started to add pages. Do you have any idea how to solve it?
推荐答案
这是一个经常的问题,e.hasmorepages还没有一个共同的行为。直到你说不(e.hasmorepages = FALSE)e.hasmorepages将火printtextbox一遍又一遍。
This is a often problem, e.hasmorepages has not a common behavior. e.hasmorepages will fire printtextbox over and over again until you say not to (e.hasmorepages=false).
您要算行,然后计算空间,如果它不将论文适合哟决定,如果文件有多个页面或不。
you have to count the lines, then calculate the space, and if it does not fit in your paper yo decide if document have more pages or does not.
我通常使用计数我将打印线的一个整数,如果不然后有足够的空间e.hasmorages = TRUE;
I normally use an integer that counts the lines I will print, if not enough space then e.hasmorages=true;
勾选此简单的例子,你必须添加system.drawing.printing
Check this easier example, you have to add system.drawing.printing
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string[] Lines = new string[10];
private int CurrentRow = 0;
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Lines[i] = i.ToString("N2");
}
PrintDocument pd=new PrintDocument();
PrintDialog pdi = new PrintDialog();
pdi.ShowDialog();
pd.PrinterSettings = pdi.PrinterSettings;
pd.PrintPage += PrintTextBox;
pd.Print();
}
private void PrintTextBox(object sender, PrintPageEventArgs e)
{
int y = 0;
do
{
e.Graphics.DrawString(Lines[CurrentRow],new Font("Calibri",10),Brushes.Black,new PointF(0,y));
CurrentRow += 1;
y += 20;
if (y > 20) // max px per page
{
e.HasMorePages = CurrentRow != Lines.Count(); // check if you need more pages
break;
}
} while(CurrentRow < Lines.Count());
}
}
这篇关于打印文本框从多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!