ReportViewer - Hide PDF Export
Asked Answered
A

18

21

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?

Affine answered 30/9, 2009 at 18:30 Comment(1)
You can also check my post on how to make Excel the first option in the dropdown: SSRS-Making Excel the default export optionRavioli
B
30

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");
Baugh answered 8/2, 2012 at 12:5 Comment(4)
Hello I used your and it work with PDF and WORD only. How can I try it with Excel??Sophistication
Ooops I figured it out. It is case sensitive it must be "Excel". Thanks for your code. ^^Sophistication
Also works for reports processed server side; just change .LocalReport to .ServerReport (tested on SSRS 2016).Handstand
Nice piece of code, used it for server side reports, very useful and easy to implement.Geostatics
C
13

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"/>
Cymbal answered 30/9, 2009 at 20:22 Comment(0)
F
11

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();  
 });
Forfeiture answered 12/4, 2013 at 16:56 Comment(2)
Worked like a charm. Simple and no messing about!Gel
Simple and easy way without modifying config file.Solifluction
M
5

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. :-(

Makassar answered 13/10, 2009 at 8:1 Comment(0)
S
3

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");
    }
Surprise answered 24/8, 2012 at 19:9 Comment(0)
F
2

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.

Freiman answered 30/9, 2009 at 19:23 Comment(0)
S
2
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.

Spearmint answered 3/6, 2011 at 11:14 Comment(0)
R
2

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.

Rosebay answered 23/10, 2012 at 8:9 Comment(0)
Y
2

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);
Yep answered 23/10, 2012 at 15:28 Comment(0)
T
2
  1. To Word option reference to "WORDOPENXML"
  2. To Excel option reference to "EXCELOPENXML"
  3. To PDF option reference to "PDF"

Regards.

Taejon answered 24/1, 2014 at 0:30 Comment(1)
This answer should have some surrounding description added to it. It's not really clear.Pash
S
2

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

Sandman answered 13/8, 2018 at 22:34 Comment(0)
G
1

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);
Gothurd answered 10/11, 2010 at 10:24 Comment(0)
R
1

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

Roo answered 6/1, 2011 at 5:12 Comment(0)
S
1

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);
 }
Shipwreck answered 3/8, 2012 at 13:52 Comment(0)
O
1

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

Ory answered 4/6, 2015 at 19:40 Comment(0)
E
1

Inspired by the answer https://mcmap.net/q/593179/-reportviewer-hide-pdf-export 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);
            }

        }
    }
}
Ensign answered 2/8, 2018 at 6:56 Comment(0)
G
0

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.

Georas answered 1/11, 2011 at 1:41 Comment(0)
L
0
    //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();
        });
Lout answered 27/6, 2020 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.