Print Items Web Page

Asked

Viewed 107 times

0

I would like to know how to print a web page, only I would not like to print the whole page, I would like you to print the title, and some grids, which are formatted (it would be necessary to print all Gridview data without paging).

Unfortunately of all the ways I try, I did not print the grid without formatting. The paging is done in the same Gridview control.

Does anyone know any way I can print the grid with all the data?

  • 1

    It’s ASP.NET or Winforms?

  • is asp.net webForms, the tag was wrong, I’ll change it. Thank you.

3 answers

1

I don’t know about asp, so maybe what I’m gonna tell you here doesn’t work very well.

In CSS we have media queries, which are basically "modes" your page is in. Example:

@media screen and (max-width: 600px){
   body{
        width: 100%
   }
}

That is, when the device is screen (screen) and is at most 600px wide, apply 100% width on the body.

With this we also have some options that deal about printing (print). Then you can use them as follows:

@media print{
    #my-grid{
        width: 1000px;
        height: 600px;
        top: 0;
        left: 0;
    }
}

That is, when you try to print by browser, the element #my-grid will receive these new values and replace the old ones.

One way to test if your style is being applied correctly is by using Devtool.

In Chrome, open the console (F12), press ESC and scroll to the option Emulate CSS Media. There you can select print (printing) and check how your element looks when it will be printed. inserir a descrição da imagem aqui

  • Good morning Raul, I tried to do as reported with the title to appear configured, I did it this way: @media print { #lblTitulo { text-align: center; font-size: 30px; font-Weight: 200; color: #33cc00; margin: 0 auto; margin-top: 130px; margin-bottom: 60px; width: 100%; } } But it didn’t work, do you have any idea why it’s not working ? Thank you.

0

I managed to settle by doing this way:

GridSuprimento.DataSource = gridCarregaSuprimento();
            GridSuprimento.AllowPaging = false;
            GridSuprimento.DataBind();
            GridRetirada.DataSource = gridCarregaRetirada();
            GridRetirada.AllowPaging = false;
            GridRetirada.DataBind();
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridSuprimento.RenderControl(hw);
            GridRetirada.RenderControl(hw);
            string gridHTML = sw.ToString().Replace("\"", "'").Replace(System.Environment.NewLine, "");
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append("window.onload = new function(){");
            sb.Append("var printWin = window.open('', '', 'left=0");
            sb.Append(",top=0,width=1000,height=600,status=0');");
            sb.Append("printWin.document.write(\"");
            sb.Append(lblTitulo.Text);
            sb.Append(gridHTML);
            sb.Append("\");");
            sb.Append("printWin.document.close();");
            sb.Append("printWin.focus();");
            sb.Append("printWin.print();");
            sb.Append("printWin.close();};");
            sb.Append("</script>");
            ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());

It appears the Loaded Grid and the titles, but without configuration, I need to know how to configure the titles, I still could not solve this part.

0


I managed to solve the problem by making the configuration this way:

sb.Append("<div ID='imprimir' align='Center' style='font-size:25px;'>Relatório de Planos</div>");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.