I make use of a ReportView component in a VB.Net 2005 app. How can I disable the PDF export functionality, only keeping the MS Excel format?
//Leave only PDF option, hide everything.
$(document).ready(function () {
$("a[title='XML file with report data']").parent().hide();
$("a[title='CSV (comma delimited)']").parent().hide();
$("a[title='IMAGE']").parent().hide();
$("a[title='MHTML']").parent().hide();
$("a[title='Excel']").parent().hide();
$("a[title='Word']").parent().hide();
$("a[title='PowerPoint']").parent().hide();
$("a[title='Data Feed']").parent().hide();
$("a[title='MHTML (web archive)']").parent().hide();
$("a[title='TIFF file']").parent().hide();
});
I had exactly the same problem and solved using the following C# method, found here!:
public void DisableUnwantedExportFormat(ReportViewer ReportViewerID, string strFormatName)
{
FieldInfo info;
foreach (RenderingExtension extension in ReportViewerID.LocalReport.ListRenderingExtensions())
{
if (extension.Name == strFormatName)
{
info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
info.SetValue(extension, false);
}
}
}
and on the page_load:
DisableUnwantedExportFormat(ReportViewer1, "PDF");
only do this after Refresh, like this:
ReportViewer1.LocalReport.Refresh();
string exportOption = "PDF";
RenderingExtension extension = ReportViewer1.LocalReport.ListRenderingExtensions().ToList().Find(x => x.Name.Equals(exportOption, StringComparison.CurrentCultureIgnoreCase));
if (extension != null)
{
System.Reflection.FieldInfo fieldInfo = extension.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
fieldInfo.SetValue(extension, false);
}
see on this link...
https://www.aspsnippets.com/Articles/ASPNet-RDLC-Local-SSRS-Report-Viewer-Hide-Disable-specific-export-option-Word-Excel-PDF-from-Export-button.aspx
In code behind, load a hidden value when showing the report
this.ReportServViewer.ServerReport.Refresh();
this.hidReportViewing.Value = "algo";
then use the following javascript to set a timer to check for the export buttons to be rendered. When they are render, remove the button and clear the timer.
<script>
var intervalHandler;
var maxTries = 10;
var currentTries = 0;
function removePDFFromReporting() {
var clear = false;
if (intervalHandler != null) {
if ($('#hidReportViewing').val() != '') {
var anchor = $("#<%= ReportServViewer.ClientID%>_fixedTable a:contains('PDF')");
if (anchor.length == 0) {
currentTries = currentTries + 1;
clear = currentTries >= maxTries;
}
else {
anchor.remove();
clear = true;
}
}
}
if (clear) {
$('#hidReportViewing').val('');
clearInterval(intervalHandler);
intervalHandler = null;
}
}
</script>
in the on load add (ie $(document).ready())
if ($('#hidReportViewing').val() != '')
{
intervalHandler = setInterval(removePDFFromReporting, 1500);
}
This simple jQuery approach worked for me:
$(document).ready(function () {
$("a[title='PDF']").parent().hide(); // Remove from export dropdown.
$("a[title='MHTML (web archive)']").parent().hide();
$("a[title='TIFF file']").parent().hide();
});
Inspired by the answer https://stackoverflow.com/a/9192978/1099519 I created two extension methods.
In my case I use a whitelist approach by only enabling the formats I want (So you would need to include those you want except PDF):
reportViewer.ServerReport.SetExportFormats("EXCELOPENXML", "EXCEL", "XML", "CSV");
The Extension Methods look like this (supporting both Server- and LocalReports):
/// <summary>
/// Extension for ReportViewer Control
/// </summary>
public static class ReportViewerExtensions
{
private const string VisibleFieldName = "m_isVisible";
/// <summary>
/// Sets the supported formats on the <see cref="ServerReport"/>
/// </summary>
/// <param name="serverReport"><see cref="ServerReport"/> instance to set formats on</param>
/// <param name="formatNames">Supported formats</param>
public static void SetExportFormats(this ServerReport serverReport, params string[] formatNames)
{
SetExportFormats(serverReport.ListRenderingExtensions(), formatNames);
}
/// <summary>
/// Sets the supported formats on the <see cref="LocalReport"/>
/// </summary>
/// <param name="localReport"><see cref="LocalReport"/> instance to set formats on </param>
/// <param name="formatNames">Supported formats</param>
public static void SetExportFormats(this LocalReport localReport, params string[] formatNames)
{
SetExportFormats(localReport.ListRenderingExtensions(), formatNames);
}
/// <summary>
/// Setting the visibility on the <see cref="RenderingExtension"/>
/// </summary>
/// <param name="renderingExtensions">List of <see cref="RenderingExtension"/></param>
/// <param name="formatNames">A list of Formats that should be visible (Case Sensitive)</param>
private static void SetExportFormats(RenderingExtension[] renderingExtensions, string[] formatNames)
{
FieldInfo fieldInfo;
foreach (RenderingExtension extension in renderingExtensions)
{
if (!formatNames.Contains(extension.Name))
{
fieldInfo = extension.GetType().GetField(VisibleFieldName, BindingFlags.Instance | BindingFlags.NonPublic);
fieldInfo.SetValue(extension, false);
}
}
}
}