【问题标题】:How can I get client PC Printer settings using C#, asp.net如何使用 C#、asp.net 获取客户端 PC 打印机设置
【发布时间】:2015-05-20 05:58:47
【问题描述】:

我在 Web 应用程序上运行 SSRS 报告。我想直接在客户端的打印机上打印 SSRS 报告数据。该应用程序在我的电脑上运行良好,但是当我在 IIS Web 服务器上发布该应用程序时,该应用程序不是检索客户端电脑的打印机设置,而是尝试查找安装在服务器计算机中的打印机。

我正在使用 System.Drawing.Printing,有什么方法可以在客户端计算机中获取已安装打印机的设置,或者我们可以使用 C# 从 Web 打印 SSRS 报告吗?

我正在使用以下代码。

    using System;
    using System.IO;
    using System.Text;
    using System.Globalization;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Printing;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    using Microsoft.Reporting.WebForms;
    using SeaWorld.Common.Helper;

namespace SeaWorld.BusinessObjects.BAL.Generated
{
    public class ReportPrintDocument : PrintDocument
    {
        private PageSettings m_pageSettings;
        private int m_currentPage;
        private List<Stream> m_pages = new List<Stream>();

        public ReportPrintDocument(ServerReport serverReport): this((Report)serverReport)
        {
            RenderAllServerReportPages(serverReport);
        }

        //public ReportPrintDocument(LocalReport localReport): this((Report)localReport)
        //{
        //    RenderAllLocalReportPages(localReport);
        //}

        private ReportPrintDocument(Report report)
        {
            // Set the page settings to the default defined in the report
            ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

            // The page settings object will use the default printer unless
            // PageSettings.PrinterSettings is changed.  This assumes there
            // is a default printer.
            m_pageSettings = new PageSettings();
            m_pageSettings.PaperSize = reportPageSettings.PaperSize;
            m_pageSettings.Margins = reportPageSettings.Margins;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
            {
                foreach (Stream s in m_pages)
                {
                    s.Dispose();
                }

                m_pages.Clear();
            }
        }

        protected override void OnBeginPrint(PrintEventArgs e)
        {
            base.OnBeginPrint(e);

            m_currentPage = 0;
        }

        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            base.OnPrintPage(e);

            Stream pageToPrint = m_pages[m_currentPage];
            pageToPrint.Position = 0;

            // Load each page into a Metafile to draw it.
            using (Metafile pageMetaFile = new Metafile(pageToPrint))
            {
                Rectangle adjustedRect = new Rectangle(
                        e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                        e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                        e.PageBounds.Width,
                        e.PageBounds.Height);

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

                // Draw the report content
                e.Graphics.DrawImage(pageMetaFile, adjustedRect);

                // Prepare for next page.  Make sure we haven't hit the end.
                m_currentPage++;
                e.HasMorePages = m_currentPage < m_pages.Count;
            }
        }

        protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
        {
            e.PageSettings = (PageSettings)m_pageSettings.Clone();
        }

        private void RenderAllServerReportPages(ServerReport serverReport)
        {
            string deviceInfo = CreateEMFDeviceInfo();

            // Generating Image renderer pages one at a time can be expensive.  In order
            // to generate page 2, the server would need to recalculate page 1 and throw it
            // away.  Using PersistStreams causes the server to generate all the pages in
            // the background but return as soon as page 1 is complete.
            NameValueCollection firstPageParameters = new NameValueCollection();
            firstPageParameters.Add("rs:PersistStreams", "True");

            // GetNextStream returns the next page in the sequence from the background process
            // started by PersistStreams.
            NameValueCollection nonFirstPageParameters = new NameValueCollection();
            nonFirstPageParameters.Add("rs:GetNextStream", "True");

            string mimeType;
            string fileExtension;
            Stream pageStream = serverReport.Render("IMAGE", deviceInfo, firstPageParameters, out mimeType, out fileExtension);

            // The server returns an empty stream when moving beyond the last page.
            while (pageStream.Length > 0)
            {
                m_pages.Add(pageStream);

                pageStream = serverReport.Render("IMAGE", deviceInfo, nonFirstPageParameters, out mimeType, out fileExtension);
            }
        }

        private void RenderAllLocalReportPages(LocalReport localReport)
        {
            string deviceInfo = CreateEMFDeviceInfo();

            Warning[] warnings;
            localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out warnings);
        }

        private Stream LocalReportCreateStreamCallback(
            string name,
            string extension,
            Encoding encoding,
            string mimeType,
            bool willSeek)
        {
            MemoryStream stream = new MemoryStream();
            m_pages.Add(stream);

            return stream;
        }

        private string CreateEMFDeviceInfo()
        {
            PaperSize paperSize = m_pageSettings.PaperSize;
            Margins 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));
        }

        private static string ToInches(int hundrethsOfInch)
        {
            double inches = hundrethsOfInch / 100.0;
            return inches.ToString(CultureInfo.InvariantCulture) + "in";
        }
    }
}



On print button click

 void ctrlSettingToolbar_btnPrintPressed(object sender, EventArgs e)
        {
            try
            {
                ReportPrintDocument rp = new ReportPrintDocument(rptBillOfLoadings.ServerReport);

                rp.Print();
            }
            catch (Exception ex) 
            {
                LogHelper.PrintError("Error:", ex);
            }
        }

【问题讨论】:

  • System.Drawing.Printing 将提供托管应用程序的打印机列表..为了满足您的要求,我猜您必须依赖一些脚本..如果我错了,请纠正我
  • 你是对的@Sachu,System.Drawing.Printing 只给我托管或运行应用程序的打印机列表......如果你有任何 java 脚本来解决这个问题,请与我分享.
  • 试试下面的答案..希望它有效
  • 想象一下,如果您访问的任何网站都可以列举您的打印机并开始打印他们想要的任何内容,那么世界会是什么样子。而且我认为弹出窗口很烦人。

标签: c# asp.net .net reporting-services printing


【解决方案1】:

你可以使用javascript函数window.Print() 这将列出您的客户端机器打印机并打印您的页面。

希望下面的代码会有所帮助..try

<html>
<head>
<script>
function page()
  {
  window.print()
  }
</script>
</head>
<body>

<input type="button" value="Print page" onclick="page()">

</body>
</html>

但这将打印整个页面..在您的情况下,它是要打印的报告

希望这个链接可以指导你

print crystal report to client printer

print report to client default printer

【讨论】:

  • 我认为这将打印整个窗口屏幕,而我只想打印报表查看器页面中可见的报表..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
相关资源
最近更新 更多