Aamirkhan
Aamirkhan

Reputation: 5794

Export Datalist to PDF with restricted rows per Page

I am trying to export SqlData to pdf,for that i am trying to get data list to print preview.I have followed this example.http://www.aspdotnet-suresh.com/2011/04/how-to-export-gridview-data-to-pdf.html here grid view data Displayed.But I have done some modification and tried to to take data on DataList.Now every thing works fine,I am able to take print preview of DataList.Only one problem is there,I want to restrict number of data display per page..Like i just want to Restrict 10Row per single page of pdf.Any one have idea how to achieve such functionality?

In short i need a page break functionality on exported pdf, here is pdf i am getting currently

Sorry for if my question is too simple,But i am newbie on Asp.Net

Thanks

Upvotes: 3

Views: 1316

Answers (1)

Puneet Goel
Puneet Goel

Reputation: 11

The approach show on that link is bit simpler but you can modify that to resolve your problem. Here is a bit of code:

using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace RowsCountSample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var pdfDoc = new Document(PageSize.A4))
            {
                var pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));
                pdfDoc.Open();

                var table1 = new PdfPTable(3);
                table1.HeaderRows = 2;
                table1.FooterRows = 1;

                //header row 
                var headerCell = new PdfPCell(new Phrase("header"));
                headerCell.Colspan = 3;
                headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
                table1.AddCell(headerCell);

                //footer row 
                var footerCell = new PdfPCell(new Phrase("footer"));
                footerCell.Colspan = 3;
                footerCell.HorizontalAlignment = Element.ALIGN_CENTER;
                table1.AddCell(footerCell);

                //adding some rows 
                for (int i = 0; i < 70; i++)
                {
                    //adds a new row
                    table1.AddCell(new Phrase("Cell[0], Row[" + i + "]"));
                    table1.AddCell(new Phrase("Cell[1], Row[" + i + "]"));
                    table1.AddCell(new Phrase("Cell[2], Row[" + i + "]"));

                    //sets the number of rows per page
                    if (i > 0 && table1.Rows.Count % 7 == 0)
                    {
                        pdfDoc.Add(table1);
                        table1.DeleteBodyRows();
                        pdfDoc.NewPage();
                    }
                }

                pdfDoc.Add(table1);
            }

            //open the final file with adobe reader for instance. 
            Process.Start("Test.pdf");
        }
    }
}

Upvotes: 1

Related Questions