Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to print all list items from listview with printDocument.

My print preview like :
Item1
------
Item2
------
Item3
------
so on....


I wrote this code :-
C#
private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int yPos = 140;
            //bool more = true;
            foreach (ListViewItem xItem in listView1.Items)
            {
                e.Graphics.DrawString(xItem.SubItems[0].Text, new Font("Arial", 8.5f), Brushes.Black, 50, yPos);
                e.Graphics.DrawString(xItem.SubItems[1].Text, new Font("Arial", 8.5f), Brushes.Black, 380, yPos);
                e.Graphics.DrawString(xItem.SubItems[2].Text, new Font("Arial", 8.5f), Brushes.Black, 500, yPos);
                e.Graphics.DrawString(xItem.SubItems[3].Text, new Font("Arial", 8.5f), Brushes.Black, 500, yPos);
                yPos += 13;
                e.Graphics.DrawLine(Pens.Black, 50, yPos, 650, yPos);
            }
            e.HasMorePages = true;
        }


and printpreview code is :-
PrintPreviewDialog PPD = new PrintPreviewDialog();
            PPD.Document = printDocument2;
            PPD.Document.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);
            PPD.Document.PrinterSettings.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);
            //PPD.MdiParent = Application.OpenForms[0];
            PPD.Show();


when this code is run its added infinity documents..

I searched on google, and find something that calculate the string height and compare with printDocument graphics MarginBounds.
but in my situation how to calculate and print more pages???

Thanks in Advanced...

Regards
Jayanta.
Posted

1 solution

Hello,
here one example for printing multiple pages .

C#
int count = 0;//this variable is for total number of items in the list or array
    int i = 0;//this variable is  for no of item per page


    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        float currentY = 10;// declare  one  variable for height measurment
        e.Graphics.DrawString("Animesh", DefaultFont, Brushes.Black, 100, currentY);//this will print one string in every page of the document
        while (count <= 300) // check the number of items
        {
            e.Graphics.DrawString(count.ToString(), DefaultFont, Brushes.Black, 10, currentY); //print each item
            currentY += 20; // set a gap between every item
            count += 1; //increment count by 1
            if (i < 50) // if  the number of item(per page) is less than 50 
            {
                i += 1; // increment i by +1
                e.HasMorePages = false; // set the HasMorePages property to false , so that no other page will not be added

            }
            else // if the number of item(per page) is more than 50
            {

                i = 0;           // initiate i to 0 .
                e.HasMorePages = true;
                return;
            }

        }

    }


Thanks
Animesh
 
Share this answer
 
Comments
JayantaChatterjee 30-Sep-13 8:05am    
when i run your code it couldn't stop(its generating the numerous printing documents pages), if I canceled the the generating then its shows the print pages other wise its continue to generating the pages..
please help me...
Animesh Datta 30-Sep-13 9:32am    
dear Jayanta,
firstly i want to say , you have not used any condition .so why not it will print infinite no. of pages ? you have to give some condition according your requirement (like page height , no of item per page etc.) what i have explained in previous post , you just run that code and debug it.There i print 300 no. in 6 pages. I think it will help you.

thanks
Animesh

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