I recently needed to hide or disable PDF format in Export Formats dropdown list displayed in SSRS ReportViewer’s toolbar in Local Report mode.
Unfortunately, ReportViewer control has no method or property to manage specific format’s visibility and this is true also for the recently released Report Viewer 10.
We can use Report Viewer’s property ShowExportControls to hide the export formats dropdown list but not a specific format such as PDF.
In Local Report mode, there are 2 export formats (Excel and PDF) and more Rendering Extensions for Server Report mode are available.
We have the option to hide export formats control and implement our own list. this article might help on this.
Finally, I ended up with adding new extension method SetExportFormatVisibility to ReportViewer control which uses private Reflection to disable/enable export formats.
This method is inspired by Stephen Songer’s blog post Disable/Enable export format in SSRS and ASP.NET.
The code consists of two parts, one is ReportViewerExtensions class which has SetExportFormatVisibility extension method and the second is ReportViewerExportFormat Enum to specify available export formats.
public static class ReportViewerExtensions { public static void SetExportFormatVisibility(this ReportViewer viewer, ReportViewerExportFormat format, bool isVisible) { string formatName = format.ToString(); const System.Reflection.BindingFlags Flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance; System.Reflection.FieldInfo m_previewService = viewer.LocalReport.GetType().GetField("m_previewService", Flags); System.Reflection.MethodInfo ListRenderingExtensions = m_previewService.FieldType.GetMethod("ListRenderingExtensions", Flags); object previewServiceInstance = m_previewService.GetValue(viewer.LocalReport); IList extensions = (IList)ListRenderingExtensions.Invoke(previewServiceInstance, null); System.Reflection.PropertyInfo name = extensions[0].GetType().GetProperty("Name", Flags); //object extension = null; foreach (var ext in extensions) { if ((string.Compare(name.GetValue(ext, null).ToString(), formatName, true) == 0)) { System.Reflection.FieldInfo m_isVisible = ext.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); System.Reflection.FieldInfo m_isExposedExternally = ext.GetType().GetField("m_isExposedExternally", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); m_isVisible.SetValue(ext, isVisible); m_isExposedExternally.SetValue(ext, isVisible); break; } } } } public enum ReportViewerExportFormat { Excel, PDF }
Simple to use..
ReportViewer1.SetExportFormatVisibility(ReportViewerExportFormat.PDF, false);
Thanks Ahmed Elbaz
your article is very help full , its magic
i only wanna to say,thank you a lot!!!!!!you saved me a lot!!!
Thanks for your article. But, I can’t able to hide Export to PDF option from Drillthrough event.
I’m calling report2.rdlc file from report1.rdlc upon clicking the link which is in report1.rdlc file. The below said code used for hiding the PDF option inside Drillthrough event.
report2.SetExportFormatVisibility(ReportViewerExportFormat.Excel, false);
but, still the export to PDF option appearing in the report. Please help me out of this problem.
thanks a lot.
appreciate your help..thanks saved lot of my time..
Thanks that was just what I needed!
Hi,
I need to manipulate export options for reports in remote mode. Also I am using framework 2.0
hi, i created a new class file and pasted the above code in that.
then called the class method from reportviewer page through
ReportViewer1.SetExportFormatVisibility(ReportViewerExportFormat.PDF, false);
but still i can see option for PDF exporting.
Version info :
Sql Server 2008 R2
Report Builder 3.0
Visual Studio 2010 Professional
A simple solution using CSS:
a[title=Excel] { display: none !important; }
a[title=PDF] { display: none !important; }
thx Joost your solution was so easy and it worked very well
thanks. Its working for me
thank you very much . Its working for me.
Hi. I have used your code, but error is
System.NullReferenceException: Object reference not set to an instance of an object.
in the Line 95
Line 93: Dim m_previewService As System.Reflection.FieldInfo = viewer.LocalReport.[GetType]().GetField(“m_previewService”, Flags)
Line 94:
Line 95: Dim ListRenderingExtensions As System.Reflection.MethodInfo = m_previewService.FieldType.GetMethod(“ListRenderingExtensions”, Flags)
Line 96: Dim previewServiceInstance As Object = m_previewService.GetValue(viewer.LocalReport)
Line 97:
I am using reportviewer 11.0, is there any version issue?
Maideen