4

I am trying to print a local report in either landscape or portrait.

private void Export(LocalReport report)
{
    Warning[] warnings;
    m_streams = new List<Stream>();

    var deviceInfo = new StringBuilder();
    deviceInfo.AppendLine("<DeviceInfo>");
    deviceInfo.AppendLine("<OutputFormat>EMF</OutputFormat>");
    //"11.7in", "8.3in"
    deviceInfo.AppendLine("<PageWidth>11.7in</PageWidth>");
    deviceInfo.AppendLine("<PageHeight>8.3in</PageHeight>");

    deviceInfo.AppendLine("</DeviceInfo>");

    report.Render("Image", deviceInfo.ToString(), CreateStream, out warnings);            
    foreach (var stream in m_streams) { stream.Position = 0; }
}

I have 2 different reports one in portrait mode and one in landscape mode but it doesn't matter what values I change for PageWidth and PageSize, its always printing in portrait. I've swapped width and height between 11.7in and 8.3in but its always printing in portrait mode.

Cœur
  • 37,241
  • 25
  • 195
  • 267
wakthar
  • 720
  • 1
  • 8
  • 21

2 Answers2

1

You can use ReportPageSettings.IsLandscape property to verify if the report is defined as landscape (Report Properties > Page Setup > Orientation).

If landscape you need to swap paper width and paper height in your DeviceInfo declaration.

Dim rdlLocalReport As New LocalReport
Dim strDeviceInfo As String

With rdlLocalReport.GetDefaultPageSettings

    Dim intPaperSizeWidth As Integer = 0
    Dim intPaperSizeHeight As Integer = 0

    If .IsLandscape Then
        intPaperSizeWidth = .PaperSize.Height
        intPaperSizeHeight = .PaperSize.Width
    Else
        intPaperSizeWidth = .PaperSize.Width
        intPaperSizeHeight = .PaperSize.Height
    End If

    strDeviceInfo = "<DeviceInfo>" _
        & "<OutputFormat>EMF</OutputFormat>" _
        & "<PageWidth>" & Strings.Replace(intPaperSizeWidth / 100, ",", ".") & "in</PageWidth>" _
        & "<PageHeight>" & Strings.Replace(intPaperSizeHeight / 100, ",", ".") & "in</PageHeight>" _
        & "<MarginTop>" & Strings.Replace(.Margins.Top / 100, ",", ".") & "in</MarginTop>" _
        & "<MarginLeft>" & Strings.Replace(.Margins.Left / 100, ",", ".") & "in</MarginLeft>" _
        & "<MarginRight>" & Strings.Replace(.Margins.Right / 100, ",", ".") & "in</MarginRight>" _
        & "<MarginBottom>" & Strings.Replace(.Margins.Bottom / 100, ",", ".") & "in</MarginBottom>" _
        & "</DeviceInfo>"

End With

If you use PrintDocument you also need to accordingly change PageSettings.Landscape property.

Dim printDoc As New PrintDocument
printDoc.DefaultPageSettings.Landscape = rdlLocalReport.GetDefaultPageSettings.IsLandscape
tezzo
  • 10,858
  • 1
  • 25
  • 48
0

You can do this using "GetDefaultPageSettings()" from "Report" (LocalReport/ServerReport) and stealing this code from the reportviewer internals:

private string CreateEMFDeviceInfo(int startPage, int endPage)
{
    string text = "";
    PageSettings pageSettings = PageSettings;
    int hundrethsOfInch = pageSettings.Landscape ? pageSettings.PaperSize.Height : pageSettings.PaperSize.Width;
    int hundrethsOfInch2 = pageSettings.Landscape ? pageSettings.PaperSize.Width : pageSettings.PaperSize.Height;
    text = string.Format(CultureInfo.InvariantCulture, "<MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth>", ToInches(pageSettings.Margins.Top), ToInches(pageSettings.Margins.Left), ToInches(pageSettings.Margins.Right), ToInches(pageSettings.Margins.Bottom), ToInches(hundrethsOfInch2), ToInches(hundrethsOfInch));
    return string.Format(CultureInfo.InvariantCulture, "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>{0}</StartPage><EndPage>{1}</EndPage>{2}</DeviceInfo>", startPage, endPage, text);
}

private static string ToInches(int hundrethsOfInch)
{
    return ((double)hundrethsOfInch / 100.0).ToString(CultureInfo.InvariantCulture) + "in";
}

That way you get the page orientation and margins as set in the report definition.

Roger Willcocks
  • 1,649
  • 13
  • 27
  • `LocalReport` does not have a `PageSettings` property - where are you getting that from? It has a `GetDefaultPageSettings` method, but the data returned by that method is read only. – ekolis Oct 30 '19 at 18:23
  • @ekolis I "stole it from the ReportViewer internals" as I said. Microsoft.Reporting.WinForms.ReportViewer has "PageSettings" as a private property. Your object should have such a property somewhere to work with. – Roger Willcocks Nov 01 '19 at 06:05