none
ResportViewer: set paper size programatically RRS feed

  • Question

  • Hi all,
    I'm developing a Windows application in C# and I have a problem with the ReportViewer.
    I want to set the page size of the report to the default page size of the selected printer before printing. How can I do this?
    The situation is the following: a report is printed on 2 diffrent printers by the clients, a laser printer (default paper size A4) and a dot matrix printer (default paper size Fanfold 8.5inx12in). Depending on the printer the user selects, I want to automatically set the page size. The page size is awlays Letter and this causes the dot matrix printer to crash :(. Please help!
    Friday, August 22, 2008 6:09 AM

Answers

  • I managed to solve my problem by impelementing a workaround. I found help in this thread:
    http://forums.msdn.microsoft.com/en-US/vsreportcontrols/thread/9f52d79d-5baf-4e84-97d5-7dbba6623b89/
    To anyone who may face the same problem, here is my code:

    public class CPrintManager : IDisposable   
      {   
        private int m_currentPageIndex;   
        private IList<Stream> m_streams;   
        private DataTable LoadSalesData()   
        {  
          DataSet dataSet = new DataSet();   
          dataSet.ReadXml("data.xml");   
          return dataSet.Tables[0];   
        }  
        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)   
        {   
          Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);   
          m_streams.Add(stream);   
          return stream;   
        }  
        private void Export(LocalReport report, double dPageWidth, double dPageHeigth)   
        {   
          dPageWidth = Math.Round(dPageWidth, 2);  
          dPageHeigth = Math.Round(dPageHeigth, 2);  
          string deviceInfo = "<DeviceInfo>" +   
            "  <OutputFormat>EMF</OutputFormat>" +   
            "  <PageWidth>{0}in</PageWidth>" +   
            "  <PageHeight>{1}in</PageHeight>" +   
            "  <MarginTop>0.25in</MarginTop>" +   
            "  <MarginLeft>0.25in</MarginLeft>" +   
            "  <MarginRight>0.25in</MarginRight>" +   
            "  <MarginBottom>0.25in</MarginBottom>" +   
            "</DeviceInfo>";  
          deviceInfo = String.Format(deviceInfo, CDataMange.GetDoubleSQLPretty(dPageWidth), CDataMange.GetDoubleSQLPretty(dPageHeigth));  
          Warning[] warnings;  
          m_streams = new List<Stream>();   
          report.Render("Image", deviceInfo, CreateStream, out warnings);   
          foreach (Stream stream in m_streams)    
            stream.Position = 0;  
        }  
     
        public void Print(LocalReport report)   
        {   
          PrintDialog dlg = new PrintDialog();  
          DialogResult res = dlg.ShowDialog();  
          if (res == DialogResult.Cancel)  
            return;  
            
          //get the default printer settings.   
          this.Export(report, dlg.PrinterSettings.DefaultPageSettings.PaperSize.Width/100.0,   
            dlg.PrinterSettings.DefaultPageSettings.PaperSize.Height/100.0);  
          this.m_currentPageIndex = 0;  
          if (m_streams == null || m_streams.Count == 0)           
            return;  
     
          PrintDocument printDoc = new PrintDocument();  
          printDoc.PrinterSettings = dlg.PrinterSettings;  
          if (!printDoc.PrinterSettings.IsValid)  
          {  
            CErrorHandling.WriteError("The printer settings are not valid! ""CPinting.Print");  
            MessageBox.Show("Error! """, MessageBoxButtons.OK, MessageBoxIcon.Error);   
            return;  
          }  
          printDoc.PrintPage += new PrintPageEventHandler(PrintPage);  
          printDoc.Print();  
        }   
          
        private void PrintPage(object sender, PrintPageEventArgs ev)   
        {  
          Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);   
          ev.Graphics.DrawImage(pageImage, 0, 0);  
          m_currentPageIndex++;   
          ev.HasMorePages = (m_currentPageIndex < m_streams.Count);  
        }  
     
        public void Dispose()   
        {   
          if (m_streams != null)   
          {   
            foreach (Stream stream in m_streams)     
              stream.Close();   
          }   
        }  
      } 
    I call the Print method of the class from the Print event of the ReportViewer, passing the LocalReport:
    private void reportViewer1_Print(object sender, CancelEventArgs e)  
        {  
          e.Cancel = true;  
          CPrintManager obj = new CPrintManager();  
          obj.Print(this.reportViewer1.LocalReport);  
        } 
    And that's it :)
    • Marked as answer by IvaKoleva Friday, August 22, 2008 1:54 PM
    Friday, August 22, 2008 11:47 AM

