20
votes

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
You can also check my post on how to make Excel the first option in the dropdown: SSRS-Making Excel the default export optionaackose

18 Answers

30
votes

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");
11
votes

This is how you disable a export option, just mark all the ones except Excel to false.
*Don't forget to restart the Reporting Services service.

File: InstallPath\Reporting Services\ReportServer\rsreportserver.config

Enabled:

<Extension Name="EXCEL"
Type="Microsoft.ReportingServices.Rendering.ExcelRenderer.ExcelRenderer,Microsoft.ReportingServices.ExcelRendering"/>

Disabled:

<Extension Name="EXCEL"
Type="Microsoft.ReportingServices.Rendering.ExcelRenderer.ExcelRenderer,Microsoft.ReportingServices.ExcelRendering"
Visible="false"/>
10
votes

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();  
 });
5
votes

Using the jon's code above as a reference, I manage to hide "Excel" in the program at runtime. However, I am not good a VB.net so I put a sample in C#. Sorry about that but I hope this helps. One more thing, the report the embedded inside an ASP.net page.

  // This is the Load event of the reports itself.
  // Call the recursive method.
  protected void ReportViewerResults_Load(object sender, EventArgs e)
  {
    CustomizeRV((System.Web.UI.Control)sender);
  }

  // Patterned from Jon.
  // Traverse all controls/child controls to get the dropdownlist.
  // The first dropdown list is the ZoomGroup, followed by the ExportGroup.
  // We just wanted the ExportGroup.
  // When a dropdownlist is found, create a event handler to be used upon rendering.
  private void CustomizeRV(System.Web.UI.Control reportControl)
  {
    foreach (System.Web.UI.Control childControl in reportControl.Controls)
    {
      if (childControl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
      {
        System.Web.UI.WebControls.DropDownList ddList = (System.Web.UI.WebControls.DropDownList)childControl;
        ddList.PreRender += new EventHandler(ddList_PreRender);
      }
      if (childControl.Controls.Count > 0)
      {
        CustomizeRV(childControl);
      }
    }
  }

  // This is the event handler added from CustomizeRV
  // We just check the object type to get what we needed.
  // Once the dropdownlist is found, we check if it is for the ExportGroup.
  // Meaning, the "Excel" text should exists.
  // Then, just traverse the list and disable the "Excel".
  // When the report is shown, "Excel" will no longer be on the list.
  // You can also do this to "PDF" or if you want to change the text.
  void ddList_PreRender(object sender, EventArgs e)
  {
    if (sender.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
    {
      System.Web.UI.WebControls.DropDownList ddList = (System.Web.UI.WebControls.DropDownList)sender;
      System.Web.UI.WebControls.ListItemCollection listItems = ddList.Items;

      if ((listItems != null) && (listItems.Count > 0) && (listItems.FindByText("Excel") != null))
      {
        foreach (System.Web.UI.WebControls.ListItem list in listItems)
        {
          if (list.Text.Equals("Excel")) 
          {
            list.Enabled = false;
          }
        }
      }
    }
  }

I was trying to select the default item to "PDF" but could not find a way to enable the "Export" text button. :-(

3
votes

I had the same problem. I could get the unwanted export options to hide when the report rendered, but it didn't work in the case of a drillthrough report. The following code worked for both the parent and drillthrough reports, using a LocalReport:

    private void SuppressExportButton(ReportViewer rv, string optionToSuppress)
    {
        var reList = rv.LocalReport.ListRenderingExtensions();
        foreach (var re in reList)
        {
            if (re.Name.Trim().ToUpper() == optionToSuppress.Trim().ToUpper()) // Hide the option
            {
                re.GetType().GetField("m_isVisible", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(re, false);
            }
        }
    }

The trick is to call the method from the page PreRender method:

    protected void Page_PreRender(object sender, System.EventArgs e)
    {
        SuppressExportButton(rvMain, "PDF");
        SuppressExportButton(rvMain, "Word");
    }
2
votes

I managed to disable the PDF Export button with some tinkering. The ReportViewer class does not have any public facing functions to disable the Export to PDF toolbar button. In order to do it, take a look at the following code:

Call this function during the OnLoad event of your reportViewer page:

 Private Sub CustomizeRV(ByVal ctrl As Control)
    For Each c As Control In ctrl.Controls
      If TypeOf c Is ToolStrip Then
        Dim ts As ToolStrip = DirectCast(c, ToolStrip)
        For i As Integer = 0 To ts.Items.Count - 1
          If ts.Items(i).Name = "export" Then
            Dim exp As ToolStripDropDownButton = ts.Items(i)
            AddHandler exp.DropDownOpening, AddressOf disableButton
          End If
        Next
      End If
      If c.HasChildren Then
        CustomizeRV(c)
      End If
    Next
  End Sub

I couldn't set the Visible property of the toolstrip button here, since the Export options are loaded OnDropDownOpened. Instead, I added a handler to take care of disabling the export option when the toolstrip Dropdown is clicked. The handler function is as follows:

  Private Sub disableButton(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim btn As ToolStripDropDownButton = DirectCast(sender, ToolStripDropDownButton)
    btn.DropDownItems(1).Visible = False
  End Sub

So basically, Onload you are adding an Event Handler so that when the Export Drop Down button is clicked, the above function will run - making the Export to PDF invisible.

The solution will work for sure, I just finished making it work.

If you have any questions, let me know.

2
votes
public void DisableUnwantedExportFormats()
{
    FieldInfo info;

    foreach (RenderingExtension extension in reportViewer.ServerReport.ListRenderingExtensions())
    {
        if (extension.Name != "PDF" && extension.Name != "EXCEL") // only PDF and Excel - remove the other options
        {
            info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
            info.SetValue(extension, false);
        }

        if (extension.Name == "EXCEL") // change "Excel" name on the list to "Excel 97-2003 Workbook"
        {
            info = extension.GetType().GetField("m_localizedName", BindingFlags.Instance | BindingFlags.NonPublic);
            if (info != null) info.SetValue(extension, "Excel 97-2003 Workbook");
        }
    }
}

I've tried by adding above said method DisableUnwantedExportFormats() for hiding Export to Excel option. When report loaded first time the Excel option not getting visible.

However, When I used to call the same method inside of Drillthrough() event "Excel" & PDF option getting visible in Export controls dropdown. I've tried by calling your method in the first statement of my Drillthrough() event (like what I used in Page load method).

Please let me know, How can I hide the excel option in Drillthrough() event of Reportviewer.

2
votes

I've managed to do this on the client side using JavaScript at the bottom of the page.

var exportSelectBox = document.getElementById("ReportViewer1__ctl1__ctl5__ctl0");
exportSelectBox.remove(7);
exportSelectBox.remove(6);
exportSelectBox.remove(5);
exportSelectBox.remove(4);
exportSelectBox.remove(1);
exportSelectBox.remove(1);
2
votes

Jquery solution for reportviewer 2010: Put this in the aspx file containing the reportviewer control (Assuming your reportviewer is called ReportViewer1)

<script type="text/javascript">
    $(document).ready(function () {
        hideExportOptions();
    });

    function hideExportOptions() {
        //Find the menu id by getting the parent of the parent of one of the export links
        var menuID = $("a[onclick=\"$find('ReportViewer1').exportReport('PDF');\"]").parent().parent().attr("id");
        if ($("#" + menuID).length > 0) {
            $("#" + menuID  + " div:nth-child(3)").css('display', 'none');
        }
        else {
            setTimeout("hideExportOptions()", 1000);
        }
    }

</script> 

It waits till the dropdown is rendered, then hides the chosen option. Typically the setTimeout only occurs only once. You can hide more/others by adding more nth-childs, the number being the 1-based position in the dropdown of the option you want to hide.

2
votes
  1. To Word option reference to "WORDOPENXML"
  2. To Excel option reference to "EXCELOPENXML"
  3. To PDF option reference to "PDF"

Regards.

2
votes

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

1
votes

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

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

1
votes

After 4 hours of search I found the solution. I made some little changes to marol's code to be more little:

        Control ReportViewerControl = ReportViewer1.FindControl("Ctl01");
        Control ExportGroupControl = ReportViewerControl.FindControl("Ctl05");
        DropDownList DropDownControl = (DropDownList)ExportGroupControl.FindControl("Ctl00");
        DropDownControl.PreRender += new EventHandler(ddList_PreRender);
1
votes

For ReportViewer >2010 i use this aproach made with jQuery

function HideExtension(ext) {
        var $reportViewer = $("[id*=ReportViewer1]");
        var $botons = $reportViewer.find("a");
        $botons.each(function (index,element) {
            if($(element).html()==ext)
            {
                $(element).parent().css("display", "none");
            }
        });
    }

Just change the selector for your own and call the function from $(document).ready(function(){//here})

1
votes

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

        }
    }
}
0
votes

If you're interested in a quick javascript solution using jQuery ..

Simply replace the reportViewer selector below with your dropdown ID.

jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').children().remove(); jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').append("- Select export format -"); jQuery('#ctl00_ContentPlaceHolder1_rptViewer_ctl01_ctl05_ctl00').append("EXCEL");

This removes all options and then adds back in EXCEL as the only option.

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