Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using the below code to print RDCL Report directly ,
but the problem in the printed page sittings , it is not same to what i choose in the report properties (A4), i tried to change page width and height in device info but same issue . please any idea?

What I have tried:

private Stream CreateStream(string name,
      string fileNameExtension, Encoding encoding,
      string mimeType, bool willSeek)
    {
        Stream stream = new MemoryStream();
        m_streams.Add(stream);
        return stream;
    }
    // Export the given report as an EMF (Enhanced Metafile) file.
    private void Export(LocalReport report)
    {
        string deviceInfo =
          @"<DeviceInfo>
                <OutputFormat>EMF</OutputFormat>
                <PageWidth>8.27in</PageWidth>
                <PageHeight>11.69in</PageHeight>
                <MarginTop>0.25in</MarginTop>
                <MarginLeft>0.25in</MarginLeft>
                <MarginRight>0.25in</MarginRight>
                <MarginBottom>0.25in</MarginBottom>
            </DeviceInfo>";
        Warning[] warnings;
        m_streams = new List<Stream>();
        report.Render("Image", deviceInfo, CreateStream,
           out warnings);
        foreach (Stream stream in m_streams)
            stream.Position = 0;
    }
    // Handler for PrintPageEvents
    private void PrintPage(object sender, PrintPageEventArgs ev)
    {   
        Metafile pageImage = new
           Metafile(m_streams[m_currentPageIndex]);

        // Adjust rectangular area with printer margins.
        Rectangle adjustedRect = new Rectangle(
            ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
            ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
            ev.PageBounds.Width,
            ev.PageBounds.Height);

        // Draw a white background for the report
        ev.Graphics.FillRectangle(Brushes.White, adjustedRect);

        // Draw the report content
        ev.Graphics.DrawImage(pageImage, adjustedRect);

        // Prepare for the next page. Make sure we haven't hit the end.
        m_currentPageIndex++;
        ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
    }

    private void Print()
    {
        if (m_streams == null || m_streams.Count == 0)
            throw new Exception("Error: no stream to print.");
        PrintDocument printDoc = new PrintDocument();
        if (!printDoc.PrinterSettings.IsValid)
        {
            throw new Exception("Error: cannot find the default printer.");
        }
        else
        {
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            m_currentPageIndex = 0;
            printDoc.Print();
        }
    }
    // Create a local report for Report.rdlc, load the data,
    //    export the report to an .emf file, and print it.
    private void Run()
    {
        LocalReport report = new LocalReport();
        report.ReportPath = @"Doctor_form.rdlc";
        Export(report);
        Print();
    }
 protected void print_Click1(object sender, EventArgs e)
    {
       
           LocalReport report = new LocalReport();
            report.ReportPath = @"Doctor_form.rdlc";
            report.SetParameters(parameters);
            Export(report);
            Print();}
Posted
Updated 26-Nov-18 21:39pm
v2
Comments
Vipin_Arora 27-Nov-18 3:42am    
Are you working on Windows forms, ASP.NET or Cshtml?
Also check whether problem exist only while printing with your application or is it a printer/driver issue
loai_maane 27-Nov-18 3:49am    
hi dear ,
i am using asp.net.
i tried to print the report from the print button on report viewer it is OK the printed page was A4.but using the code the printed page was huge and printed on 4 papers
loai_maane 27-Nov-18 4:11am    
dear if i rendering as PDF,is my code should create PDF physically in Program file to print the generated PDF?
alexvw 27-Nov-18 14:10pm    
Hi there,

The code you posted, seems to be a copy-paste extract of "Walkthrough: Printing a Local Report without Preview" (https://msdn.microsoft.com/en-us/library/ms252091.aspx).

Since it is not complete, it would quite hard for someone to offer useful help. The fact that said walkthrough targets a console application should not be an inconvenient.

Please, share all the relevant code, and error message(s) you may be getting with the community, so that you can receive adequate help.

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



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