我有一个 Windows 窗体,它有一个以上的页面,主要是标签和文本框,我试图保留我已经在 winform 中的字体,到目前为止我可以打印第一页,但是当我尝试添加其余的控件它会做各种奇怪的事情这是我的代码的一部分,我将所有内容都打印出来,但面板中的所有控件都不会显示在打印预览中。所以我发现面板中的控件没有按顺序排列,我需要做的是先创建打印页数,然后将控件放在这些打印页中。尝试首先创建打印页面以向其添加控件的任何帮助。它将始终是 4 个打印页。
int mainCount = 0;
public void printStuff(System.Drawing.Printing.PrintPageEventArgs e)
{
Font printFont = new Font("Arial", 9);
int dgX = dataGridView1.Left;
int dgY = dataGridView1.Top += 22;
double linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
float bottomMargin = e.MarginBounds.Bottom;
StringFormat str = new StringFormat();
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
Control ctrl;
while ((count < linesPerPage) && (panel1.Controls.Count != mainCount))
{
ctrl = panel1.Controls[mainCount];
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
mainCount++;
count++;
if (ctrl is Label)
{
e.Graphics.DrawString(ctrl.Text, printFont, Brushes.Black, ctrl.Left + 5, ctrl.Top + 40);
}
else if (ctrl is TextBox)
{
e.Graphics.DrawString(ctrl.Text, printFont, Brushes.Black, ctrl.Left + 5, ctrl.Top + 40);
e.Graphics.DrawRectangle(Pens.Black, ctrl.Left, ctrl.Top + 40, ctrl.Width, ctrl.Height);
}
}
if (count > linesPerPage)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
//Print
private void exportFileToolStripMenuItem_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
printStuff(e);
}