1

I have win form app that have some info on date base when the user click on a button it will open the report Viewer and the user should save the file

as you know the report viewer will let the user save the file as PDF , WORD AND EXCEL .

All I want is to let the user only see and press the PDF save.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Lubbock
  • 63
  • 2
  • 13
  • Possible duplicate of [SSRS - Disabling export options (eg. PDF) for individual reports](https://stackoverflow.com/questions/15831328/ssrs-disabling-export-options-eg-pdf-for-individual-reports) – Cee McSharpface Jan 14 '18 at 18:35
  • @dlatikay no that's not work – Lubbock Jan 14 '18 at 18:43
  • it is not quite clear if you're talking about the crystal report component, or the microsoft data report designer. I tagged as the latter, but I may be wrong. please let us know or at least include relevant parts of your code. – Cee McSharpface Jan 14 '18 at 19:06
  • @dlatikay it's report Viewer of c# windows form application sorry i don't have any code for now i'm tying to use this tool for my project , he need a pdf file from the data-base any ideas? – Lubbock Jan 14 '18 at 19:21
  • do users need the preview? if not, you can [directly generate the file](https://stackoverflow.com/a/2686205/1132334) without opening the report viewer. – Cee McSharpface Jan 14 '18 at 19:27
  • no he don't how can i create pdf and get the data from database ? – Lubbock Jan 14 '18 at 19:36
  • that's too broad for a SO question. especially if there is no existing code that we can improve or fix together. look for samples and documentation like [this one](https://learn.microsoft.com/en-us/sql/reporting-services/application-integration/using-the-winforms-reportviewer-control) – Cee McSharpface Jan 14 '18 at 19:44
  • Do you mean RDLC Report Viewer? – Reza Aghaei Jan 15 '18 at 06:12
  • @RezaAghaei sorry i'm really sorry i don't know what is the different between rdlc and rdl so i google it and i found i need rdlc i'm useing visual studio – Lubbock Jan 16 '18 at 22:48
  • No problem, So the answer which I posted is definitely what you need. – Reza Aghaei Jan 17 '18 at 02:51
  • thanks a lot it's working @RezaAghaei – Lubbock Jan 19 '18 at 14:20

3 Answers3

1

First, you can disable the export using the ShowExportControls property.

Then, you'll add a custom button to the toolstrip. This will be done in your form load event.

ToolStrip ts = (ToolStrip)crystalReportViewer1.Controls[3]; 
ToolStripButton printbutton = new ToolStripButton(); 
printbutton.Image = ts.Items[1].Image; 
ts.Items.Remove(ts.Items[1]); 
ts.Items.Insert(1, printbutton); 
ts.Items[1].Click += new EventHandler(this.CaptureEvent);                   
cr = new CrystalReport1(); 
this.crystalReportViewer1.ReportSource = cr; 

Then you'll have the custom event handler for that button

private void CaptureEvent(Object Sender, EventArgs e) 
{
    // In this code, you'll have your own custom save file dialog
    // Once you get that file name, save to PDF
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
    saveFileDialog.Filter = "Document (*.pdf)|*.PDF";
    saveFileDialog.FilterIndex = 1;
    if (saveFileDialog.ShowDialog() == DialogResult.OK)
    { 
        crystalReportViewer1.ExportToDisk(ExportFormatType.PortableDocFormat, saveFileDialog.FileName);;
    }
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
1

To change the ToolStrip appearance to show just the Save button without drop-down, you can find the ToolStrip of the ReportViewer and then find the "export" button and remove the dropdown.

To show the save dialog to just allow saving PDF, attach an event handler to click event of the "export" button and show the save dialog using ExportDialog method of the report viewer. You can find the PDF extension between extensions which return by ListRenderingExtensions() method of the LocalReport and pass it to ExportDialog method to limit the dialog to just show PDF extension.

Example 1

Paste this code in load event of your form and after loading the report, press save button. It will show a save dialog containing just PDF option for saving file:

var toolStrip = (ToolStrip)reportViewer1.Controls.Find("toolStrip1", true).First();
((ToolStripDropDownButton)toolStrip.Items["export"]).ShowDropDownArrow = false;
((ToolStripDropDownButton)toolStrip.Items["export"]).DropDownOpening += (obj, arg) =>
{
    ((ToolStripDropDownButton)obj).DropDownItems.Clear();
};
((ToolStripDropDownButton)toolStrip.Items["export"]).Click += (obj, arg) =>
{
    var pdf = reportViewer1.LocalReport.ListRenderingExtensions()
        .Where(x => x.Name == "PDF").First();
    reportViewer1.ExportDialog(pdf);
};

Example 2

If you don't want to remove the dropdown arrow and just want it to show PDF option in drop down, use this code:

var toolStrip = (ToolStrip)reportViewer1.Controls.Find("toolStrip1", true).First();
((ToolStripDropDownButton)toolStrip.Items["export"]).DropDownOpening += (obj, arg) =>
{
    var item = ((ToolStripDropDownButton)obj);
    item.DropDownItems.Cast<ToolStripItem>().Where(x => x.Text != "PDF").ToList()
        .ForEach(x => item.DropDownItems.Remove(x));
};
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I've tried both of the solutions without any problem. Let me know if you faced a problem applying the solution ... – Reza Aghaei Jul 03 '18 at 13:43
-1

(seems I do not have enough reputatiuon to add a comment)

I used the suggestion provided by Reza Aghaei. However the contextmenu of the reportview also provides an Export Dropdown, which is untouched by Reza's suggestion. I disabled the ReportViewer ConcextMenu to solve this issue.

ShowContextMenu = false

Marko
  • 39
  • 3