c# - 从 Windows 窗体应用程序以横向模式打印 SSRS 报告

标签 c# winforms reporting-services reportviewer

我有一个 Windows 窗体应用程序,其中有一个文本框、一个按钮和 3 个ReportViewer。 3 个 ReportViewer 框被隐藏。当您在文本框中输入 ShopOrder 并单击按钮时,它会自动将 Shop order 值作为参数传递给所有 3 个报表,渲染报表,渲染完成后渲染报表报告为 EMF 文件,打印报告。

I am using this link as a guide to print SSRS reports automatically from a Windows Forms application.

我的应用程序存在一些差异,因为我在 ReportViewer 中使用 ServerReports,而不是 LocalReport。但在完成所有这些更改之后,我的应用程序毫无问题地将它们全部打印出来。

但我遇到的唯一问题是,我无法将页面方向设置为横向,即使我的报告上的方向是横向。

所以我想也许我需要相应地设置 deviceInfo 变量的 PageWidthPageHeight 变量,所以这就是 deviceInfo 变量有:

string deviceInfo =
    @"<DeviceInfo>
        <OutputFormat>EMF</OutputFormat>
        <PageWidth>11in</PageWidth>
        <PageHeight>8.5in</PageHeight>
        <MarginTop>0.25in</MarginTop>
        <MarginLeft>0.25in</MarginLeft>
        <MarginRight>0.25in</MarginRight>
        <MarginBottom>0.25in</MarginBottom>
    </DeviceInfo>";

我有两个 Export 函数:ExportExportLandscape。上面的代码片段是 ExportLandscape 的一部分。当我调用 ExportLandscape 时,我的报告仍以纵向打印。

我尝试从 DeviceInfo 变量中完全删除页面设置选项,并使其仅显示 OutputFormat。那也没有做到。

为了让我的报告以横向方式打印,我还需要更改什么吗?我缺少什么?

还值得注意的是,在我的 3 份报告中,其中 2 份以横向打印,1 份以纵向打印。因此,我真的希望我的应用程序能够在报告所在的任何页面设置中打印它。我只是尝试获取报告的页面大小和报告的边距,并将它们设置为我的 DeviceInfo 变量 as suggested here 。还是没有运气!

我刚刚尝试在 Export(ReportViewer report) 函数中添加断点并单步执行。当我在立即窗口中获取 report.ServerReport.GetDefaultPageSettings().PaperSize 时,我看到了以下内容:

{[PaperSize Letter Kind=Letter Height=1100 Width=850]}
    Height: 1100
    Kind: Letter
    PaperName: "Letter"
    RawKind: 1
    Width: 850

这让我感觉即使我的报告设置为横向(高度 = 8.5 英寸,宽度 = 11 英寸),我的应用程序似乎无法识别它。

重要更新:

我要打印的打印机有 2 个纸盘。当我打印纵向报告时,它会从具有默认纸张尺寸的默认纸盘(纸盘 2)中取出该报告。但是,当我的应用程序发送横向报告进行打印时,打印机尝试从纸盘 1 中取出纸张。当我在纸盘 1 中装入与纸盘 2 中相同的纸张时,它会要求我输入纸张的宽度和高度。当我告诉打印机横向打印时,打印机似乎不理解。或者更确切地说,打印机认为这是一些它不知道的新设置。当我输入宽度 11 和高度 8.5 时,它会在纵向纸张上打印横向数据。

为了让自己更清楚,数据打印的宽度为 11,高度为 8.5。也就是说,只有 75% 的数据被打印。其余部分被推出页面,因为页面仍然是纵向的。

最佳答案

您需要为用于打印的PrintDocument使用合适的PageSettings。您需要对 article 的代码进行一些更改能够以不同的纸张尺寸或页面布局进行打印。

首先,您需要创建一个合适的PageSettings,例如,如果您已将报告的默认页面设置设置为横向:

var report = reportViewer1.LocalReport;
var pageSettings = new PageSettings();
pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
pageSettings.Margins = report.GetDefaultPageSettings().Margins;

或者如果您想创建新的页面设置:

var pageSettings = new PageSettings();
pageSettings.Landscape = true;
pageSettings.PaperSize = reportViewer1.PrinterSettings.PaperSizes.Cast<PaperSize>()
    .Where(x => x.Kind == PaperKind.A4).First();

然后在创建deviceInfo时使用pageSetting:

string deviceInfo =
    $@"<DeviceInfo>
        <OutputFormat>EMF</OutputFormat>
        <PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
        <PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
        <MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
        <MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
        <MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
        <MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
    </DeviceInfo>";

最后,使用与 PrintDocument 相同的 pageSettings:

PrintDocument printDoc = new PrintDocument();
printDoc.DefaultPageSettings = pageSettings;

我创建了一个扩展方法,以便通过调用 Print()Print(PageSettings) 更轻松地打印报表。您可以在这里找到它:Print RDLC Report without showing the ReportViewer

关于c# - 从 Windows 窗体应用程序以横向模式打印 SSRS 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51753431/

相关文章:

c# - 如何使用 LINQ to XML 查询日期时间值?

winforms - 是否可以有一个窗口来插入由 powershell 制作的数据?

sql - SSRS 报告生成器 LENGTH 表达式和尺寸特定字段

reporting-services - 如何将数据标签添加到柱形图?

c# - 我可以使用 Microsoft Lync API 与 Communicator 2007/2007 R2 通信吗?

c# - 开发人员许可证错误 Windows RT(错误 0x90070005)

c# - Uncaught ReferenceError : __doPostBack is not defined

c# - 如何处理数组中的一组文本框/标签

c# - Windows窗体应用中的三层架构实现

excel - 如何避免excel,SSRS报告中较粗的边框宽度?