title: How do I display a PDF using PdfSharp in ASP.Net MVC?
body: <p>We're making an ASP.Net MVC app that needs to be able to generate a PDF and display it to the screen or save it somewhere easy for the user to access. We're using PdfSharp to generate the document. Once it's finished, how do we let the user save the document or open it up in a reader? I'm especially confused because the PDF is generated server-side but we want it to show up client-side.</p>
  
  <hr>
  
  <p>Here is the MVC controller to create the report that we have written so far:</p>
  
  <pre><code>public class ReportController : ApiController
  {
      private static readonly string filename = "report.pdf";
  
      [HttpGet]
      public void GenerateReport()
      {
          ReportPdfInput input = new ReportPdfInput()
          {
              //Empty for now
          };
  
          var manager = new ReportPdfManagerFactory().GetReportPdfManager();
          var documentRenderer = manager.GenerateReport(input);
          documentRenderer.PdfDocument.Save(filename); //Returns a PdfDocumentRenderer
          Process.Start(filename);
      }
  }
  </code></pre>
  
  <p>When this runs, I get an <code>UnauthorizedAccessException</code> at <code>documentRenderer.PdfDocument.Save(filename);</code> that says, <code>Access to the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\report.pdf' is denied.</code> I'm also not sure what will happen when the line <code>Process.Start(filename);</code> is executed.</p>
  
  <p>This is the code in <code>manager.GenerateReport(input)</code>:</p>
  
  <pre><code>public class ReportPdfManager : IReportPdfManager
  {
      public PdfDocumentRenderer GenerateReport(ReportPdfInput input)
      {
          var document = CreateDocument(input);
          var renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
          renderer.Document = document;
          renderer.RenderDocument();
  
          return renderer;
      }
  
      private Document CreateDocument(ReportPdfInput input)
      {
          //Put content into the document
      }
  }
  </code></pre>