title: How to print all pages in CSV file to 1 pdf file when user selects "Microsoft print to pdf"
body: <p>What: I have a program which prints an RDLC report page using a CSV file when using a printer.</p>
  <p>Issue: When the user prints to pdf, it saves each page 1 by 1 which is not efficent when there are 1000 pages to print.</p>
  <p>Intent: When user selects specifically microsoft print to pdf, all files will print to 1 single pdf file. However other printers will print as usual.</p>
  <p>Current situation: When printing to microsoft print to pdf, it prints to 1 file at a time, so 1000 records = 1000 files.</p>
  <p>Below is the source code for the printer class and main class where the printer class is called</p>
  <p>LocalReportExtensioncs.cs file</p>
  <p>this file runs for every page so if there is 1000 pages to print, it will run 1000 times.</p>
  <pre><code>    public static void PrintToPrinter(this LocalReport report)
      {
          var pageSettings = new PageSettings();
          pageSettings.Color = false;
          pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
          pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
          pageSettings.Margins = report.GetDefaultPageSettings().Margins;
          pageSettings.Landscape = true;
          Print(report, pageSettings);
      }
      public static void Print(this LocalReport report, PageSettings pageSettings)
      {
          string deviceInfo =
              $@&quot;&lt;DeviceInfo&gt;
              &lt;OutputFormat&gt;EMF&lt;/OutputFormat&gt;
              &lt;PageWidth&gt;{pageSettings.PaperSize.Width * 100}in&lt;/PageWidth&gt;
              &lt;PageHeight&gt;{pageSettings.PaperSize.Height * 100}in&lt;/PageHeight&gt;
              &lt;MarginTop&gt;{pageSettings.Margins.Top * 100}in&lt;/MarginTop&gt;
              &lt;MarginLeft&gt;{pageSettings.Margins.Left * 100}in&lt;/MarginLeft&gt;
              &lt;MarginRight&gt;{pageSettings.Margins.Right * 100}in&lt;/MarginRight&gt;
              &lt;MarginBottom&gt;{pageSettings.Margins.Bottom * 100}in&lt;/MarginBottom&gt;
          &lt;/DeviceInfo&gt;&quot;;
          Warning[] warnings;
          var streams = new List&lt;Stream&gt;();
          var currentPageIndex = 0;
          report.Render(&quot;Image&quot;, deviceInfo,
              (name, fileNameExtension, encoding, mimeType, willSeek) =&gt;
              {
                  var stream = new MemoryStream();
                  streams.Add(stream);
                  return stream;
              }, out warnings);
          foreach (Stream stream in streams)
              stream.Position = 0;
          if (streams.Equals(null) || streams.Count == 0)
              throw new Exception(&quot;Error: no stream to print.&quot;);
          var printDocument = new PrintDocument();
          printDocument.DocumentName = &quot;default&quot;;
          var printerController = new StandardPrintController();
          printDocument.PrintController = printerController;
          printDocument.DefaultPageSettings.Color = false;
          printDocument.DefaultPageSettings = pageSettings;
          DialogResult result = DialogResult.Retry;
          while (!printDocument.PrinterSettings.IsValid)
          {
              result = MessageBox.Show(&quot;Error, Default printer hasnt been selected&quot;, &quot;Error&quot;, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
              if (result == DialogResult.Cancel)
              {
                  Environment.Exit(0);
              }
          }
          printDocument.PrintPage += (sender, e) =&gt;
          {
              Metafile pageImage = new Metafile(streams[currentPageIndex]);
              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.DrawImage(pageImage, adjustedRect);
              currentPageIndex++;
              e.HasMorePages = (currentPageIndex &lt; streams.Count);
              e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
          };
          printDocument.EndPrint += (Sender, e) =&gt;
          {
              if (streams != null)
              {
                  foreach (Stream stream in streams)
                      stream.Close();
                  streams = null;
              }
          };
          printDocument.PrinterSettings.PrinterName = Properties.Settings.Default.PrinterName;
          printDocument.Print();
      }
  }
  </code></pre>
  <p>In main.cs file where the RDLC report data is inserted,</p>
  <pre><code>                    localReport = AddToLocalReport(localReport);
                      localReport.ReportPath = Application.StartupPath + &quot;\\RDLCreport.rdlc&quot;;
                          for (int i = 1; i &lt; printCounter + 1; i++)
                          {
                              if (Properties.Settings.Default.canPrint == true)
                              {
                                  localReport.PrintToPrinter();
                              }
                          }
  </code></pre>