如何打印从WinForms的多个页面? [英] How do I print multiple pages from WinForms?

查看:99
本文介绍了如何打印从WinForms的多个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有网络上的一些教程,我还是输了为什么这不能正确打印多个页面。我在做什么错了?



 公共静态无效printTest()
{
PrintDialog类printDialog1 =新PrintDialog类();
的PrintDocument printDocument1 =新的PrintDocument();

printDialog1.Document = printDocument1;
printDocument1.PrintPage + =
新PrintPageEventHandler(printDocument1_PrintPage);

DialogResult的结果= printDialog1.ShowDialog();
如果(结果== DialogResult.OK)
{
printDocument1.Print();
}
}

静态无效printDocument1_PrintPage(对象发件人,PrintPageEventArgs E)
{
图形图形= e.Graphics;
SolidBrush刷=新SolidBrush(Color.Black);

字体的字体=新的字体(宋体,12);

e.PageSettings.PaperSize =新PAPERSIZE(A4,850,1100);

浮页宽= e.PageSettings.PrintableArea.Width;
浮动pageHeight = e.PageSettings.PrintableArea.Height;

浮动fontHeight = font.GetHeight();
INT运行startx = 40;
INT startY = 30;
INT offsetY = 40;

的for(int i = 0; I< 100;我++)
{
graphic.DrawString(行:+ I,字体,画笔,运行startx,startY + offsetY);
offsetY + =(INT)fontHeight;

如果(offsetY> = pageHeight)
{
e.HasMorePages = TRUE;
offsetY = 0;
}
,否则{
e.HasMorePages = FALSE;
}
}
}

您可以找到的一个例子这段代码的位置打印结果:打印件

解决方案

您从来没有从环路返回。将其更改为:



<预类=郎-CS prettyprint-覆盖> 如果(offsetY> = pageHeight)
{
e.HasMorePages = TRUE;
offsetY = 0;
的回报; //你需要返回,那么它会进入这个功能再次
}
,否则{
e.HasMorePages = FALSE;
}



另外,你需要改变循环在对当前一些启动第2页,而不是再次重新启动我= 0。


Although there are some tutorials on web, I'm still lost on why this doesn't print multiple pages correctly. What am I doing wrong?

public static void printTest()
{
   PrintDialog printDialog1 = new PrintDialog();
   PrintDocument printDocument1 = new PrintDocument();

   printDialog1.Document = printDocument1;
   printDocument1.PrintPage += 
       new PrintPageEventHandler(printDocument1_PrintPage);

   DialogResult result = printDialog1.ShowDialog();
   if (result == DialogResult.OK)
   {
       printDocument1.Print();
   }       
}

static void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
   Graphics graphic = e.Graphics;
   SolidBrush brush = new SolidBrush(Color.Black);

   Font font = new Font("Courier New", 12);

   e.PageSettings.PaperSize = new PaperSize("A4", 850, 1100);

   float pageWidth = e.PageSettings.PrintableArea.Width;
   float pageHeight = e.PageSettings.PrintableArea.Height;

   float fontHeight = font.GetHeight();
   int startX = 40;
   int startY = 30;
   int offsetY = 40;

   for (int i = 0; i < 100; i++)
   {             
       graphic.DrawString("Line: " + i, font, brush, startX, startY + offsetY);
       offsetY += (int)fontHeight;

       if (offsetY >= pageHeight)
       {
           e.HasMorePages = true;
           offsetY = 0;
       }
       else {
           e.HasMorePages = false;
       }
   }
}

You can find an example of this code's printed result here: Printed Document

解决方案

You never return from the loop. Change it to:

if (offsetY >= pageHeight)
{
    e.HasMorePages = true;
    offsetY = 0;
    return; // you need to return, then it will go into this function again
}
else {
    e.HasMorePages = false;
}

In addition you need to change the loop to start at the current number on the 2nd page instead of restarting with i=0 again.

这篇关于如何打印从WinForms的多个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