ReportViewer - Hide PDF Export

Viewed 50390

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?

18 Answers

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

If it helps... the code to hide the excel item em VB.Net (.Net 3.5)

Private Sub CustomizeRV(ByVal ctrl As ReportViewer)

    For Each c As Control In ctrl.Controls

        If c.GetType.ToString = "Microsoft.Reporting.WebForms.ToolbarControl" Then

            For Each ct In c.Controls

                If ct.GetType.ToString = "Microsoft.Reporting.WebForms.ExportGroup" Then

                    Dim cbo As DropDownList = CType(ct.controls(0), DropDownList)

                    AddHandler cbo.PreRender, AddressOf cboExportReportViewer_PreRender

                End If

            Next

        End If

    Next

End Sub

Protected Sub cboExportReportViewer_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim cbo = CType(sender, DropDownList)

    For i As Integer = 0 To cbo.Items.Count - 1

        If cbo.Items(i).Text.ToLower = "excel" Then
            cbo.Items.Remove(cbo.Items(i))
            Exit Sub
        End If

    Next

End Sub

... and put a call to CustomizeRV(ReportViewer1) in the page_load event

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);
            }

        }
    }
}
    //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();
        });
Related