• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2476
  • Last Modified:

Print Crystal Report programmatically in ASP.NET

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
Sebastian_OH
Asked:
Sebastian_OH
  • 4
  • 4
  • 3
1 Solution
 
AndyAinscowCommented:
xxx.Print();
where xxx is the crystal report viewer - use instead of the command to show the viewer
0
 
mlmccCommented:
Another way is to use the Report Object PrintToPrinter method

mlmcc
0
 
Sebastian_OHAuthor Commented:
Will I be able to choose the printer with the PrintToPrinter method ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mlmccCommented:
It will print to the default printer on the machine.

mlmcc
0
 
Sebastian_OHAuthor Commented:
I would like to allow the user to choose the printer
0
 
mlmccCommented:
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
 
AndyAinscowCommented:
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
 
Sebastian_OHAuthor Commented:
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
 
AndyAinscowCommented:
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
 
Sebastian_OHAuthor Commented:
attached u see what i have in CR13 CR13 Print Options
0
 
mlmccCommented:
PrintToPrinter will be on the report object.

mlmcc
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now