Solved

Print Crystal Report programmatically in ASP.NET

Posted on 2011-03-18
11
2,357 Views
Last Modified: 2012-05-11
Hi,

i created a Report in with Crystal Reports and would like to Print the report without opening it in the viewer.
When a button is clicked, the user should see the Print Dialog and print the report!

Is that possible?

Thanks
0
Comment
Question by:Sebastian_OH
  • 4
  • 4
  • 3
11 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35168487
xxx.Print();
where xxx is the crystal report viewer - use instead of the command to show the viewer
0
 
LVL 100

Accepted Solution

by:
mlmcc earned 500 total points
ID: 35168559
Another way is to use the Report Object PrintToPrinter method

mlmcc
0
 
LVL 3

Author Comment

by:Sebastian_OH
ID: 35169001
Will I be able to choose the printer with the PrintToPrinter method ?
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35169414
It will print to the default printer on the machine.

mlmcc
0
 
LVL 3

Author Comment

by:Sebastian_OH
ID: 35169829
I would like to allow the user to choose the printer
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 100

Expert Comment

by:mlmcc
ID: 35169902
From the Crystal API Help

[C#]

      private void PrintReport(string printerName)
      {
         PageMargins margins;
     
         margins = Report.PrintOptions.PageMargins;
         margins.bottomMargin = 350;
         margins.leftMargin = 350;
         margins.rightMargin = 350;
         margins.topMargin = 350;
         Report.PrintOptions.ApplyPageMargins(margins);
     
         Report.PrintOptions.PrinterName = printerName;
     
         Report.PrintToPrinter(1, false,0,0);
      }

mlmcc
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35170412
I've just checked some code.
My first comment should be
xxx.PrintReport() instead of xxx.Print()
That allows the user to select a printer...
0
 
LVL 3

Author Comment

by:Sebastian_OH
ID: 35171770
I just tried to use the PrintReport() Method. The method does not exist for me.

Which version of CR is it? I use CR 13 for VS2010.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35172298
The viewer (crystal reports for VS 2005) should have a PrintReport function.  Here is a snippet, see the final line

namespace CrystalDecisions.Windows.Forms
{
    // Summary:
    //     Provides properties, methods, and events that give control over how the viewer
    //     appears and functions.
    [ToolboxItemFilter("System.Windows.Forms")]
    [ToolboxBitmap(typeof(CrystalReportViewer))]
    [Designer("CrystalDecisions.VSDesigner.Windows.Forms.ReportViewerDesigner, CrystalDecisions.VSDesigner, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304")]
    [DefaultProperty("ReportSource")]
    [Docking(2)]
    public class CrystalReportViewer : UserControl, ICrystalReportViewer
    {
        public CrystalReportViewer();

        // Summary:
        //     Gets or sets the index of the active view in the viewer.
        [Browsable(false)]
        public int ActiveViewIndex { get; set; }
        //
        // Summary:
        //     Gets or sets the background color for the control.
        [Browsable(false)]
        [Category("Appearance")]
        [Localizable(true)]
        public override Color BackColor { get; set; }
        //
        // Summary:
        //     Gets or sets the background image displayed in the control.
        [Localizable(true)]
        [Browsable(false)]
        [Category("Appearance")]
        public override Image BackgroundImage { get; set; }
        //
        // Summary:
        //     Gets or sets the background image layout as defined in the System.Windows.Forms.ImageLayout
        //     enumeration.
        [Category("Appearance")]
        [Localizable(true)]
        [Browsable(false)]
        public override ImageLayout BackgroundImageLayout { get; set; }
        [DesignOnly(true)]
        [DesignerSerializationVisibility(0)]
        [Browsable(false)]
        [EditorBrowsable(1)]
        public string DesignTimeWinFormDirectory { get; set; }
        //
        // Summary:
        //     Determines whether the report is offset from the edge of its view window.
        [DefaultValue(true)]
        [ViewerSysDescription("IDS_DISPLAY_BACKGROUNDEDGE_DESCRIPTION")]
        [Localizable(true)]
        [Category("Appearance")]
        public bool DisplayBackgroundEdge { get; set; }
        //
        // Summary:
        //     Gets or sets whether the tree view is visible or hidden.
        [Category("Appearance")]
        [ViewerSysDescription("IDS_DISPLAY_GROUPTREE_DESCRIPTION")]
        [Localizable(true)]
        [DefaultValue(true)]
        public bool DisplayGroupTree { get; set; }
        //
        // Summary:
        //     Boolean. Gets or set whether the status bar is visible or hidden.
        [ViewerSysDescription("IDS_ENABLE_STATUSBAR_DESCRIPTION")]
        [Category("Appearance")]
        [DefaultValue(true)]
        public bool DisplayStatusBar { get; set; }
        //
        // Summary:
        //     Gets or sets whether the toolbar is visible or hidden.
        [Category("Appearance")]
        [DefaultValue(true)]
        [ViewerSysDescription("IDS_DISPLAY_TOOLBAR_DESCRIPTION")]
        [Localizable(true)]
        public bool DisplayToolbar { get; set; }
        //
        // Summary:
        //     Gets or sets whether the user can drill down into the report.
        [Localizable(true)]
        [ViewerSysDescription("IDS_ENABLE_DRILLDOWN_DESCRIPTION")]
        [DefaultValue(true)]
        public bool EnableDrillDown { get; set; }
        //
        // Summary:
        //     Boolean. Gets or Sets whether tool tips are displayed in the report viewer.
        [DefaultValue(true)]
        [Category("Appearance")]
        [ViewerSysDescription("IDS_ENABLE_TOOLTIP_DESCRIPTION")]
        public bool EnableToolTips { get; set; }
        //
        // Summary:
        //     Gets or sets the logon information for an enterprise report.
        [DesignerSerializationVisibility(0)]
        [Browsable(false)]
        [Bindable(false)]
        public object EnterpriseLogon { get; set; }
        //
        // Summary:
        //     Gets or sets the TableLogOnInfos collection.
        [DesignerSerializationVisibility(0)]
        [Browsable(false)]
        [DefaultValue("")]
        public TableLogOnInfos LogOnInfo { get; set; }
        //
        // Summary:
        //     Gets or sets the parameter fields collection.
        [Browsable(false)]
        [DefaultValue("")]
        [DesignerSerializationVisibility(0)]
        public ParameterFields ParameterFieldInfo { get; set; }
        //
        // Summary:
        //     Gets or sets the report to bind to the viewer.
        [DefaultValue("")]
        [Browsable(true)]
        [Category("Data")]
        [Editor("CrystalDecisions.ReportSource.ReportSourceEditor, CrystalDecisions.ReportSource, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304", typeof(UITypeEditor))]
        [ViewerSysDescription("IDS_REPORT_SOURCE_DESCRIPTION")]
        [TypeConverter("CrystalDecisions.ReportSource.ReportSourceConverter, CrystalDecisions.ReportSource, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304")]
        public object ReportSource { get; set; }
        //
        // Summary:
        //     Gets or sets the information needed to create an internal Reportsource object.
        [Browsable(false)]
        [DesignerSerializationVisibility(0)]
        [Bindable(false)]
        public string ReportSourceClassFactoryName { get; set; }
        //
        // Summary:
        //     Gets or sets whether the text appears from right to left, such as when using
        //     Hebrew or Arabic fonts.
        [Category("Appearance")]
        [Localizable(true)]
        [Browsable(false)]
        public override RightToLeft RightToLeft { get; set; }
        //
        // Summary:
        //     Gets or sets the record selection formula of the report.
        [Category("Data")]
        [DefaultValue("")]
        [ViewerSysDescription("IDS_SELECTION_FORMULA_DESCRIPTION")]
        public string SelectionFormula { get; set; }
        //
        // Summary:
        //     Gets or sets whether the viewer toolbar has the button for closing a report
        //     page.
        [DefaultValue(true)]
        [Localizable(true)]
        [Category("Appearance")]
        [ViewerSysDescription("IDS_SHOW_CLOSEBUTTON_DESCRIPTION")]
        public bool ShowCloseButton { get; set; }
        //
        // Summary:
        //     Gets or sets whether the viewer toolbar has the button for exporting.
        [Category("Appearance")]
        [Localizable(true)]
        [DefaultValue(true)]
        [ViewerSysDescription("IDS_SHOW_EXPORTBUTTON_DESCRIPTION")]
        public bool ShowExportButton { get; set; }
        //
        // Summary:
        //     Gets or sets whether the viewer toolbar has the button for specifying page
        //     number.
        [ViewerSysDescription("IDS_SHOW_GOTOPAGEBUTTON_DESCRIPTION")]
        [Localizable(true)]
        [DefaultValue(true)]
        [Category("Appearance")]
        public bool ShowGotoPageButton { get; set; }
        //
        // Summary:
        //     Gets whether the viewer toolbar has the button for showing or hiding the
        //     group tree.
        [ViewerSysDescription("IDS_SHOW_GROUPTREEBUTTON_DESCRIPTION")]
        [Category("Appearance")]
        [Localizable(true)]
        [DefaultValue(true)]
        public bool ShowGroupTreeButton { get; set; }
        //
        // Summary:
        //     Boolean. Gets or sets whether the CrystalReportViewer control toolbar has
        //     the buttons for page navigation.
        [ViewerSysDescription("IDS_SHOW_PAGENAVIGATEBUTTONS_DESCRIPTION")]
        [DefaultValue(true)]
        [Category("Appearance")]
        [Localizable(true)]
        public bool ShowPageNavigateButtons { get; set; }
        //
        // Summary:
        //     Gets or sets whether the print button on the toolbar is visible or hidden.
        [Category("Appearance")]
        [Localizable(true)]
        [DefaultValue(true)]
        [ViewerSysDescription("IDS_SHOW_PRINTBUTTON_DESCRIPTION")]
        public bool ShowPrintButton { get; set; }
        //
        // Summary:
        //     Gets or sets whether the refresh button on the toolbar is visible or hidden.
        [ViewerSysDescription("IDS_SHOW_REFRESHBUTTON_DESCRIPTION")]
        [Localizable(true)]
        [DefaultValue(true)]
        [Category("Appearance")]
        public bool ShowRefreshButton { get; set; }
        //
        // Summary:
        //     Gets or sets whether the search button on the toolbar is visible or hidden.
        [Category("Appearance")]
        [Localizable(true)]
        [DefaultValue(true)]
        [ViewerSysDescription("IDS_SHOW_TEXTSEARCHBUTTON_DESCRIPTION")]
        public bool ShowTextSearchButton { get; set; }
        //
        // Summary:
        //     Gets or sets whether the zoom factor list on the toolbar is visible or hidden.
        [ViewerSysDescription("IDS_SHOW_ZOOMBUTTON_DESCRIPTION")]
        [Category("Appearance")]
        [Localizable(true)]
        [DefaultValue(true)]
        public bool ShowZoomButton { get; set; }
        //
        // Summary:
        //     Gets the number of views in the viewer.
        [Browsable(false)]
        public int ViewCount { get; }
        //
        // Summary:
        //     String. Gets or sets the record selection formula to filter the data that
        //     are currently used in the report.
        [Category("Data")]
        [DefaultValue("")]
        [ViewerSysDescription("IDS_VIEWTIME_SELECTION_FORMULA_DESCRIPTION")]
        public string ViewTimeSelectionFormula { get; set; }

        // Summary:
        //     Event fires when a report is drilled down on.
        [ViewerSysDescription("ID_OnDrillDescr")]
        [Category("Action")]
        public event DrillEventHandler Drill;
        //
        // Summary:
        //     Event fires when the user drills down on a subreport.
        [Category("Action")]
        [ViewerSysDescription("ID_OnSubreportDrillDownDescr")]
        public event DrillSubreportEventHandler DrillDownSubreport;
        [Category("Action")]
        [ViewerSysDescription("ID_OnHandleExceptionDescr")]
        public event ExceptionEventHandler Error;
        //
        // Summary:
        //     Event fires when an exception occurs in the CrystalReportViewer control.
        [EditorBrowsable(1)]
        [Browsable(false)]
        [ViewerSysDescription("ID_OnHandleExceptionDescr")]
        [Category("Action")]
        public event ExceptionEventHandler HandleException;
        //
        // Summary:
        //     Event fires when the user navigates through a report.
        [Category("Action")]
        [ViewerSysDescription("ID_OnPageNavigateDescr")]
        public event NavigateEventHandler Navigate;
        //
        // Summary:
        //     Event fires when the data in the report is refreshed.
        [Category("Action")]
        [ViewerSysDescription("ID_OnRefreshDescr")]
        public event RefreshEventHandler ReportRefresh;
        //
        // Summary:
        //     Event fires when text is searched for in the report.
        [ViewerSysDescription("ID_OnSearchDescr")]
        [Category("Action")]
        public event SearchEventHandler Search;
        //
        // Summary:
        //     Event fires when zoom level of the CrystalReportViewer control changes.
        [ViewerSysDescription("ID_OnZoomDescr")]
        [Category("Action")]
        public event ZoomEventHandler ViewZoom;

        // Summary:
        //     Closes a view tab in the viewer.
        //
        // Parameters:
        //   viewURI:
        //     Indicates a string value that contains the name of the view tab.
        public virtual void CloseView(object viewURI);
        protected override void Dispose(bool disposeManaged);
        //
        // Summary:
        //     Drills down on a group.
        //
        // Parameters:
        //   groupInfo:
        //     The group to drill down on.
        public void DrillDownOnGroup(TotallerNodeID groupInfo);
        //
        // Summary:
        //     Exports the report displayed in the viewer.
        public virtual void ExportReport();
        //
        // Summary:
        //     Gets the current page number of the report.
        //
        // Returns:
        //     Returns the page number that is currently displayed in the CrystalReportViewer.
        public virtual int GetCurrentPageNumber();
        public virtual void InitReportViewer();
        protected override void OnCreateControl();
        protected override void OnPaint(PaintEventArgs evtArgs);
        //
        // Summary:
        //     Prints the report displayed in the viewer.
        public virtual void PrintReport();
0
 
LVL 3

Author Comment

by:Sebastian_OH
ID: 35172367
attached u see what i have in CR13 CR13 Print Options
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35172462
PrintToPrinter will be on the report object.

mlmcc
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now