title: How to open an existing PDF file with Migradoc PDF library
body: <p>I am trying to use the Migradoc library from PDFSharp (<a href="http://www.pdfsharp.net/" rel="noreferrer">http://www.pdfsharp.net/</a>) to print pdf files.  So far I have found that Migradoc does support printing through its <code>MigraDoc.Rendering.Printing.MigraDocPrintDocument</code> class.  However, I have not found a way to actually open an existing PDF file with MigraDoc.</p>
  
  <p>I did find a way to open an existing PDF file using PDFSharp, but I cannot successfully convert a <code>PDFSharp.Pdf.PdfDocument</code> into a <code>MigraDoc.DocumentObjectModel.Document</code> object.  So far I have not found the MigraDoc and PDFSharp documentation to be very helpful.</p>
  
  <p>Does anyone have any experience using these libraries to work with existing PDF files?</p>
  
  <p>I wrote the following code with help from <a href="http://www.pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx" rel="noreferrer">this</a> sample, but the result when my input PDF is 2 pages is an output PDF with 2 blank pages.</p>
  
  <pre><code>using MigraDoc.DocumentObjectModel;
  using MigraDoc.Rendering;
  using PdfSharp.Drawing;
  using PdfSharp.Pdf;
  using PdfSharp.Pdf.IO;
  
  ...
  
  public void PrintPDF(string filePath, string outFilePath)
  {
  
      var document = new Document();
  
      var docRenderer = new DocumentRenderer(document);
      docRenderer.PrepareDocument();
  
      var inPdfDoc = PdfReader.Open(filePath, PdfDocumentOpenMode.Modify);
  
      for (var i = 0; i &lt; inPdfDoc.PageCount; i++)
      {
          document.AddSection();
          docRenderer.PrepareDocument();
  
          var page = inPdfDoc.Pages[i];
  
          var gfx = XGraphics.FromPdfPage(page);
  
          docRenderer.RenderPage(gfx, i+1);
      }
  
      var renderer = new PdfDocumentRenderer();
  
      renderer.Document = document;
  
      renderer.RenderDocument();
  
      renderer.PdfDocument.Save(outFilePath);
  
  }
  </code></pre>