簡體   English   中英

在沒有預覽的情況下在rdlc中打印橫向/縱向

[英]Printing landscape/portrait in rdlc without preview

我試圖以橫向或縱向打印本地報告。

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

我有兩個不同的報告,一個是縱向模式,另一個是橫向模式,但是我為PageWidth和PageSize更改的值無關緊要,它始終以縱向打印。 我已經在11.7英寸和8.3英寸之間交換寬度和高度,但它始終以縱向模式打印。

您可以使用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

您可以使用“Report”(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";
}

這樣,您就可以獲得報表定義中設置的頁面方向和頁邊距。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM