用C#在printdocument中打印更多页面。 [英] printing more pages in printdocument in C#.

查看:193
本文介绍了用C#在printdocument中打印更多页面。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用listDocument打印listview 中的所有列表项。



我的打印预览如:

Item1

------

Item2

------

Item3

------

等......




我写了这段代码: -

I want to print all list items from listview with printDocument.

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


I wrote this code :-

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;
        }





和printpreview代码是: -



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();





当此代码运行时添加了无限文档..



我在谷歌搜索,找到计算字符串高度的东西,并与printDocument图形MarginBounds进行比较。

但在我的情况下如何计算和打印更多页面? ??



感谢高级...



问候

Jayanta 。



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.

推荐答案

您好,

这里有一个打印多页的例子。



Hello,
here one example for printing multiple pages .

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;
            }

        }

    }





谢谢

Animesh



Thanks
Animesh


这篇关于用C#在printdocument中打印更多页面。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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