c# - Visual Studio 2015 社区报告查看器版本 12 使用 C# 获取额外 margin 时出错

标签 c# sql-server rdlc

我正在使用 Visual Studio 2015 社区报告查看器版本 12 在我的 C# 项目中显示 rdlc 报告。 这是普通的A4页报告

Normal Image on windows 7

它在客户端 PC 上适用于 Windows XP、Vista、Win 7,但是当在 Windows 10 64 位上安装相同的应用程序时,我会遇到如下问题

Error In Windows 10

正如您在上图中看到的那样,右侧和底部出现了不必要的边距,字体大小也减小了。但是当我将报告导出为 PDF 时,生成的 PDF 没有问题,它与我设计的报告相同。

我尝试了什么:

  1. 我安装了 MICROSOFT® REPORT VIEWER 2015 RUNTIME https://www.microsoft.com/en-us/download/details.aspx?id=45496
  2. 已安装 Microsoft® SQL Server® 2014 功能包(SQL 的系统 CLR 类型 服务器 2014)
  3. 尝试使用以下代码将 RDLC 直接导出到打印机

打印类代码

public static class _cWainfoPrintReport
    {
        private static int m_currentPageIndex;
        private static IList<Stream> m_streams;
        public static Stream CreateStream(string name,
        string fileNameExtension, Encoding encoding,
        string mimeType, bool willSeek)
        {
            Stream stream = new MemoryStream();
            m_streams.Add(stream);
            return stream;
        }
        public static void _mExport(LocalReport report, bool print = true, double _pageWightInches = 8.27, double _pageHeightInches = 11.69, double _MarginTopInches = 0.025, double _MarginLeftInches = 0.025, double _MarginRightInches = 0.025, double _MarginBottomInches = 0.025)
        {
            string deviceInfo =
            @"<DeviceInfo> <OutputFormat>EMF</OutputFormat> <PageWidth>" + _pageWightInches + "in</PageWidth> <PageHeight>" + _pageHeightInches + "in</PageHeight> <MarginTop>" + _MarginTopInches + "in</MarginTop> <MarginLeft>" + _MarginLeftInches + "in</MarginLeft> <MarginRight>" + _MarginRightInches + "in</MarginRight> <MarginBottom>" + _MarginBottomInches + "in</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;
            if (print)
            {
                _mPrint(_pageWightInches, _pageHeightInches, _MarginTopInches, _MarginLeftInches, _MarginRightInches, _MarginBottomInches);
            }
            report.ReleaseSandboxAppDomain();
        }
        // Handler for PrintPageEvents
        public static void _mPrintPage(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);
        }
        public static PaperSize CalculatePaperSize(double WidthInCentimeters, double HeightInCentimetres)
        {
            int Width = int.Parse((Math.Round((WidthInCentimeters * 0.393701) * 100, 0, MidpointRounding.AwayFromZero)).ToString());
            int Height = int.Parse((Math.Round((HeightInCentimetres * 0.393701) * 100, 0, MidpointRounding.AwayFromZero)).ToString());

            PaperSize NewSize = new PaperSize();
            NewSize.RawKind = (int)PaperKind.Custom;
            NewSize.Width = Width;
            NewSize.Height = Height;
            NewSize.PaperName = "Letter";

            return NewSize;

        }
        public static void _mPrint(double _pageWightInches = 8.27, double _pageHeightInches = 11.69, double _MarginTopInches = 0.025, double _MarginLeftInches = 0.025, double _MarginRightInches = 0.025, double _MarginBottomInches = 0.025)
        {
            if (m_streams == null || m_streams.Count == 0)
                throw new Exception("Error: no stream to print.");
            PrintDocument printDoc = new PrintDocument();

            PaperSize RequiredPaperSize = CalculatePaperSize(_pageWightInches * 2.54, _pageHeightInches * 2.54);
            bool FoundMatchingPaperSize = false;
            for (int index = 0; index < printDoc.PrinterSettings.PaperSizes.Count; index++)
            {
                if (printDoc.PrinterSettings.PaperSizes[index].Height == RequiredPaperSize.Height && printDoc.PrinterSettings.PaperSizes[index].Width == RequiredPaperSize.Width)
                {
                    printDoc.PrinterSettings.DefaultPageSettings.PaperSize = printDoc.PrinterSettings.PaperSizes[index];
                    printDoc.DefaultPageSettings.PaperSize = printDoc.PrinterSettings.PaperSizes[index];
                    FoundMatchingPaperSize = true;
                    break;
                }
            }


            if (!printDoc.PrinterSettings.IsValid)
            {
                throw new Exception("Error: cannot find the default printer.");
            }
            else
            {

                printDoc.PrintPage += new PrintPageEventHandler(_mPrintPage);
                m_currentPageIndex = 0;
                printDoc.Print();
            }
        }
        public static void _mPrintToPrinter(this LocalReport report)
        {
            _mExport(report);
        }
        public static void _mDisposePrint()
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
                m_streams = null;
            }
        }
    }

