[英]Print multiple pages from TextBox
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();
}
I tried in PrintTextBox
method using the e.HasMorePages == true
but then it continously started to add pages. 我尝试使用e.HasMorePages == true
在PrintTextBox
方法中,但是随后它连续开始添加页面。 Do you have any idea how to solve it? 您知道如何解决吗?
This is a often problem, e.hasmorepages has not a common behavior. 这是一个经常出现的问题,例如,hasmorepages并不常见。 e.hasmorepages will fire printtextbox over and over again until you say not to (e.hasmorepages=false). e.hasmorepages将一遍又一遍地触发printtextbox,直到您拒绝为止(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. 您必须计算行数,然后计算空间,如果它不适合您的纸张,则请确定文档是否有更多页面。
I normally use an integer that counts the lines I will print, if not enough space then e.hasmorages=true; 我通常使用一个整数来计数要打印的行数,如果没有足够的空间,则e.hasmorages = true;
Check this easier example, you have to add system.drawing.printing 检查这个更简单的示例,您必须添加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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.