All replies

  • Hello IvaKoleva,

    IMHO it is not possible to change the size of the Page after the Reportviewer has loaded the Report.
    You only can change the size by editing the size in the RDLC-File before the Report is loaded.



    BanditTreiber
    Friday, August 22, 2008 6:54 AM
  • I managed to solve my problem by impelementing a workaround. I found help in this thread:
    http://forums.msdn.microsoft.com/en-US/vsreportcontrols/thread/9f52d79d-5baf-4e84-97d5-7dbba6623b89/
    To anyone who may face the same problem, here is my code:

    public class CPrintManager : IDisposable   
      {   
        private int m_currentPageIndex;   
        private IList<Stream> m_streams;   
        private DataTable LoadSalesData()   
        {  
          DataSet dataSet = new DataSet();   
          dataSet.ReadXml("data.xml");   
          return dataSet.Tables[0];   
        }  
        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)   
        {   
          Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);   
          m_streams.Add(stream);   
          return stream;   
        }  
        private void Export(LocalReport report, double dPageWidth, double dPageHeigth)   
        {   
          dPageWidth = Math.Round(dPageWidth, 2);  
          dPageHeigth = Math.Round(dPageHeigth, 2);  
          string deviceInfo = "<DeviceInfo>" +   
            "  <OutputFormat>EMF</OutputFormat>" +   
            "  <PageWidth>{0}in</PageWidth>" +   
            "  <PageHeight>{1}in</PageHeight>" +   
            "  <MarginTop>0.25in</MarginTop>" +   
            "  <MarginLeft>0.25in</MarginLeft>" +   
            "  <MarginRight>0.25in</MarginRight>" +   
            "  <MarginBottom>0.25in</MarginBottom>" +   
            "</DeviceInfo>";  
          deviceInfo = String.Format(deviceInfo, CDataMange.GetDoubleSQLPretty(dPageWidth), CDataMange.GetDoubleSQLPretty(dPageHeigth));  
          Warning[] warnings;  
          m_streams = new List<Stream>();   
          report.Render("Image", deviceInfo, CreateStream, out warnings);   
          foreach (Stream stream in m_streams)    
            stream.Position = 0;  
        }  
     
        public void Print(LocalReport report)   
        {   
          PrintDialog dlg = new PrintDialog();  
          DialogResult res = dlg.ShowDialog();  
          if (res == DialogResult.Cancel)  
            return;  
            
          //get the default printer settings.   
          this.Export(report, dlg.PrinterSettings.DefaultPageSettings.PaperSize.Width/100.0,   
            dlg.PrinterSettings.DefaultPageSettings.PaperSize.Height/100.0);  
          this.m_currentPageIndex = 0;  
          if (m_streams == null || m_streams.Count == 0)           
            return;  
     
          PrintDocument printDoc = new PrintDocument();  
          printDoc.PrinterSettings = dlg.PrinterSettings;  
          if (!printDoc.PrinterSettings.IsValid)  
          {  
            CErrorHandling.WriteError("The printer settings are not valid! ""CPinting.Print");  
            MessageBox.Show("Error! """, MessageBoxButtons.OK, MessageBoxIcon.Error);   
            return;  
          }  
          printDoc.PrintPage += new PrintPageEventHandler(PrintPage);  
          printDoc.Print();  
        }   
          
        private void PrintPage(object sender, PrintPageEventArgs ev)   
        {  
          Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);   
          ev.Graphics.DrawImage(pageImage, 0, 0);  
          m_currentPageIndex++;   
          ev.HasMorePages = (m_currentPageIndex < m_streams.Count);  
        }  
     
        public void Dispose()   
        {   
          if (m_streams != null)   
          {   
            foreach (Stream stream in m_streams)     
              stream.Close();   
          }   
        }  
      } 
    I call the Print method of the class from the Print event of the ReportViewer, passing the LocalReport:
    private void reportViewer1_Print(object sender, CancelEventArgs e)  
        {  
          e.Cancel = true;  
          CPrintManager obj = new CPrintManager();  
          obj.Print(this.reportViewer1.LocalReport);  
        } 
    And that's it :)
    • Marked as answer by IvaKoleva Friday, August 22, 2008 1:54 PM
    Friday, August 22, 2008 11:47 AM