title: Printing RDLC Report directly without displaying reportviewer
body: <p>I am working on WPF C# 4.0 project in which
  I am trying to print a RDLC report which is basically an invoice directly without displaying the ReportViewer. I refered code from the following <a href="http://msdn.microsoft.com/en-us/library/ms252091.aspx" rel="nofollow noreferrer">MSDN link</a> and modified it for my purpose.</p>
  
  <p>Following is the modified code which I am using.</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Data;
  using System.Text;
  using System.Drawing.Imaging;
  using System.Drawing.Printing;
  using System.Collections.Generic;
  using System.Windows.Forms;
  using Microsoft.Reporting.WinForms;
  using System.Xml;
  using System.Resources;
  using System.Drawing;
  using System.Reflection;
  
  namespace POS.Classes
  {
      public class DirectReportPrint : IDisposable
      {
          private double[] PageBounds = { 0,0,0,0,0,0};
          private int m_currentPageIndex;
          private IList&lt;Stream&gt; m_streams;
  
          private DataTable _table = new DataTable();
          /// &lt;summary&gt;
          /// Must set this to get the report data
          /// &lt;/summary&gt;
          public DataTable ReportTable
          {
              get { return _table; }
              set { _table = value; }
          }
  
          // Routine to provide to the report renderer, in order to
          //    save an image for each page of the report.
          private Stream CreateStream(string name,
            string fileNameExtension, Encoding encoding,
            string mimeType, bool willSeek)
          {
              //Stream stream = new FileStream(@"..\..\" + name +
              //   "." + fileNameExtension, FileMode.Create);
              Stream stream = new FileStream(Path.GetTempPath() + "\\" + name +
                 "." + fileNameExtension, FileMode.Create);
              m_streams.Add(stream);
              return stream;
          }
  
          private string ReadEmbeddedResource(string ResourceName)
          {
              var assembly = Assembly.GetExecutingAssembly();
              using (Stream stream = assembly.GetManifestResourceStream(ResourceName))
              using (StreamReader reader = new StreamReader(stream))
              {
                  string result = reader.ReadToEnd();
                  string temp = result.Replace('\r',' ');
                  return temp;
              }
          }
  
          private string ReadReportXML(string ReportName)
          {
              try
              {
                  string s = " ", temp = "", t = "";
                  int x, y, z;
                  string result = ReadEmbeddedResource(ReportName);
                  if (result.Contains("&lt;PageHeight&gt;") &amp;&amp; result.Contains("&lt;/PageHeight&gt;"))
                  {
                      x = result.IndexOf("&lt;PageHeight&gt;");
                      y = result.IndexOf("&lt;/PageHeight&gt;");
                      temp = result.Substring(x, y - x);
                      s += temp + "&lt;/PageHeight&gt;  ";
                      z = temp.IndexOf('&gt;') + 1;
                      t = temp.Substring(z, temp.Length - z);
                      PageBounds[0] = Math.Round(Convert.ToDouble(t.Substring(0, t.Length - 2)), 2);
                  }
                  if (result.Contains("&lt;PageWidth&gt;") &amp;&amp; result.Contains("&lt;/PageWidth&gt;"))
                  {
                      x = result.IndexOf("&lt;PageWidth&gt;");
                      y = result.IndexOf("&lt;/PageWidth&gt;");
                      temp = result.Substring(x, y - x);
                      s += temp + "&lt;/PageWidth&gt;  ";
                      z = temp.IndexOf('&gt;') + 1;
                      t = temp.Substring(z, temp.Length - z);
                      PageBounds[1] = Math.Round(Convert.ToDouble(t.Substring(0, t.Length - 2)), 2);
                  }
                  if (result.Contains("&lt;LeftMargin&gt;") &amp;&amp; result.Contains("&lt;/LeftMargin&gt;"))
                  {
                      x = result.IndexOf("&lt;LeftMargin&gt;");
                      y = result.IndexOf("&lt;/LeftMargin&gt;");
                      temp = result.Substring(x, y - x);
                      s += temp + "&lt;/LeftMargin&gt;  ";
                      z = temp.IndexOf('&gt;') + 1;
                      t = temp.Substring(z, temp.Length - z);
                      PageBounds[2] = Math.Round(Convert.ToDouble(t.Substring(0, t.Length - 2)), 2);
                  }
                  if (result.Contains("&lt;RightMargin&gt;") &amp;&amp; result.Contains("&lt;/RightMargin&gt;"))
                  {
                      x = result.IndexOf("&lt;RightMargin&gt;");
                      y = result.IndexOf("&lt;/RightMargin&gt;");
                      temp = result.Substring(x, y - x);
                      s += temp + "&lt;/RightMargin&gt;  ";
                      z = temp.IndexOf('&gt;') + 1;
                      t = temp.Substring(z, temp.Length - z);
                      PageBounds[3] = Math.Round(Convert.ToDouble(t.Substring(0, t.Length - 2)), 2);
                  }
                  if (result.Contains("&lt;TopMargin&gt;") &amp;&amp; result.Contains("&lt;/TopMargin&gt;"))
                  {
                      x = result.IndexOf("&lt;TopMargin&gt;");
                      y = result.IndexOf("&lt;/TopMargin&gt;");
                      temp = result.Substring(x, y - x);
                      s += temp + "&lt;/TopMargin&gt;  ";
                      z = temp.IndexOf('&gt;') + 1;
                      t = temp.Substring(z, temp.Length - z);
                      PageBounds[4] = Math.Round(Convert.ToDouble(t.Substring(0, t.Length - 2)), 2);
                  }
                  if (result.Contains("&lt;BottomMargin&gt;") &amp;&amp; result.Contains("&lt;/BottomMargin&gt;"))
                  {
                      x = result.IndexOf("&lt;BottomMargin&gt;");
                      y = result.IndexOf("&lt;/BottomMargin&gt;");
                      temp = result.Substring(x, y - x);
                      s += temp + "&lt;/BottomMargin&gt;  ";
                      z = temp.IndexOf('&gt;') + 1;
                      t = temp.Substring(z, temp.Length - z);
                      PageBounds[5] = Math.Round(Convert.ToDouble(t.Substring(0, t.Length - 2)), 2);
                  }
                  return s;
              }
              catch (Exception ex)
              {
                  return null;
              }
          }        
  
          // Export the given report as an EMF (Enhanced Metafile) file.
          private void Export(LocalReport report, string ReportName)
          {
              try
              {
                  string temp = ReadReportXML(ReportName);
                  if (temp != null)
                  {
                      string deviceInfo =
                        "&lt;DeviceInfo&gt;" +
                        "  &lt;OutputFormat&gt;EMF&lt;/OutputFormat&gt;"
                        + temp +
                        "&lt;/DeviceInfo&gt;";
  
                      Warning[] warnings;
                      m_streams = new List&lt;Stream&gt;();
                      report.Render("Image", deviceInfo, CreateStream,
                         out warnings);
                      foreach (Stream stream in m_streams)
                          stream.Position = 0;
                  }
                  else
                  {
                      throw new Exception("Something went wrong. Unable to Print Report");
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
                  MessageBox.Show(ex.InnerException.Message);
              }
          }
  
          // Handler for PrintPageEvents
          private void PrintPage(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 &lt; m_streams.Count);
          }
  
          private void Print()
          {
              PrinterSettings settings = new PrinterSettings();
  
              if (m_streams == null || m_streams.Count == 0)
                  return;
              PrintDocument printDoc = new PrintDocument();
              printDoc.PrinterSettings.PrinterName = settings.PrinterName;
  
              PaperSize CustomSize = new PaperSize("Custom", (int)PageBounds[1]*100, (int)PageBounds[0]*100);
              CustomSize.RawKind = (int)PaperKind.Custom;
              printDoc.DefaultPageSettings.PaperSize = CustomSize;
  
  
              if (!printDoc.PrinterSettings.IsValid)
              {
                  string msg = String.Format(
                     "Can't find printer \"{0}\".", settings.PrinterName);
                  MessageBox.Show(msg, "Print Error");
                  return;
              }
              printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
              printDoc.Print();
          }
  
          /// &lt;summary&gt;
          ///  Create a local report for Report.rdlc, load the data,
          ///  export the report to an .emf file, and print it.
          /// Prints the report directly
          /// &lt;/summary&gt;
          /// &lt;param name="ReportName"&gt;Name of the report&lt;/param&gt;
          /// &lt;param name="DS_Name"&gt;Dataset name for the report. Provide the name which was used during RDLC file creation&lt;/param&gt;
          public void Run(string ReportName, string DS_Name)
          {
              LocalReport report = new LocalReport();
              report.ReportEmbeddedResource = ReportName;
              report.DataSources.Add(new ReportDataSource(DS_Name, ReportTable));
              Export(report, ReportName);
              m_currentPageIndex = 0;
              Print();
              Dispose();
          }
  
          public void Dispose()
          {
              if (m_streams != null)
              {
                  foreach (Stream stream in m_streams)
                      stream.Close();
                  m_streams = null;
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Code which Invokes Run Method</strong></p>
  
  <pre><code>Classes.DirectReportPrint drp = new Classes.DirectReportPrint();
  drp.ReportTable = Classes.Class_Sales_Bill.GetBillDetails(this.saleObj.Row_ID);
  drp.Run("POS.Reports.Rpt_Anil_Sale_Bill.rdlc", "DataSet1");
  </code></pre>
  
  <p>This code works well when I am using Visual Studio debugger to execute it. However, it fails if I publish my project, install its setup and then try to run its exe <code>(Weird Behaviour)</code>.</p>
  
  <p>If I run exe it works for the first invoice which I try to print. For the subsequent invoices I get the following <strong>error</strong>. </p>
  
  <pre><code>An error occurred during local report processing. Access to the Path 'c:\Users\Rpt_Anil_Sale_Bill_0.EMF' is denied.
  </code></pre>
  
  <p>Here <code>Rpt_Anil_Sale_Bill</code> is the name of my RDLC file. </p>
  
  <p>On further digging I identified that the exe code creates an intermediate EMF file in this path <code>C:\Users\Nilesh\AppData\Local\Apps\2.0\BWQ6XD5A.DB0</code></p>
  
  <p><img src="https://i.stack.imgur.com/ef544.png" alt="enter image description here"></p>
  
  <p><strong>If I try to open this file I get the following error:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/QRj72.png" alt="enter image description here"></p>
  
  <p>Need help in identifying what's going wrong with the code.</p>
  
  <p>Any help is appreciated. Thanks!!</p>