This is a migrated thread and some comments may be shown as answers.

grid export to pdf, records per page

3 Answers 110 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Phillip
Top achievements
Rank 1
Phillip asked on 09 Nov 2011, 11:16 PM

This is fairly easy to accomplish using the <?hard-pagebreak?>. 

Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
    If e.Item.ItemIndex Mod 7 = 0 AndAlso e.Item.ItemIndex > 1 AndAlso TypeOf e.Item Is GridDataItem Then
        e.Item.Attributes.Add("pageBreak", "pageBreak")
    End If
End Sub
This inserts a pageBreak="pageBreak" on the 8th <tr>...ie..  <tr pageBreak="pageBreak">

Then using pdfExporting you replace pagebreak="pagebreak" with closing the table, the hard pagebreak, opening the new table.  It's hard to get the tags correct because I could not find any documentation about how this xhtml is fabricated, however I was able to save the raw xhtml to file and review it to get the replace correct and fix errors. 

Protected Sub RadGrid1_PdfExporting(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridPdfExportingArgs) Handles RadGrid1.PdfExporting
    Dim replacement As String = "</tbody></table><?hard-pagebreak?>" & _
                                "<table >" & _
                                    "<colgroup>" & _
                                        "<col />" & _
                                        "<col  />" & _
                                    "</colgroup>" & _
                                    "<thead>" & _
                                        "<tr>" & _
                                            "<th scope='col' > </th><th scope='col' >Print Column</th>" & _
                                        "</tr>" & _
                                    "</thead>" & _
                                    "<tbody>" & _
                                        "<tr  "
    e.RawHTML = e.RawHTML.Replace("<tr pageBreak=""pageBreak""", replacement)
    Dim FILE_NAME As String = "c:\inetpub\PDFServer\file.html"
    If System.IO.File.Exists(FILE_NAME) = True Then
        Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
        objWriter.Write(e.RawHTML)
        objWriter.Close()
    End If
End Sub

This is a really great feature of radgrid as it makes fabricating these pdfs about as easy as writing html.  I just wish there was more documentation on how to do this because it took me forever to find this information.  I hope someone else can use these examples to speed up their development. 

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 14 Nov 2011, 04:55 PM
Hi Phillip,

There is no built-in way to limit the items. You could try to use hard-pagebreak but in reality there is a significant risk to break the table layout so this may not be suitable for you.

Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Phillip
Top achievements
Rank 1
answered on 05 Dec 2011, 11:36 PM
Is there a way that I can count each row and when it hits a certain count to add the  <?hard-pagebreak?> and reset the count?  This would give me a certain number of records per page.  When would I count, onrowcreated? and then how do I insert the break. 
0
Daniel
Telerik team
answered on 08 Dec 2011, 06:56 PM
Hello Phillip,

It would be easy to calculate when to add the hard pagebreak in ItemCreated or ItemDataBound. You will need something like this if you want to break on every six lines:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
      GridDataItem item = e.Item as GridDataItem;
      if(item != null && item.ItemIndex % 6 == 0)
      {
            //insert hard pagebreak here
      }
}

The real issue is to determine where exactly to insert the hard pagebreak in your scenario. If put on wrong place, this will result in a broken layout.

Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Phillip
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Phillip
Top achievements
Rank 1
Share this question
or