按“Enter”键时 ReportViewer 打印 在 C# 中

发布于 2024-07-29 07:52:36 字数 103 浏览 3 评论 0原文

我有一个报表查看器,并启用了打印按钮,但未启用工具栏。当用户按 Enter 时,报表查看器应该开始打印。它甚至不应该显示 printdialog。我应该在 KeyPres 事件中编写什么代码?

I have a reportviewer and have enabled print button but not enabled toolbar.When user press enter reportviewer should start printing.It should not show even printdialog also.What code should i write in KeyPres event ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

書生途 2024-08-05 07:52:36

这是当我想以编程方式从 ReportViewer 打印时,什么对我有用。

您需要将 PrintController 设置为 StandardPrintController 以抑制打印对话框,例如

printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();

This is what worked for me when I wanted to programmatically print from ReportViewer.

You'll want to set the PrintController to StandardPrintController to suppress the print dialog, e.g.

printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();
内心激荡 2024-08-05 07:52:36

这是我在报表查看器中手动打印的代码。 不过它是在 VB.NET 中的。
它通过处理 PrintDocument 对象的打印事件来工作。

  Dim m_pageSettings As PageSettings          'Stores page settings for printout
  Dim m_currentPage As Integer                'Used for index of pages
  Private m_pages As New List(Of Stream)()    'Stores a stream for each pages

  'Event fires when printDocument starts printing - reset page index to zero
  Private Sub PrintDocument1_BeginPrint(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
    m_currentPage = 0
  End Sub

  'Function that prints all the pages included in the report
  Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim pageToPrint As Stream = m_pages(m_currentPage)
    pageToPrint.Position = 0

    Dim pageMetaFile As Metafile = New Metafile(pageToPrint)      'create an image(metafile) of the report page
    Using (pageMetaFile)
      'Create a rectangle the size of our report - include margins
      ' Dim adjustedRect As Rectangle = New Rectangle( _
      '    e.PageBounds.Left - CType(e.PageSettings.HardMarginX, Integer), _
      '    e.PageBounds.Top - CType(e.PageSettings.HardMarginY, Integer), _
      '    e.PageBounds.Width, _
      '    e.PageBounds.Height)

      Dim adjustedRect As Rectangle = New Rectangle( _
    e.PageBounds.Left, _
    e.PageBounds.Top, _
    e.PageBounds.Width, _
    e.PageBounds.Height)

      e.Graphics.FillRectangle(Brushes.White, adjustedRect)   'Fill rectangle with WHITE background
      e.Graphics.DrawImage(pageMetaFile, adjustedRect)        'Draw report in rectangle - this will be printed
      m_currentPage = m_currentPage + 1
      e.HasMorePages = m_currentPage < m_pages.Count          'If more pages are left - keep processing
    End Using
  End Sub

  'Event fires when PrintDocument queries for PageSettings.  Return a copy of m_pagesettings.
  Private Sub PrintDocument1_QueryPageSettings(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles PrintDocument1.QueryPageSettings
    e.PageSettings = CType(m_pageSettings.Clone, PageSettings)
  End Sub

  'Render the report in a EMF - This function creates metafiles(images) of each page in the report
  Private Sub RenderAllLocalReportPages(ByVal localReport As LocalReport)
    Dim deviceInfo As String = CreateEMFDeviceInfo()    'Enhanced MetaFile
    Dim warnings As Warning() = Nothing
    localReport.Render("IMAGE", deviceInfo, AddressOf LocalReportCreateStreamCallback, warnings)
  End Sub

  'Callback function used with RenderAllLocalReportPages
  Private Function LocalReportCreateStreamCallback(ByVal name As String, ByVal extension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
    Dim stream As New MemoryStream()
    m_pages.Add(stream)
    Return stream
  End Function

  Private Function CreateEMFDeviceInfo() As String
    Dim paperSize As PaperSize = m_pageSettings.PaperSize
    Dim margins As Margins = m_pageSettings.Margins

    'The device info string defines the page range to print as well as the size of the page.
    'A start and end page of 0 means generate all pages.
    Return String.Format(CultureInfo.InvariantCulture, "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>", ToInches(margins.Top), ToInches(margins.Left), ToInches(margins.Right), ToInches(margins.Bottom), ToInches(paperSize.Height), ToInches(paperSize.Width))
  End Function

  'Convert report printing size to inches
  Private Shared Function ToInches(ByVal hundrethsOfInch As Integer) As String
    Dim inches As Double = hundrethsOfInch / 100.0R
    Return inches.ToString(CultureInfo.InvariantCulture) & "in"
  End Function

Here is my code for manual printing in Report Viewer. It's in VB.NET though.
It works by handling the Print Event of your PrintDocument object.

  Dim m_pageSettings As PageSettings          'Stores page settings for printout
  Dim m_currentPage As Integer                'Used for index of pages
  Private m_pages As New List(Of Stream)()    'Stores a stream for each pages

  'Event fires when printDocument starts printing - reset page index to zero
  Private Sub PrintDocument1_BeginPrint(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
    m_currentPage = 0
  End Sub

  'Function that prints all the pages included in the report
  Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim pageToPrint As Stream = m_pages(m_currentPage)
    pageToPrint.Position = 0

    Dim pageMetaFile As Metafile = New Metafile(pageToPrint)      'create an image(metafile) of the report page
    Using (pageMetaFile)
      'Create a rectangle the size of our report - include margins
      ' Dim adjustedRect As Rectangle = New Rectangle( _
      '    e.PageBounds.Left - CType(e.PageSettings.HardMarginX, Integer), _
      '    e.PageBounds.Top - CType(e.PageSettings.HardMarginY, Integer), _
      '    e.PageBounds.Width, _
      '    e.PageBounds.Height)

      Dim adjustedRect As Rectangle = New Rectangle( _
    e.PageBounds.Left, _
    e.PageBounds.Top, _
    e.PageBounds.Width, _
    e.PageBounds.Height)

      e.Graphics.FillRectangle(Brushes.White, adjustedRect)   'Fill rectangle with WHITE background
      e.Graphics.DrawImage(pageMetaFile, adjustedRect)        'Draw report in rectangle - this will be printed
      m_currentPage = m_currentPage + 1
      e.HasMorePages = m_currentPage < m_pages.Count          'If more pages are left - keep processing
    End Using
  End Sub

  'Event fires when PrintDocument queries for PageSettings.  Return a copy of m_pagesettings.
  Private Sub PrintDocument1_QueryPageSettings(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles PrintDocument1.QueryPageSettings
    e.PageSettings = CType(m_pageSettings.Clone, PageSettings)
  End Sub

  'Render the report in a EMF - This function creates metafiles(images) of each page in the report
  Private Sub RenderAllLocalReportPages(ByVal localReport As LocalReport)
    Dim deviceInfo As String = CreateEMFDeviceInfo()    'Enhanced MetaFile
    Dim warnings As Warning() = Nothing
    localReport.Render("IMAGE", deviceInfo, AddressOf LocalReportCreateStreamCallback, warnings)
  End Sub

  'Callback function used with RenderAllLocalReportPages
  Private Function LocalReportCreateStreamCallback(ByVal name As String, ByVal extension As String, ByVal encoding As Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
    Dim stream As New MemoryStream()
    m_pages.Add(stream)
    Return stream
  End Function

  Private Function CreateEMFDeviceInfo() As String
    Dim paperSize As PaperSize = m_pageSettings.PaperSize
    Dim margins As Margins = m_pageSettings.Margins

    'The device info string defines the page range to print as well as the size of the page.
    'A start and end page of 0 means generate all pages.
    Return String.Format(CultureInfo.InvariantCulture, "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>", ToInches(margins.Top), ToInches(margins.Left), ToInches(margins.Right), ToInches(margins.Bottom), ToInches(paperSize.Height), ToInches(paperSize.Width))
  End Function

  'Convert report printing size to inches
  Private Shared Function ToInches(ByVal hundrethsOfInch As Integer) As String
    Dim inches As Double = hundrethsOfInch / 100.0R
    Return inches.ToString(CultureInfo.InvariantCulture) & "in"
  End Function
我最亲爱的 2024-08-05 07:52:36

我正在为我的项目使用自定义报告。 自定义表示使用 e.Graphics.DrawString 类型代码构建的报告。

所以我在PrintDocument的默认打印事件中制作了所有报告(编码)

这是PrintDocument工具的。 将其拖放到屏幕上,然后双击它以创建事件处理程序。

PrintDocoument 事件处理程序背后的代码是:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
       Image image = Properties.Resources.New_Header;
       Font font = new Font("Arial", 7, FontStyle.Bold);
        Font font2  = new Font("Arial", 7, FontStyle.Regular);
        float fontHeight = font2.GetHeight();
        e.Graphics.DrawImage(image, 20, 5, 260, 130);
        e.Graphics.DrawString("Date:" + DateTime.Now.ToShortDateString(), new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(20, 160));
        e.Graphics.DrawString("Invoice#:  "+InvoiceNoTextBox.Text, new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(160, 160));
        e.Graphics.DrawString("Client Name:  " + ClientNameTextBox.Text.Trim(), new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(20, 180));
        e.Graphics.DrawString("______________________________",
            new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(20, 190));
        e.Graphics.DrawString("Product", new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(23, 220));
        e.Graphics.DrawString("U/Price", new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(170, 220));
        e.Graphics.DrawString("Qty", new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(210, 220));
        e.Graphics.DrawString("Total", new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(240, 220));
        e.Graphics.DrawString("______________________________",
           new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(20, 220));

        int CurrentX = 25;
        int CurrentY = 230;
        int offset = 20;
        foreach (var i in shoppingCart)
        {
            e.Graphics.DrawString(i.ItemName, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(CurrentX, CurrentY+offset));
            e.Graphics.DrawString(i.UnitPrice.ToString(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + offset));
            e.Graphics.DrawString(i.Quantity.ToString(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(215, CurrentY + offset));
            e.Graphics.DrawString(i.TotalPrice.ToString(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(240, CurrentY + offset));
            offset = offset + (int)fontHeight + 5;

        }
        CurrentY = CurrentY+offset;
        e.Graphics.DrawString("______________________________",
          new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY-5));
        e.Graphics.DrawString("Total Amt: Rs." + TotalAmountTextBox.Text, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 35));
        e.Graphics.DrawString("Discount%:  " + DiscountTextBox.Text.Trim()+"%", new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 50));
        e.Graphics.DrawString("Discount: Rs." + JustDiscountTextBox.Text.Trim(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 65));
        e.Graphics.DrawString("Grand Total: Rs." + TotalToPayTextBox.Text, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 80));
        e.Graphics.DrawString("Advance: Rs." + AdvancePaymentTextBox.Text.Trim(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY + 95));
        e.Graphics.DrawString("Cash: Rs." + givenCash.Text.Trim(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 95));
        e.Graphics.DrawString("Pending: Rs." + PendingPaymentTextBox.Text, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY + 110));
        e.Graphics.DrawString("Return: Rs." + ReturnTextBox.Text, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 110));
        e.Graphics.DrawString("Software Provided By Muhammad Abbas", new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY + 125));
        e.Graphics.DrawString("Mob:0304-9550308", new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY + 140));

    }

    private void PrintPreviewButton_Click(object sender, EventArgs e)
    {
        try
        {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

最后是您问题的答案。 在您的表单 KeyDown 事件中使用它。

    private void Form_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            printDocument1.Print();
        }
    }