打印按钮代码

PrintViewer _PJobEntry = new PrintViewer();
DataTable dt = new DataSet1.MainTableDataTable();
dt.Rows.Add('vales for dataset');
_PJobEntry._RptView.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
_PJobEntry._RptView.LocalReport.ReportEmbeddedResource = "WAINFOBUSSOLN.Printing.RptSaleInvoice02.rdlc";
_PJobEntry._RptView.SetDisplayMode(DisplayMode.PrintLayout);
_cWainfoPrintReport._mExport(_PJobEntry._RptView.LocalReport, true, 8.27, 11.69, 0.25, 0.25, 0.28, 0.25);

但它也像上面描述的那样打印

同一应用程序在我的 Windows 10 64 位 PC 上运行,但在部署到具有 Windows 10 64 位的客户端 PC 上后无法运行

在客户端 PC 上安装 .Net 4.0 框架、SQL Server 2008、Windows 10 64 位

如何解决。

最佳答案

我认为此问题与安装的任何工具无关,或者仅与 Windows 10 64 位有关。

相反,这是 jdweng 和 Reza Aghaei 提到的 DPI 问题。更具体地说,是高 DPI 设备。

不确定您是否注意到,但是您上传的两张图片的像素不同,低像素的图片显示了报告的正确呈现。这可以说支持了由高 DPI 引起的缩放问题的观点。

High-DPI_Scaling_Issue

现在,有很多关于此的文章、帖子和问题。但是,微软支持门户网站上的一个非常接近于为受害者提供某种正确方向的人似乎没有为这种行为提供的可能的解决方案(是的,复数)和解决方法(再次,复数)。

您可以在此处找到这篇文章:https://support.microsoft.com/en-au/help/3025083/windows-scaling-issues-for-high-dpi-devices

我相信,我在下面引用的解决方法暂时可以帮助您。

Change application properties

In Explorer or on the Start menu, right-click the application name, select Properties, select the Compatibility tab, and then select the Disable display scaling on high DPI settings check box.

Note: In Windows 10 Version 1703 and later version of Windows, the text of the Disable display scaling on high DPI settings option is changed to Override high DPI scaling behavior, scaling performed by: Application.

关于c# - Visual Studio 2015 社区报告查看器版本 12 使用 C# 获取额外 margin 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51946537/

相关文章:

printing - 尚未指定运行报告所需的一个或多个参数

c# - 如何阻止刷新值?

c# - StructureMap:选择嵌套依赖的具体类型

sql-server - 计算列定义为空

asp.net - 有哪些学习 rdlc 报表设计的好教程网站?

c# - 消除本地报告额外的空白页

c# - 是否可以指定代码契约以确保该方法不会更改对象的状态

c# - 抑制警告 CS1998 : This async method lacks 'await'

sql-server - T-SQL - 文本字段到 nvarchar

php - Laravel如何调用sql server中的函数