Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is where you are wrong. You call the <code>printDocument1.Print()</code> inside a loop. </p> <p>Try this:</p> <pre><code>public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { PrintDocument pd=new PrintDocument(); int index=0, count=0; pd.BeginPrint+=(s, ev) =&gt; { // find page count from form label count=int.Parse(label1.Text); }; pd.PrintPage+=(s, ev) =&gt; { // for each page index++; // get form size var size=this.Size; // create bitmap of same size var bmp=new Bitmap(size.Width, size.Height); // draw form into bitmap this.DrawToBitmap(bmp, new Rectangle(Point.Empty, size)); // draw bitmap into graphics, resize to fit paper margins ev.Graphics.DrawImage(bmp, new Rectangle(ev.MarginBounds.Location, ev.MarginBounds.Size)); // create a font and draw on graphics the page number using(var font = new Font(FontFamily.GenericSansSerif, 16f)) { ev.Graphics.DrawString(index.ToString(), font, Brushes.Black, ev.MarginBounds.Location); } // check for final page ev.HasMorePages=index&lt;count; }; pd.EndPrint+=(s, ev) =&gt; { // reset count and index index=0; count=0; }; PaperSize paper=new PaperSize("MyCustomSize", 100, 65); paper.RawKind=(int)PaperKind.Custom; // set paper size pd.DefaultPageSettings.PaperSize=paper; // set paper margins appropriately pd.DefaultPageSettings.Margins=new Margins(10, 10, 10, 10); // call up the print preview dialog to see results PrintPreviewDialog dlg=new PrintPreviewDialog(); dlg.Document=pd; dlg.ShowDialog(); } } </code></pre> <p>with a simple form like this:</p> <p><img src="https://i.stack.imgur.com/zKhPw.png" alt="Form"></p> <p>the <strong>only</strong> button creates a 10 page document like this:</p> <p><img src="https://i.stack.imgur.com/egsj6.png" alt="Printout"></p> <h2>Updated Code</h2> <p>Per the comments below, I changed the printing code to:</p> <pre><code>// get form size var size=groupBox1.Size; // create bitmap of same size var bmp=new Bitmap(size.Width, size.Height); // draw form into bitmap groupBox1.DrawToBitmap(bmp, new Rectangle(Point.Empty, size)); </code></pre> <p>which grabs only the groupbox for drawing.</p> <p><img src="https://i.stack.imgur.com/ILqsq.png" alt="UpdatedScreen"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload