打印机使用rdlc中的本地报告在所有页面中打印第一页值

时间:2018-04-11 04:56:46

标签: c# rdlc localreport

我使用rdlc报告设计了账单。在该报告中,我使用没有预览打印。它基于此链接https://msdn.microsoft.com/en-us/library/ms252091.aspx

它可以完美地生成20行的账单。如果表格行在表格中超过20条记录,同样的第一页再次生成下一页。我在google搜索了很多东西。但我无法找到合适的解决方案我很累。

namespace Hotel_Project
{
public class PrintClassNew : IDisposable
{
    int Paper_height = 0;
    private int m_currentPageIndex;
    private IList<Stream> m_streams;
    //int 
    // Routine to provide to the report renderer, in order to
    //    save an image for each page of the report.
    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><StartPage>0</StartPage><PageWidth>3.5in</PageWidth><PageHeight>8.1in</PageHeight><MarginTop>0in</MarginTop><MarginLeft>0in</MarginLeft><MarginRight>0in</MarginRight><MarginBottom>0in</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 Run(DataTable dt)
    {
        LocalReport report = new LocalReport();
        report.ReportPath = "Report.rdlc";
        report.DataSources.Add(
           new ReportDataSource("BillReport", dt));

        ReportParameter[] param = new ReportParameter[18];
            param[0] = new ReportParameter("hotelname", label4.Text, true);
            param[1] = new ReportParameter("hoteladdress", TempPrint_Address, true);
            param[2] = new ReportParameter("gstno", Temp_Print_GSTNo, true);
            param[3] = new ReportParameter("printmessage", Temp_Print_OutMessage, true);
            param[4] = new ReportParameter("companyname", sql.ReadSetting("BillBy"), true);
            param[5] = new ReportParameter("billno", label7.Text, true);
            param[6] = new ReportParameter("billdate", label8.Text, true);
            param[7] = new ReportParameter("billtime", DateTime.Now.ToString("hh:mm:ss tt"), true);
            param[8] = new ReportParameter("billtype", BookingType, true);
            param[9] = new ReportParameter("billmode", PayMode, true);
            param[10] = new ReportParameter("billamount", " RS " + Convert.ToString(Temp_Amount) + " /-", true);
            param[11] = new ReportParameter("total", Convert.ToString(Temp_Amount), true);
            param[12] = new ReportParameter("qty", Convert.ToString(Temp_QtyNew), true);
            param[13] = new ReportParameter("sgst", Convert.ToString(Temp_GST), true);
            param[14] = new ReportParameter("cgst", Convert.ToString(Temp_GST), true);
            param[15] = new ReportParameter("autual", Convert.ToString(Temp_Amount - Temp_TotalTAXAmount), true);
            if (Temp_TotalTAXAmount == 0)
                param[16] = new ReportParameter("Temp_TotalTAXAmount", Convert.ToString(Temp_TotalTAXAmount), true);
            else
                param[16] = new ReportParameter("Temp_TotalTAXAmount", Convert.ToString(Temp_TotalTAXAmount / 2), true);
            param[17] = new ReportParameter("TotalGst", Convert.ToString(Temp_GST + Temp_GST), true);

            //LocalReport.ReportPath = "BillPrint.rdlc";

            report.SetParameters(param);
            report.Refresh();
        Export(report);
        Print();
    }

    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.DefaultPageSettings.Margins.Top = 0;
            //printDoc.DefaultPageSettings.Margins.Bottom = 0;
            //printDoc.DefaultPageSettings.Margins.Left = 0;
            //printDoc.DefaultPageSettings.Margins.Right = 0;
            printDoc.DefaultPageSettings.PaperSize = new PaperSize("", 300, (370 + Paper_height));

            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            m_currentPageIndex = 0;
            printDoc.Print();
            Paper_height = 0;
        }
    }
    // Create a local report for Report.rdlc, load the data,
    //    export the report to an .emf file, and print it.
    public void Run(LocalReport report, int Print_height)
    {
        Paper_height = Print_height;
        // LocalReport report = new LocalReport();
        //report.ReportPath = @"D:\Sathish\FreeLance\Projects\testing_PrintConsole\testing_PrintConsole\Report.rdlc";
        //string[] sqlparArrNew1 = { };
        //DataSet dsNew = GetDataSetFromSP("USP_test", sqlparArrNew1, 2);

        //report.DataSources.Add(
        //   new ReportDataSource("DataSet1", dsNew.Tables[0]));
        Export(report);
        Print();
        Dispose();
    }

    public void Dispose()
    {
        if (m_streams != null)
        {
            foreach (Stream stream in m_streams)
                stream.Close();
            m_streams = null;
        }
    }

    private static SqlConnection Get_connection(int Con_Status)
    {
        string Connection_Name = "";
        if (Con_Status == 1) { Connection_Name = "Testing_con"; }
        else if (Con_Status == 2) { Connection_Name = "ExcelTrack"; }
        SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings[Connection_Name].ConnectionString);

        return con1;
    }
    public DataSet GetDataSetFromSP(string procedureName, string[] parametervalues, int con_status)
    {
        SqlConnection sqlcon = Get_connection(con_status);

        DataSet resultTable = new DataSet();
        //  WriteLog("Query Execution starts....");
        try
        {
            SqlCommand sqlcmd = new SqlCommand(procedureName, sqlcon);
            SqlCommand cmd = new SqlCommand(procedureName, sqlcon);
            //  WriteLog("Query : " + procedureName);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandType = CommandType.StoredProcedure;

            if (sqlcon.State == ConnectionState.Closed)
            { sqlcon.Open(); }

            SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
            SqlCommandBuilder.DeriveParameters(cmd);

            int i = 0;

            foreach (SqlParameter p in cmd.Parameters)
            {
                if (p.Direction == ParameterDirection.Input)
                {
                    //if (parametervalues[i].Trim() == string.Empty)
                    //    sqlcmd.Parameters.AddWithValue(p.ParameterName, DBNull.Value);
                    //else
                    sqlcmd.Parameters.AddWithValue(p.ParameterName, parametervalues[i]);
                    //    WriteLog("Parameter: " + p.ParameterName + " | " + parametervalues[i]);
                    i++;
                }
            }

            adp.Fill(resultTable);
            //   WriteLog("Query Execution End....");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (sqlcon.State == ConnectionState.Open)
            {
                sqlcon.Close();
            }
            sqlcon = null;
        }

        return resultTable;
    }
}
}
  

我的账单样本如下,

enter image description here 谢谢你!

0 个答案:

没有答案