它将直接从打印机打印您的收据。

I am using custom reporting for my project. Custom means the report that is built using e.Graphics.DrawString type code.

So I made All the report(coding) in the default printing event of PrintDocument

Here is the of the PrintDocument Tool. Drag and Drop it on your screen and then double click on it to create the event handler.

The code behind event handler of PrintDocoument is:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
       Image image = Properties.Resources.New_Header;
       Font font = new Font("Arial", 7, FontStyle.Bold);
        Font font2  = new Font("Arial", 7, FontStyle.Regular);
        float fontHeight = font2.GetHeight();
        e.Graphics.DrawImage(image, 20, 5, 260, 130);
        e.Graphics.DrawString("Date:" + DateTime.Now.ToShortDateString(), new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(20, 160));
        e.Graphics.DrawString("Invoice#:  "+InvoiceNoTextBox.Text, new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(160, 160));
        e.Graphics.DrawString("Client Name:  " + ClientNameTextBox.Text.Trim(), new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(20, 180));
        e.Graphics.DrawString("______________________________",
            new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(20, 190));
        e.Graphics.DrawString("Product", new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(23, 220));
        e.Graphics.DrawString("U/Price", new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(170, 220));
        e.Graphics.DrawString("Qty", new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(210, 220));
        e.Graphics.DrawString("Total", new Font("Arial", 7, FontStyle.Bold), Brushes.Black, new Point(240, 220));
        e.Graphics.DrawString("______________________________",
           new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(20, 220));

        int CurrentX = 25;
        int CurrentY = 230;
        int offset = 20;
        foreach (var i in shoppingCart)
        {
            e.Graphics.DrawString(i.ItemName, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(CurrentX, CurrentY+offset));
            e.Graphics.DrawString(i.UnitPrice.ToString(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + offset));
            e.Graphics.DrawString(i.Quantity.ToString(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(215, CurrentY + offset));
            e.Graphics.DrawString(i.TotalPrice.ToString(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(240, CurrentY + offset));
            offset = offset + (int)fontHeight + 5;

        }
        CurrentY = CurrentY+offset;
        e.Graphics.DrawString("______________________________",
          new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY-5));
        e.Graphics.DrawString("Total Amt: Rs." + TotalAmountTextBox.Text, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 35));
        e.Graphics.DrawString("Discount%:  " + DiscountTextBox.Text.Trim()+"%", new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 50));
        e.Graphics.DrawString("Discount: Rs." + JustDiscountTextBox.Text.Trim(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 65));
        e.Graphics.DrawString("Grand Total: Rs." + TotalToPayTextBox.Text, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 80));
        e.Graphics.DrawString("Advance: Rs." + AdvancePaymentTextBox.Text.Trim(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY + 95));
        e.Graphics.DrawString("Cash: Rs." + givenCash.Text.Trim(), new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 95));
        e.Graphics.DrawString("Pending: Rs." + PendingPaymentTextBox.Text, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY + 110));
        e.Graphics.DrawString("Return: Rs." + ReturnTextBox.Text, new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(180, CurrentY + 110));
        e.Graphics.DrawString("Software Provided By Muhammad Abbas", new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY + 125));
        e.Graphics.DrawString("Mob:0304-9550308", new Font("Arial", 7, FontStyle.Regular), Brushes.Black, new Point(20, CurrentY + 140));

    }

    private void PrintPreviewButton_Click(object sender, EventArgs e)
    {
        try
        {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

And Finally the Answer of you Question. Use this in your Form KeyDown event.

    private void Form_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            printDocument1.Print();
        }
    }

It will Directly print your receipt from your Printer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文