Report generated with reportview, directly in pdf

1

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

I would like to know if it is possible and if yes how to do to shorten this entire path and click on the print button, and it already open the reportview report already in pdf, triggering the user program's pdf program to view, or either when you click the print button or view it in pdf.

Follow the code on the print button that calls the 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();
} 
    
asked by anonymous 17.11.2017 / 19:03

2 answers

2

I'll help you with the code I use to generate the PDF file of 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");
    
17.11.2017 / 19:08
0

Yes, this is possible. In the method printed by the button, create a DataTable .

DataTable dt = new DataTable();

After retrieving the data and populating the DataTable, load the data into the ReportViewer.

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

Finally, return the data so that the mimeType is of type application / pdf , as per 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);

NOTE: To use the Warning class, include the Microsoft.Reporting.WebForms

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

    
17.11.2017 / 19:53