C#控制台应用程序中的SSRS报告未填充打印页面

时间:2017-04-18 01:48:45

标签: c# reporting-services

以下是VS2015 C#控制台应用程序的代码,其中我打印了8.5x14 SSRS报告(无数据连接)。当我从报表生成器打印报表时,它会正确呈现。当我从控制台应用程序打印时,报告不会填满页面。我将报告呈现为流,我将大小设置为8.5x14,然后打印它,将文档大小设置为8.5x14。不知道还需要做些什么才能让它适应。

如果有人愿意看看并提供建议,我将非常感激。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using Microsoft.Reporting.WinForms;
namespace SULabelPrinting
{
    class Program
    {
        private static int m_currentPageIndex;
        private static IList<Stream> m_streams;
        static void Main()
        {
            try
            {
                LocalReport report = new LocalReport();
                report.ReportPath = @"..\..\Test.rdl";
                Export(report);
                Print();
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
        }

        // Routine to provide to the report renderer, in order to
        //    save an image for each page of the report.
        private static Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
        {
            Stream stream = new MemoryStream();
            m_streams.Add(stream);
            return stream;
        }

        // Export the given report as an EMF (Enhanced Metafile) file.
        private static void Export(LocalReport report)
        {
            string deviceInfo =
              @"<DeviceInfo>
                <OutputFormat>EMF</OutputFormat>
                <PageWidth>14in</PageWidth>
                <PageHeight>8.5in</PageHeight>
                <MarginTop>.1in</MarginTop>
                <MarginLeft>.1in</MarginLeft>
                <MarginRight>.1in</MarginRight>
                <MarginBottom>.1in</MarginBottom>
            </DeviceInfo>";
            Warning[] warnings;
            m_streams = new List<Stream>();
            report.Render("Image", deviceInfo, CreateStream,
               out warnings);
            foreach (Stream stream in m_streams)
                stream.Position = 0;
        }

        // Handler for PrintPageEvents
        private static void PrintPageHandler(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new
               Metafile(m_streams[m_currentPageIndex]);

            // Adjust rectangular area with printer margins.
            Rectangle adjustedRect = new Rectangle(
                ev.PageBounds.Left - (int) ev.PageSettings.HardMarginX,
                ev.PageBounds.Top - (int) ev.PageSettings.HardMarginY,
                ev.PageBounds.Width,
                ev.PageBounds.Height);

            // Draw a white background for the report
            ev.Graphics.FillRectangle(Brushes.White, adjustedRect);

            // Draw the report content
            ev.Graphics.DrawImage(pageImage, adjustedRect);

            // Prepare for the next page. Make sure we haven't hit the end.
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }

        private static void Print()
        {
            if (m_streams == null || m_streams.Count == 0)
                throw new Exception("No stream to print.");

            PrintDocument printDoc = new PrintDocument();
            printDoc.DefaultPageSettings.PaperSize = new PaperSize("Legal", 850, 1400);
            printDoc.DefaultPageSettings.Landscape = true;
            printDoc.PrintPage += new PrintPageEventHandler(PrintPageHandler);
            m_currentPageIndex = 0;
            printDoc.Print();
        }

        public void Dispose()
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
                m_streams = null;
            }
        }
    }
}

如果有帮助,下面是项目的链接。 SULabelPrinting.zip

谢谢, 史蒂夫。

0 个答案:

没有答案