I'm trying to use the PDFViewer to preview a PDF of the radgrid export on my page, but I can't find how I would export the radgrid PDF to a filesteram instead of directly to teh browser. Is this possible?
Rumen
Telerik team
commented on 30 Mar 2023, 06:22 PM
Hi Danielle,
You can use the OnGridExporting server event to show the result in a RadWindow or the PdfViewe.
Thanks, I looked at the documentation and we're finding two issues, one is locating the file being exported from the radgrid in order to assign it to the PDFViewer. The only event we see that document surfaced is the GridExporting. We used the code referenced above and assigned it to the PDFViewer, however, the PDFViewer is never populated. Further, the filestream is unable to be cancelled so it still streams to the browser, which is behavior we don't want to see. Here is the code we are using. I really can't see why the PDFViewer isn't displaying the document.
Rumen
Telerik team
commented on 04 Apr 2023, 03:18 PM
Hi Danielle,
Assigning the values to the PDF Viewer in the Grid's Export event will not have an effect, because the grid export functionality clears the headers. What you can do is save the file to the disk, cookie, or session, and in the Page_Load event, if the file exists or the data in the cookie/session exists, assign that info to the PdfViewer.
Thanks! We tweaked the code so it saves to a session variable and then redirects to cancel the download and it worked beautifully. Thanks for the direction!
Rumen
Telerik team
commented on 07 Apr 2023, 02:09 PM
Great! If you wish you can also share a code snippet of your code with your fellow developers.
Per Rumen's guidance we were able to implement this. The solution we have requires the user to select a location from a dropdown and the click a 'Get Report' button. The get report button click event triggrs the export of the document to the viewer using the following code:
ProtectedSub BtnGetReport_Click(sender AsObject, e As EventArgs)
If ddlLocation.SelectedValue <> "None"Then'Sets the session to the drop down's selected value
Session("LocationDropDownValue") = ddlLocation.SelectedValue
If RadGrid1.Items.Count > 1Then'format PDF and trigger RadGrid's export to PDF function
RadGrid1.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.AllBorders
RadGrid1.ExportSettings.Pdf.PageHeader.MiddleCell.Text = "<h4>Header 1</h4><h2>Header 2</h2>"
RadGrid1.ExportSettings.Pdf.PageHeader.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center
RadGrid1.ExportSettings.Pdf.PageFooter.MiddleCell.Text = "<?page-number?>"
RadGrid1.ExportSettings.Pdf.PageFooter.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center
RadGrid1.MasterTableView.ExportToPdf()
Session("IsPageLoad") = TrueEndIfElse
RadPdfViewer1.Visible = FalseEndIfEndSub
in the rad grid exporting event:
ProtectedSub RadGrid1_GridExporting(sender AsObject, e As GridExportingArgs)
If Session("IsPageLoad") Then'generate data for PDF viewer
Session("ReportStream") = Encoding.Default.GetBytes(e.ExportOutput)
Session("IsReportLoaded") = True
Session("IsPageLoad") = False'stop page from redirecting and cancel PDF client-side download
Response.Redirect(Request.RawUrl, True)
EndIfEndSub
and on the page load event we assign the document to the viewer and clear up all the session variables
Hi Danielle,
You can use the OnGridExporting server event to show the result in a RadWindow or the PdfViewe.
You can find more information in these resources:
Thanks, I looked at the documentation and we're finding two issues, one is locating the file being exported from the radgrid in order to assign it to the PDFViewer. The only event we see that document surfaced is the GridExporting. We used the code referenced above and assigned it to the PDFViewer, however, the PDFViewer is never populated. Further, the filestream is unable to be cancelled so it still streams to the browser, which is behavior we don't want to see. Here is the code we are using. I really can't see why the PDFViewer isn't displaying the document.
Hi Danielle,
Assigning the values to the PDF Viewer in the Grid's Export event will not have an effect, because the grid export functionality clears the headers. What you can do is save the file to the disk, cookie, or session, and in the Page_Load event, if the file exists or the data in the cookie/session exists, assign that info to the PdfViewer.
Great! If you wish you can also share a code snippet of your code with your fellow developers.
Keep up the good work!