0

I want to print a document once using PrintDocument_PrintPage method in C#, but it is printing twice consecutively. Can you please help me with this?

Example of print result

It is printing the draft lines on top of each other as seen in the image, and it is doing this for each line. What I want to do is to print each of these draft lines seven times on each page and continue with the next line on the next page. Please help me with this.



public static int sayfano=1, k=0, j = 0, a = 0;
        public static int satir;


private void button2_Click(object sender, EventArgs e)
{
    // Yazdırma işlemi için yazdırma önizleme diyaloğunu gösterin
    PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
    // Yazdırılacak belgeyi ayarlayın
    printDocument1.DocumentName = "DataGridView Print";
    printDocument1.DefaultPageSettings.Landscape = false; // Belge yatay yönde olacak
    printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    // Yazdırma işlemi için yazdırma önizleme diyaloğunu gösterin
    printPreviewDialog.Document = printDocument1;
    printPreviewDialog.ShowDialog();
}

private int sayfa = 1;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    // Verileri oku ve diziye ata
    string[] ad_soyad = new string[dataGridView1.RowCount];
    string[] telefon = new string[dataGridView1.RowCount];
    string[] adres = new string[dataGridView1.RowCount];
    string[] ilce = new string[dataGridView1.RowCount];
    string[] aciklama = new string[dataGridView1.RowCount];

    int i = 0;
    foreach (DataGridViewRow satirs in dataGridView1.Rows)
    {
        ad_soyad[i] = satirs.Cells[2].Value.ToString();
        telefon[i] = satirs.Cells[4].Value.ToString();
        adres[i] = satirs.Cells[0].Value.ToString();
        ilce[i] = satirs.Cells[3].Value.ToString();
        aciklama[i] = satirs.Cells[5].Value.ToString();
        i++;
    }
    // font ve brush ayarları
    Font yazi_tip = new Font("Arial", 14);
    SolidBrush kalem = new SolidBrush(Color.Black);
    Pen kalems = new Pen(Color.Black);

    // Sayfa içeriği
    int j = 0, a = 0, k;
    for ( k = sayfa - 1; k < satir; k++)
    {
        // Verileri sayfaya yazdır
        e.Graphics.DrawString(ad_soyad[k], yazi_tip, kalem, 40, 30 * (a + 4 + j));
        e.Graphics.DrawString(telefon[k], yazi_tip, kalem, 40, 30 * (a + 5 + j));
        e.Graphics.DrawString(adres[k], yazi_tip, kalem, 40, 30 * (a + 6 + j));
        e.Graphics.DrawString(ilce[k], yazi_tip, kalem, 450, 30 * (a + 4 + j));
        e.Graphics.DrawString(aciklama[k], yazi_tip, kalem, 450, 30 * (a + 5 + j));
        Point[] points = { new Point(50, 30 * (a + 7 + j)), new Point(780, 30 * (a + 7 + j)) };
        e.Graphics.DrawLines(kalems, points);
        j += 4;
               
        // Sayfa sayısına göre belirleme yap
        if (a == 6)
        {
            a = 0;
            a ++;
            sayfa++; // bir sonraki sayfaya geç
            e.HasMorePages = true; // yeni sayfa olduğunu belirt
            break; // döngüden çık
        }

        a++; 
        e.Graphics.DrawString("Sayfa " + sayfa, yazi_tip, kalem, 750, 1130);
    }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • I made an attempt to format the code but it appears malformed – Mark Schultheiss Mar 19 '23 at 21:11
  • I edited it again. I forgot to add the public variables. – Ahmed Akpolat Mar 19 '23 at 21:45
  • 2
    It's a bit confusing what you mean by consecutive, but you should only add the print handler once: `printDocument1.PrintPage += printDocument1_PrintPage;` You are adding it every time you click the button. Move that line to your form's constructor. I don't see where you set the value of `satir`. Use the debugger to see what is happening, and use better variable names. You've declared `j` `a` and `k` in multiple places. – LarsTech Mar 19 '23 at 22:08

0 Answers0