在横向模式下打印ReportServer时如何避免拉伸和挤压?

时间:2019-02-09 07:03:53

标签: c# reporting-services printing landscape stretched

我正在使用ReportPrinting类(继承PrintDocument类)来打印ServerReport而不进行预览。当我以纵向模式打印报告时,报告打印效果很好。 但是,当我设置横向模式(PageSettings.Landscape设置为true)时,打印的报告将水平拉伸并垂直挤压。

我发现了这个Printing an SSRS report in Landscape mode from Windows Forms application问题,作者提到其中的印刷报告受到挤压,但没有描述发现的灵魂。

我的问题是:

1)什么会导致拉伸和挤压打印的报告问题?

2)我该如何解决?

我不确定这是否重要,但是还有一个问题:尽管ServerReport.GetDefaultPageSettings()。IsLandscape返回true,并且我使用此属性初始化m_pageSettings。以纵向模式打印报告。作为临时解决方案,我以硬编码方式将此属性设置为true。

背景:

  • Windows Server 2012 R2标准版

  • .NET Framework 4

  • MS SQL Server 2016(SP1-CU8)

  • MS Visual Studio 2010

我已经尝试过的内容

我将OutputFormat标记值(在CreateEMFDeviceInfo()中)更改为TIFF,JPEG和TIFF,但是尚未打印报告。 我将格式参数值(在ServerReport.Render()中)更改为PDF和EXCEL,但是它不能解决拉伸和挤压问题。

using System;

class ReportPrinting : PrintDocument
{
    private PageSettings m_pageSettings;
    private int m_currentPage;
    private List<Stream> m_pages = new List<Stream>();

    public ReportPrinting(ServerReport serverReport)
        : this((Report)serverReport)
    {
        RenderAllServerReportPages(serverReport);

        PrintController = new System.Drawing.Printing.StandardPrintController();
    }

    public ReportPrinting(LocalReport localReport)
        : this((Report)localReport)
    {
        RenderAllLocalReportPages(localReport);
    }

    private ReportPrinting(Report report)
    {
        ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

        m_pageSettings = new PageSettings();
        m_pageSettings.PaperSize = reportPageSettings.PaperSize;
        m_pageSettings.Margins = reportPageSettings.Margins;

        m_pageSettings.Landscape = true; 
        //m_pageSettings.Landscape =  reportPageSettings.IsLandscape;     }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (disposing)
        {
            foreach (Stream s in m_pages)
            {
                s.Dispose();
            }

            m_pages.Clear();
        }
    }

    protected override void OnBeginPrint(PrintEventArgs e)
    {
        base.OnBeginPrint(e);

        m_currentPage = 0;
    }

    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        base.OnPrintPage(e);

        Stream pageToPrint = m_pages[m_currentPage];
        pageToPrint.Position = 0;

        using (Metafile pageMetaFile = new Metafile(pageToPrint))
        {
            Rectangle adjustedRect = new Rectangle(
                    e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                    e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                    e.PageBounds.Width,
                    e.PageBounds.Height);

            e.Graphics.FillRectangle(Brushes.White, adjustedRect);

            e.Graphics.DraDrawImage(pageMetaFile, adjustedRect);

            m_currentPage++;
            e.HasMorePages = m_currentPage < m_pages.Count;
        }
    }

    protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
    {
        e.PageSettings = (PageSettings)m_pageSettings.Clone();
    }

    private void RenderAllServerReportPages(ServerReport serverReport)
    {

            string deviceInfo = CreateEMFDeviceInfo();

            NameValueCollection firstPageParameters = new NameValueCollection();
            firstPageParameters.Add("rs:PersistStreams", "True");

            NameValueCollection nonFirstPageParameters = new NameValueCollection();
            nonFirstPageParameters.Add("rs:GetNextStream", "True");

            string mimeType;
            string fileExtension;


            Stream pageStream = serverReport.Render("IMAGE", deviceInfo, firstPageParameters, out mimeType, out fileExtension);

            while (pageStream.Length > 0)
            {
                m_pages.Add(pageStream);

                pageStream = serverReport.Render("IMAGE", deviceInfo, nonFirstPageParameters, out mimeType, out fileExtension);
            }
    }

    private void RenderAllLocalReportPages(LocalReport localReport)
    {
        string deviceInfo = CreateEMFDeviceInfo();

        Warning[] warnings;

        localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out warnings);
    }

    private Stream LocalReportCreateStreamCallback(
        string name,
        string extension,
        Encoding encoding,
        string mimeType,
        bool willSeek)
    {
        MemoryStream stream = new MemoryStream();
        m_pages.Add(stream);

        return stream;
    }

    private string CreateEMFDeviceInfo()
    {
        PaperSize paperSize = m_pageSettings.PaperSize;
        Margins margins = m_pageSettings.Margins;

        return string.Format(
            CultureInfo.InvariantCulture,
            "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>",
            ToInches(margins.Top),
            ToInches(margins.Left),
            ToInches(margins.Right),
            ToInches(margins.Bottom),
            ToInches(paperSize.Height),
            ToInches(paperSize.Width));
    }

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

0 个答案:

没有答案