I have a Windows Forms application that has a textbox
, a button
, and 3 ReportViewer
. The 3 ReportViewer
boxes are hidden. When you enter a ShopOrder
into the textbox and click on the button, it will automatically pass the Shop order value as a parameter to all 3 reports, render the report, and once the rendering is complete, render the report as EMF file, print the report.
I have a few variances in my application because I am using ServerReports
in my ReportViewer
and not LocalReport
. But after all these changes, my application prints them all out with no problems.
But the only issue I have is, I am not able to set my page orientation to Landscape
, even though the orientation on my report is Landscape.
So I thought maybe I need to set the deviceInfo
variable's PageWidth
and PageHeight
variables accordingly, so this is what the deviceInfo
variable has:
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>";
I have two Export
functions: Export
and ExportLandscape
. The code snippet above is a part of ExportLandscape
. When I call ExportLandscape
, my report still prints in Portrait.
I tried just completely removing the Page setup options from my DeviceInfo
variable and made it just say the OutputFormat
. That didn't do it either.
Is there anything else I need to change for my report to print in Landscape? What am I missing?
It is also worth noting that, out of my 3 reports, 2 of them print in Landscape and 1 prints in Portrait. So I would really like for my application to just print it in whatever page settings the report is in. I just tried getting the report's page size and report's margins and setting those to my DeviceInfo
variable as suggested here. STILL NO LUCK!!
I just tried adding a breakpoint at the Export(ReportViewer report)
function and stepped through. When I get the report.ServerReport.GetDefaultPageSettings().PaperSize
in the immediate window, I see this:
{[PaperSize Letter Kind=Letter Height=1100 Width=850]}
Height: 1100
Kind: Letter
PaperName: "Letter"
RawKind: 1
Width: 850
This makes me feel like even though my report is set to landscape (height = 8.5in and Width = 11in), my application does not seem to recognize it.
Important Update:
The printer I am printing to has 2 paper trays. When I print a portrait report, it takes it from the default tray with the default paper size (tray 2). But when my application sends the landscape report to print, the printer tries to get a paper out of tray 1. When I load tray 1 with the same paper that is in tray 2, it asks me to enter a width and height of the paper. The printer does not seem to understand when I tell it to print it in landscape. Or rather, the printer thinks this is some new setting that it does not know about. When I enter 11 for width and 8.5 for height, it prints landscape data on a portrait paper.
To make myself clearer, the data is getting printed with a width of 11 and height of 8.5. AKA, only 75% of the data gets printed. The rest gets pushed out of the page because the page is still being oriented in portrait.
You need to use a suitable PageSettings
for the PrintDocument
which is used for printing. You need to apply some changes to code of that article to be able to print in different paper size or page layout.
First you need to create a suitable PageSettings
, for example if you have set the default page setting for your report to be landscape:
var report = reportViewer1.LocalReport;
var pageSettings = new PageSettings();
pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
pageSettings.Margins = report.GetDefaultPageSettings().Margins;
or if you want to create a new page setting:
var pageSettings = new PageSettings();
pageSettings.Landscape = true;
pageSettings.PaperSize = reportViewer1.PrinterSettings.PaperSizes.Cast<PaperSize>()
.Where(x => x.Kind == PaperKind.A4).First();
Then use the pageSetting
when creating the deviceInfo:
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>";
And at last, use the same pageSettings with PrintDocument
:
PrintDocument printDoc = new PrintDocument();
printDoc.DefaultPageSettings = pageSettings;
I've created an extension method to make it easier to print the report easily by calling Print()
or Print(PageSettings)
. You can find it here: Print RDLC Report without showing the ReportViewer