我得到了一个窗体,其中有多个主要标签和文本框的页面,我试图保留我已经在winform中的字体,到目前为止我能够打印的第一页,但是当我尝试添加其余的控件时,它做了各种奇怪的东西,这是我的代码的一部分,我把所有的东西打印,但并非面板中的所有控件显示打印预览。所以我发现面板上的控件没有按顺序,我需要做的是先创建打印页面的数量,然后将控件放在这些打印页面中。首先尝试创建打印页面以将控件添加到它的任何帮助。它将始终是4个打印页面。打印时打印WinForform中的所有控件c#使用PrintDocument
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);
}
和究竟是这些确实奇怪的事情? – 2013-03-07 16:53:28
在第一页之后,它将整个页面上的其他控件打印出来,并且很多页面都是空白的。 – 2013-03-07 16:57:40
该代码假定* mainCount *与应打印控件的页面有关。情况并非如此,控制的位置属性是重要的。还必须对页码进行调整。并且BeginPrint缺失将计数器重置为0.让表格不适合放在纸上也非常不寻常。 – 2013-03-07 17:51:47