Click here to Skip to main content
15,882,315 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Print.aspx.cs
C#
public static void PrintWebControl(Control ctrl)
{
    PrintWebControl(ctrl, string.Empty);
}

public static void PrintWebControl(Control ctrl, string Script)
{
    StringWriter stringWrite = new StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
    if (ctrl is WebControl)
    {
        Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
    }
    Page pg = new Page();
    pg.EnableEventValidation = false;
    if (Script != string.Empty)
    {
        pg.ClientScript.RegisterStartupScript(pg.GetType(),"PrintJavaScript", Script);
    }
    HtmlForm frm = new HtmlForm();
    pg.Controls.Add(frm);
    frm.Attributes.Add("runat", "server");
    frm.Controls.Add(ctrl);
    pg.DesignerInitialize();
    pg.RenderControl(htmlWrite);
    string strHTML = stringWrite.ToString();


    PaperSize p = null;
    PrintDocument pd = new PrintDocument();


    pd.PrinterSettings.DefaultPageSettings.Landscape = true;
    pd.DefaultPageSettings.PaperSize = p;

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Write(@"<link href='/Style1.css' rel='stylesheet' type='text/css' />");
    HttpContext.Current.Response.Write(strHTML);
    HttpContext.Current.Response.Write("<script>window.print();</script>");
    HttpContext.Current.Response.End();
}
protected void Print_Button_Click(object sender, EventArgs e)
{
    PrintInvoice.PrintWebControl(Panel1);
}



[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v2
Comments
Abinash_Sahoo 24-Apr-14 9:57am    
Did you check whether the relative path of the image is correct in the final strHTML? Before printing the page, try to show it and check whether it's displaying the image.
Ank001 24-Apr-14 11:23am    
Where should I check the path of image in strHTML?
Sunasara Imdadhusen 8-May-14 6:12am    
Does image is displayed on screen?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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