gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:00:11 27 4
gpt4 key购买 nike

我有一个 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。但在所有这些更改之后,我的应用程序可以毫无问题地将它们全部打印出来。

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

所以我想也许我需要相应地设置 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();

然后在创 build 备信息时使用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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com