Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi devs, my problem is that i can print a multiple page but what i'm gonna do is that i will download it as pdf first and print it all, i did a one click button using javascript but i can't do a multiple page here's my code below.

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Session["pager"] = ((DataTable)Session["table"]).Rows.Count;
                rptDoc.Load(Server.MapPath(Session["path"].ToString()));
                string aaa = Session["path"].ToString();
                rptDoc.SetDataSource((DataTable)Session["table"]);
                reportViewer.ReportSource = rptDoc;
                reportViewer.RefreshReport();
                if (!IsPostBack)
                {
                    Session["index"] = 1;
                    lblback.Text = Session["back"].ToString();
                }
            }
            catch (Exception ex)
            {
                error.ErrorLogs(Session["UserName"].ToString(), url, ex);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('" + ConString.ErrorMessage() + "')", true);
            }
        }


<input id="btnPrint" type="button" value="Print" onclick="Print()" />



<div id="dvReport">  
                <CR:CrystalReportViewer ID="reportViewer" runat="server" AutoDataBind="true"
                    BestFitPage="False" EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" PrintMode="ActiveX" HasGotoPageButton="False" HasPageNavigationButtons="False" />
            </div> 



<script type="text/javascript">
    function Print() {
            var dvReport = document.getElementById("dvReport");
            var frame1 = dvReport.getElementsByTagName("iframe")[0];
            if (navigator.appName.indexOf("Internet Explorer") != -1 || navigator.appVersion.indexOf("Trident") != -1) {
                frame1.name = frame1.id;
                window.frames[frame1.id].focus();
                window.frames[frame1.id].print();
                
            }
            else {
                var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
                frameDoc.print();
            }
        }
</script>


What I have tried:

my original code is this.
protected void btnPrint_Click(object sender, EventArgs e)
       {
           Session["pager"] = ((DataTable)Session["table"]).Rows.Count;
           rptDoc.Load(Server.MapPath(Session["path"].ToString()));
           string aaa = Session["path"].ToString();
           rptDoc.SetDataSource((DataTable)Session["table"]);
           reportViewer.ReportSource = rptDoc;
           reportViewer.RefreshReport();

           PrinterSettings printerSettings = new PrinterSettings();
           PrintDocument localPrinter = new PrintDocument();
           printerSettings.PrinterName = localPrinter.PrinterSettings.PrinterName;
           rptDoc.PrintToPrinter(printerSettings, new PageSettings(), false);
       }


i change it to javascript because of some printer problem on my side i can use this and print a multiple page but whenever i launched this an the client tried to used my website
PrintDocument localPrinter = new PrintDocument();
this is throwing an error that i can't detect the client print but when i tried it work's perfectly that's why i tried javascript.
Posted
Updated 1-Dec-19 18:42pm
v2
Comments
Richard Deeming 3-Dec-19 8:54am    
Your original code won't work because it's running on the server. It would only be able to print to a printer connected to your server and installed for the AppPool user. Unless this is an intranet application, with the server and the users all in the same office, that's not going to do what you want.

Printing from Javascript is notoriously difficult. Judging by the PrintMode="ActiveX", the Crystal Reports viewer is using a solution which will only work in Internet Explorer on Windows, and only if the user has allowed your site to "initialize and script ActiveX controls not marked as safe for scripting".

Your best bet is probably to export the report to PDF and let the user download it and print it themselves.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900