【问题标题】:Printing landscape/portrait in rdlc without preview在没有预览的情况下在 rdlc 中打印横向/纵向
【发布时间】:2014-02-22 16:09:27
【问题描述】:

我正在尝试以横向或纵向打印本地报告。

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; }
}

我有 2 份不同的报告,一份是纵向模式,一份是横向模式,但我为 PageWidth 和 PageSize 更改什么值并不重要,它总是以纵向打印。 我在 11.7 英寸和 8.3 英寸之间交换了宽度和高度,但它始终以纵向模式打印。

【问题讨论】:

    标签: c# printing render rdlc localreport


    【解决方案1】:

    您可以使用“报告”(LocalReport/ServerReport)中的“GetDefaultPageSettings()”来执行此操作 并从 reportviewer 内部窃取此代码:

    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";
    }
    

    这样您就可以获得报表定义中设置的页面方向和边距。

    【讨论】:

    • LocalReport 没有 PageSettings 属性 - 你从哪里得到的?它有一个GetDefaultPageSettings 方法,但是该方法返回的数据是只读的。
    • @ekolis 正如我所说,我“从 ReportViewer 内部窃取了它”。 Microsoft.Reporting.WinForms.ReportViewer 将“PageSettings”作为私有属性。你的对象应该在某个地方有这样的属性可以使用。
    【解决方案2】:

    您可以使用ReportPageSettings.IsLandscape 属性来验证报表是否定义为横向(报表属性> 页面设置> 方向)。

    如果是横向,您需要在 DeviceInfo 声明中交换纸张宽度和纸张高度。

    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
    

    如果您使用PrintDocument,您还需要相应地更改PageSettings.Landscape 属性。

    Dim printDoc As New PrintDocument
    printDoc.DefaultPageSettings.Landscape = rdlLocalReport.GetDefaultPageSettings.IsLandscape
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 2013-05-24
      相关资源
      最近更新 更多