title: Using PDFsharp and MigraDoc to write to and then read from a PDF
body: <p>I'm trying to write verification code for our PDF generating routines, and I'm having difficulty getting PDFsharp to extract text from files created with MigraDoc. The ExtractText code works with other PDFs, but not with the PDFs that I generate with MigraDoc (see code below.)</p>
  
  <p>Any tips on what I'm doing wrong?</p>
  
  <pre><code>//Create the Doc
  var doc = new MigraDoc.DocumentObjectModel.Document();
  doc.Info.Title = "VerifyReadWrite";
  var section = doc.AddSection();
  section.AddParagraph("ABCDEF abcdef");
  
  //Render the PDF
  var renderer = new PdfDocumentRenderer(true);
  var pdf = new PdfDocument();
  renderer.PdfDocument = pdf;
  renderer.Document = doc;
  renderer.RenderDocument();
  var msOut = new MemoryStream();
  pdf.Save(msOut, true);
  var pdfBytes = msOut.ToArray();
  
  //Read the PDF into PdfSharp
  var ms = new MemoryStream(pdfBytes);
  var pdfRead = PdfSharp.Pdf.IO.PdfReader.Open(ms, PdfDocumentOpenMode.ReadOnly);
  var segments = pdfRead.Pages[0].ExtractText().ToList();
  </code></pre>
  
  <p>Results in the following:</p>
  
  <p>segments[0] = "\0$\0%\0&amp;\0'\0(\0)"<br>
   segments[1] = "\0D\0E\0F\0G\0H\0I" </p>
  
  <p>I'd expect to see:</p>
  
  <p>segments[0] = "ABCDEF"<br>
   segments[1] = "abcdef"</p>
  
  <p>I'm using the ExtractText code from here:
  <a href="https://stackoverflow.com/questions/10141143/c-sharp-extract-text-from-pdf-using-pdfsharp/24046096#24046096">C# Extract text from PDF using PdfSharp</a></p>
  
  <p>and it works very well for all but PDFs generated with MigraDoc.</p>
  
  <pre><code>    public static IEnumerable&lt;string&gt; ExtractText(this PdfPage page)
      {
          var content = ContentReader.ReadContent(page);
          var text = content.ExtractText();
          return text.Select(x =&gt; x.Trim());
      }
  
      public static IEnumerable&lt;string&gt; ExtractText(this CObject cObject)
      {
          if (cObject is COperator)
          {
              var cOperator = (COperator) cObject;
              if (cOperator.OpCode.Name == OpCodeName.Tj.ToString() ||
                  cOperator.OpCode.Name == OpCodeName.TJ.ToString())
              {
                  foreach (var cOperand in cOperator.Operands)
                      foreach (var txt in ExtractText(cOperand))
                          yield return txt;
              }
          }
          else
          {
              var sequence = cObject as CSequence;
              if (sequence != null)
              {
                  var cSequence = sequence;
                  foreach (var element in cSequence)
                      foreach (var txt in ExtractText(element))
                          yield return txt;
              }
              else if (cObject is CString)
              {
                  var cString = (CString) cObject;
                  yield return cString.Value;
              }
          }
      }
  </code></pre>