Search code examples
webformsrdlcreportviewer

ReportViewer prints one page letter size, but when I print it is in two pages


I'm using vs2017, C#, asp.net 4.6.1, and Rdlc 14.2 in a web.forms site

I have an one page report when I sent directly it to pdf, is in one page

        ReportViewer1.DataBind();

        Microsoft.Reporting.WebForms.Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string extension;

        string deviceInfo = "<DeviceInfo>" +
                                           "  <OutputFormat>PDF</OutputFormat>" +
                                           "  <PageWidth>8.5in</PageWidth>" +
                                           "  <PageHeight>11in</PageHeight>" +
                                           "  <MarginTop>0.5in</MarginTop>" +
                                           "  <MarginLeft>0.25in</MarginLeft>" +
                                           "  <MarginRight>0.25in</MarginRight>" +
                                           "  <MarginBottom>1in</MarginBottom>" +
                                           "</DeviceInfo>";
        byte[] writeBinaryBytes = new byte[0];
        writeBinaryBytes = ReportViewer1.LocalReport.Render
            ("Pdf", deviceInfo, out mimeType, out encoding, out extension,
            out streamids, out warnings);

        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.AddHeader
    ("content-disposition", "attachment; filename=" + nombreReporte+".pdf");
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.BinaryWrite(writeBinaryBytes);
        HttpContext.Current.Response.Flush();

When I view it in ReportViewer is in one page, but if I export it to pdf or print it, is in two pages… This is my code behind before printing

        System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
        pg.Margins.Top = 50;        //hundredths of an inch 0.5" * 100
        pg.Margins.Left = 25;       //hundredths of an inch 0.25" * 100
        pg.Margins.Right = 25;      //hundredths of an inch 0.25" * 100
        pg.Margins.Bottom = 100;    //hundredths of an inch 1" * 100
        System.Drawing.Printing.PaperSize size = new PaperSize
        {
            RawKind = (int)PaperKind.Letter
            //hundredths of an inch
            , Width = 850       //hundredths of an inch 8.5" * 100
            , Height = 1100     //hundredths of an inch 11" * 100
        };
    pg.PaperSize = size;
        pg.Landscape = false;
        //pg.PaperSource.RawKind = (int)PaperKind.A5;
        ReportViewer1.SetPageSettings(pg);
        ReportViewer1.LocalReport.Refresh();

This is the ReportViewer control in aspx page:

            <rsweb:ReportViewer ID="ReportViewer1" runat="server" 
                ShowToolBar="true" 
                ShowFindControls ="false"
                ShowCredentialPrompts="true"
                ShowDocumentMapButton="true"
                EnableEventValidation="fase" 
                AsyncRendering = "false"
                width="100%" />

Any suggestions?

Thanks rubenc


Solution

  • Well it turned out that the problem was that I had margins set inside the RDLC report, I just changed them to 0 and now its working.