开发者

Print 3 records per page to PDF

开发者 https://www.devze.com 2023-03-31 02:01 出处:网络
I want to create dynamic PDF document pages as per my record. Please help me. I want to print 3 records per page.

I want to create dynamic PDF document pages as per my record. Please help me.

I want to print 3 records per page.

string[] collection = {
    "vivek", "kashyap", "viral", "darshit", "arpit", "sameer", "vanraj"
};

PdfDocument pdfDoc = new PdfDocument();

int records = collection.Length;
int perpage = 3;
int pages = (int)Math.Ceiling((double)records / (double)perpage);


for (int p = 0; p < pages; p++)
{          
    PdfPage pdfPage = new PdfPage();
    pdfPage.Size = PageSize.Letter;
    pdfDoc.Pages.Add(pdfPage);
    XFont NormalFont = new XFont("Helvetica", 10, XFontStyle.Regular);

    using (var pdfGfx = XGraphics.FromPdfPage(pdfPage))
    {
        for (int i = 0,next = 100; i < collection.Length; i++)
        {
            pdfGf开发者_Go百科x.DrawString( "Name : " + collection[i].ToString()
                             , NormalFont, XBrushes.Black, 55, next
                             , XStringFormats.Default);
            next += 20;
        }
    }
}


I assume the code as you have presented it is showing the same top entries? What you need to do is maintain the start of each 3 entries as you move from page to page. I have called this variable idx and updated your code below (note I haven't actually compiled it except in my head).

string[] collection = { "vivek", "kashyap", "viral", "darshit", "arpit", "sameer", "vanraj" };

PdfDocument pdfDoc = new PdfDocument();

int records = collection.Length;
int perpage = 3;
int pages = (int)Math.Ceiling((double)records / (double)perpage);

int idx = 0;

for (int p = 0; p < pages; p++)
{

    PdfPage pdfPage = new PdfPage();
    pdfPage.Size = PageSize.Letter;
    pdfDoc.Pages.Add(pdfPage);
    XFont NormalFont = new XFont("Helvetica", 10, XFontStyle.Regular);
    using (var pdfGfx = XGraphics.FromPdfPage(pdfPage))
    {
        for (int i = 0,next = 100; i < perpage; i++)
        {
            if ((idx + i) >= records.length) break;
            pdfGfx.DrawString("Name : " + collection[idx  + i].ToString(), NormalFont,
                XBrushes.Black, 55, next, XStringFormats.Default);

            next += 20;
        }
    }

    idx += perpage;

}


I believe that with this code:

for (int i = 0,next = 100; i < collection.Length; i++)

you are looping on all records of your collection.

you should redesign your code and only print 3 records there then you can switch to the next page and print the next 3 records and so on.

you can do this with break command which exits the loop but you should have a variable to store the reference or index or latest printed record so you can continue with the next one on the next page.

I would re-think at the whole code because can be that nesting of the loops could be improved, for example personally I would have a main loop at the top level which loops on all records and not on all pages, so you can switch to the next page in the body of the main loop but never leave the loop on all records untill all of them are printed out.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号