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 == true,但随后它连续地开始添加页面。你知道怎么解决吗?
发布于 2013-11-22 16:21:16
这是一个经常出现的问题,e.hasmorepages没有一个常见的行为。e.hasmorepages会一次又一次地触发printtextbox,直到您说不(e.hasmorepages=false)为止。
您必须数行,然后计算空格,如果它不适合您的论文哟决定文件是否有更多的页。
我通常使用一个整数来计算我将要打印的行,如果没有足够的空间,则使用e.hasmorages=true;
检查这个简单的示例,您必须添加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());
}
}https://stackoverflow.com/questions/20149377
复制相似问题