Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to do a little C # script, to send a number of user-defined starting number copies to the printer, generating pages with incremental numbers.
For example I ask in a windows form: the starting number (10), how many pages to print (4), and it will print, 10,11,12,13 in different pages.
I wrote this, the script sends the right pages to the printer, but a series of numbers overlap on each page.
Who can tell me where I make the mistake? Thank You very much for helping.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintLabels
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }
        
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

            int nr = Convert.ToInt32(textBox1.Text); 
            int nr_labels = Convert.ToInt32(textBox2.Text);
            int i = 0;
                                   
            for(i=0;i< nr_labels; i=i+1)            
            {             
                e.Graphics.DrawString(nr + "/" + textBox3.Text, textBox4.Font, Brushes.Black, 10, 20);
                
               nr = nr + 1;
            }
        }

        private void btn_Print_Click(object sender, EventArgs e)
        {
            int nr = Convert.ToInt32(textBox1.Text);
            int nr_labels = Convert.ToInt32(textBox2.Text);
            int copies = Convert.ToInt16(textBox2.Text);
            int i = 0;

            for (i = 0; i < nr_labels; i = i + 1)
            
            printDocument1.PrinterSettings.Copies = (short)copies;
            printDocument1.Print();            
        }
        
    }
}
Posted
Updated 23-Feb-21 5:24am
v4
Comments
[no name] 23-Feb-21 10:55am    
This one looks suspicious
            
for (i = 0; i < nr_labels; i = i + 1)                        printDocument1.PrinterSettings.Copies = (short)copies;    
printDocument1.Print();    
 
albepisti 23-Feb-21 11:28am    
ok i also deleted the for() instruction because they don't affect ...
[no name] 23-Feb-21 11:38am    
thumbsup, now please reflect this in the code of your question. You can modify the question by Editing a Question[^]

You capture the starting page number at the beginning of printDocument1_PrintPage, and you then draw the sequence of page numbers all in the same place. You need to capture the numbers when you start printing and then increment them only once for each page. Something like:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintLabels
{
    public partial class Form1 : Form
    {
        int startPage = 0;
        int pageCount = 0;
        int pageLimit = 0;

        public Form1()
        {
            InitializeComponent();
            
        }
        
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (startPage < pageLimit)
            {
                e.Graphics.DrawString(startPage.ToString() + "/" + pageCount.ToString(), textBox4.Font, Brushes.Black, 10, 20);
                
               startPage++;
            }
        }

        private void btn_Print_Click(object sender, EventArgs e)
        {
            if (Int32.TryParse(textBox1.Text, out startPage) &&
                Int32.TryParse(textBox2.Text, out pageCount))
            {
                pageLimit = startPage + pageCount;
                int copies = Convert.ToInt16(textBox2.Text); // why is this also TextBox2 ?
                printDocument1.PrinterSettings.Copies = (short)copies;
                printDocument1.Print();            
            }
            else
            {
                 // non-numeric value
            }
        }
    }
}

It would also help you to use proper meaningful names for your Textboxes rather than 1, 2, 3 etc.
 
Share this answer
 
v2
Comments
albepisti 24-Feb-21 6:16am    
thanks I'm sorry but it doesn't work. it prints always the same number.
I need to input the starting number (for example 1) and how many pages i want (for example 4), then the printer will print 4 pages each with an incremental number (e.g. 1,2,3,4). thanks again for your interest.
Richard MacCutchan 24-Feb-21 6:27am    
Sorry, but I cannot see what your code is doing. Please use the Improve question link above and edit your question and put in the actual code you are using.
hi,
after some time and several attempts, but also thanks to your suggestions, I finally came to solve my request.
For those who need it. I am attaching the code:



<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing; //added


namespace hasmorepages
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // declaration of the variables 
        PaperSize paperSize = new PaperSize("papersize", 90, 55);   // set page dimensions
        int startnr = 0;
        int itemperpage = 0;                                        // item per page
        

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            // set the fonts
            Font myfont1 = new Font("Arial", 16, FontStyle.Bold, GraphicsUnit.Point);
            Font myfont2 = new Font("Arial", 4, FontStyle.Bold, GraphicsUnit.Point);

            // set the textboxes in variables
            int beginnr = Convert.ToInt16(txt_startnr.Text);
            int howmanynr = Convert.ToInt16(txt_qty.Text);
            int endnr = beginnr + howmanynr;
            
            float currentY = 10;    // start to print from here inserting the date too
            e.Graphics.DrawString(dateTimePicker1.Text, myfont2, Brushes.Black, 30, currentY);
            currentY += 5;

            
            while (startnr <= endnr) // start the cicle of generation of the serialized numbers fron the input number 
                e.Graphics.DrawString(startnr + "/" + txt_departmnt.Text, myfont1, Brushes.Black, 18, currentY); // print each number 

                currentY += 40;             // space between rows
                startnr ++;         // increment the number

                if (itemperpage < 0)        // for each number jump to another page
                {
                    itemperpage += 1;       
                    e.HasMorePages = false; 
                }
                else                         
                {
                    itemperpage = 0;        
                    e.HasMorePages = true;  
                    return;                 
                }

                
            }
        }
        private void button1_Click(object sender, EventArgs e) //preview
        {
            itemperpage = 0;
            
            startnr = Convert.ToInt16(txt_startnr.Text);
            
            {
                printPreviewDialog1.Document = printDocument1;

                ((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Enabled = false;
                // disabilito la stampa diretta dalla anteprima così quando si fa clic sul pulsante Stampa l'evento PrintPage si attiva di nuovo.

                printDocument1.DefaultPageSettings.PaperSize = paperSize;
                printPreviewDialog1.ShowDialog();
            }
            

        }

        private void button2_Click(object sender, EventArgs e) //print
        {
            itemperpage = 0;

            startnr = Convert.ToInt16(txt_startnr.Text);

            printDocument1.PrinterSettings.PrinterName = "Microsoft Print to PDF";
            printDocument1.DefaultPageSettings.PaperSize = paperSize;
            printDocument1.Print();

        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900