2

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 am using this link as a guide to print SSRS reports automatically from a Windows Forms application.

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.

0

1 Answer 1

3
+50

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

9
  • I am about to try this out now. Commented Aug 15, 2018 at 12:16
  • So I encountered a few hiccups in this solution: 1. I have a separate class with the Print functionalities in it and this class does not have a constructor for ReportViewer. Just Report, LocalReport, and ServerReport. I tried doing it on the ServerReport constructor AND the Report constructor. Neither have the PrinterSettings property. 2. reportViewer1.PrinterSettings.PaperSizes does not have a cast property. Continued in next comment.... Commented Aug 15, 2018 at 13:30
  • But I used yours as an example to create a separate PaperSize variable and if the report is landscape, I set the PaperSize variable width and height accordingly and use this variable for my printing purposes. It now prints in landscape. The only problem I have is, the report is getting squeezed in a bit. My actual report is designed to fit a landscape paper exactly, but for some reason, the application squeezes it in. I can look at further and figure out what's going on now. Thank you so much for bringing me this far! :) Commented Aug 15, 2018 at 13:31
  • I didn't have any problem printing the report. I just added another way of creating the PageSetting based on the default page setting of the report, since you have setup the default page setting to be landscape, you can use it. (Just for your information Cast is a linq extension method in System.Linq namespace. Commented Aug 15, 2018 at 16:51
  • Also I created an extension method called Print for LocalReport. You can easily use Print() or Print(PageSettings). You can find it here. Commented Aug 15, 2018 at 16:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.