Report generated with reportview, directly in pdf

Asked

Viewed 1,003 times

1

I have a form, and in this form, I have a button print out which opens a reportviwer report made with a dataset. What I need to do with this report is automate it, instead of me clicking the print button, it open the reportviwer screen later and have to save in PDF to then open the pdf.

I would like to know if it is possible and if yes how to shorten this whole way and click on the print button, and it already open the report of reportview already in pdf, triggering the pdf program of the usurer’s machine to view, that is when you click the print button already view in pdf.

Follow the code on the print button that calls reportviwer.

public partial class frmPedioVenda : Form
    {

        public frmPedioVenda()
        {
            InitializeComponent();
        }
        private void frmPedioVenda_Load(object sender, EventArgs e)
        {

        }
        private void bntPesquisa_Click(object sender, EventArgs e)
        {
            this.PedidoVendaPHTableAdapter.Fill_ph(this.PedidoVendaDataSet1.PedidoVendaPH, txtPedido.Text);
            this.reportViewer1.RefreshReport();
        } 

Here I already create the report and visualize:

private void bntPesquisa_Click(object sender, EventArgs e)
{
    this.PedidoVendaPHTableAdapter.Fill_ph(this.PedidoVendaDataSet1.PedidoVendaPH, txtPedido.Text);
    this.reportViewer1.RefreshReport();
} 

2 answers

2


I will help you with the code I use to generate the PDF file from ReportViewer

Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;

byte[] bytes = this.reportViewer1.LocalReport.Render(
    "PDF", null, out mimeType, out encoding, out filenameExtension,
    out streamids, out warnings);

using (FileStream fs = new FileStream("output.pdf", FileMode.Create))
{
    fs.Write(bytes, 0, bytes.Length);
}

System.Diagnostics.Process.Start("output.pdf");
  • Pablo thanks for the attention this code you passed above it just generates not visualize right.

  • No, it saves the pdf to disk and opens

  • Why you link the dataset 2 times?

  • Because they are two different Datasets, the DemonstrativoDataSet I use to mount the header. and the DemonstrativoProcedimentoDataSet I use to show the details.

  • But depending on how your report is going, you don’t need to put the datasets.

  • So Pablo I already assemble the report as shown in my code above, I already see it on the mounted screen, what I need and generate direct pdf and already view.

  • Chat..

Show 3 more comments

0

Yes, this is possible. In the method triggered by the print button, create a Datatable.

DataTable dt = new DataTable();

After recovering the data and filling the Datatable, load the data in Reportviewer.

ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("NOME_DATASET", dt));
ReportViewer1.DataBind();

Finally, return the data so that the mimetype be the type application/pdf, according to the code below.

Warning[] warn = null;
string[] streamids = null;
string mimeType = "application/pdf";
string encoding = string.Empty;
string extension = string.Empty;
byte[] byteViewer = null;
string deviceInfo = null;

byteViewer = ReportViewer1.LocalReport.Render("pdf", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warn);
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=NOME_DO_ARQUIVO.pdf");
Response.BinaryWrite(byteViewer);

OBS.: To use the class Warning, include the namespace Microsoft.Reporting.Webforms.

With this return, the PDF file will automatically open in the user’s browser.

  • Seriously, your example using Response will only work if the project is web, in case the question is a Winforms project

  • Truth does not work because and windows form and not web, I’m having errors in Response

  • True, I hadn’t noticed that detail.

Browser other questions tagged

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