title: Converting a PDF to text using GhostScript.net, I get an error trying to send the text to the anonymous pipe instead of the writing it to the disk
body: <p>I am using Ghostscript to convert PDF files to text via the GhostScript.NET wrapper library.   I was getting it to convert when writing the text to a file  (&quot;-sOutputFile=&quot; + outputPipeHandle), but I'd rather write it to a stream (&quot;-o&quot; + outputPipeHandle).  Here's my attempt in c#:</p>
  <pre><code>            try
              {
                  Debug.WriteLine(&quot;Installed &quot; + _lastInstalledVersion.LicenseType.ToString() + &quot; Ghostscript &quot; + _lastInstalledVersion.Version.ToString());
                  GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();
                  
                  // pipe handle format: %handle%hexvalue
                  string outputPipeHandle = &quot;%handle%&quot; + int.Parse(gsPipedOutput.ClientHandle).ToString(&quot;X2&quot;);
  
                  using (GhostscriptProcessor processor = new GhostscriptProcessor(_lastInstalledVersion, true))
                  {
                      processor.Processing += new GhostscriptProcessorProcessingEventHandler(ghostscript_Processing);
  
                      List&lt;string&gt; switches = new List&lt;string&gt;();
                      switches.Add(&quot;-empty&quot;);
                      switches.Add(&quot;-dQUIET&quot;);
                      switches.Add(&quot;-dSAFER&quot;);
                      switches.Add(&quot;-dBATCH&quot;);
                      switches.Add(&quot;-dNOPAUSE&quot;);
                      switches.Add(&quot;-dNOPROMPT&quot;);
                      if (this.Context.IsPreview)
                      {
                          switches.Add(&quot;-dLastPage=5&quot;);
                      }
                      switches.Add(&quot;-dTextFormat=3&quot;);
                      switches.Add(&quot;-sDEVICE=txtwrite&quot;);
                      switches.Add(&quot;-o&quot; + outputPipeHandle);
                      switches.Add(&quot;-q&quot;);
                      switches.Add(&quot;-f&quot;);
                      switches.Add(Context.Path);
                          //&quot;-sOutputFile=&quot; + outputPipeHandle
  
                      try
                      {
                          //processor.Process(switches.ToArray());
                          // THROWS AN ERROR HERE
                          processor.StartProcessing(switches.ToArray(), new ConsoleStdIO(true, true, true));
  
                          // HERE IS WHERE I HOPE TO GRAB THE TEXT FROM THE PIPED OUTPUT
                          byte[] rawDocumentData = gsPipedOutput.Data;
                          returnVal = System.Text.Encoding.Default.GetString(rawDocumentData);
                          Debug.WriteLine(returnVal);
                      }
                      catch (Exception ex)
                      {
                          returnVal = ex.Message;
                      }
                      finally
                      {
                          gsPipedOutput.Dispose();
                          gsPipedOutput = null;
                      }
                  }
              }
              catch (Exception ex)
              {
                  Debug.WriteLine(ex.Message);
                  returnVal = ex.Message;
              }
  
              return returnVal;
          }
      }
  </code></pre>
  <p>my errors are as follows - I'm not so much concerned with the warnings as I am the fatal error about &quot;Could not open the file 958&quot; - as I thought that it would be in memory.</p>
  <pre><code>   **** Warning: can't process font stream, loading font by the name.
     **** Error reading a content stream. The page may be incomplete.
     **** File did not complete the page properly and may be damaged.
     **** Warning: File has unbalanced q/Q operators (too many q's)
  The thread 0x59c0 has exited with code 0 (0x0).
     **** Warning: can't process font stream, loading font by the name.
     **** Error reading a content stream. The page may be incomplete.
     **** File did not complete the page properly and may be damaged.
  GPL Ghostscript 9.06: **** Could not open the file 958 .
  Error: /ioerror in --showpage--
  Operand stack:
     1   true
  Execution stack:
     %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1926   1   3   %oparray_pop   1925   1   3   %oparray_pop   1909   1   3   %oparray_pop   --nostringval--   --nostringval--   5   1   5   --nostringval--   %for_pos_int_continue   --nostringval--   --nostringval--   1809   0   10   %oparray_pop   --nostringval--   --nostringval--
  Dictionary stack:
     --dict:1173/1684(ro)(G)--   --dict:1/20(G)--   --dict:82/200(L)--   --dict:82/200(L)--   --dict:109/127(ro)(G)--   --dict:293/300(ro)(G)--   --dict:25/31(L)--   --dict:6/8(L)--   --dict:21/40(L)--   --dict:7/15(L)--
  Current allocation mode is local
  Last OS error: Bad file descriptor
  GPL Ghostscript 9.06: Unrecoverable error, exit code 1
  Exception thrown: 'Ghostscript.NET.GhostscriptAPICallException' in Ghostscript.NET.dll
  </code></pre>
  <h3>Update</h3>
  <p>For additional reference, I'm essentially using this code from the GhostScript.net samples on their website:  <a href="https://github.com/jhabjan/Ghostscript.NET/blob/master/Ghostscript.NET.Samples/Samples/PipedOutputSample.cs" rel="nofollow noreferrer">PipedOutputSample.cs</a></p>
  <p>The only difference I can see is that I changed the DEVICE to 'txtwrite' instead of 'pdfwrite'.</p>
  <h3>Update 2</h3>
  <p>After reading the documentation more carefully</p>
  <blockquote>
  <p>Note that on MS Windows systems, the % character also has a special meaning for the command processor (shell), so you will have to double it.</p>
  </blockquote>
  <p>As I happen to be on Windows, I modified where I name my outputPipeHandle to:
  <code>string outputPipeHandle = &quot;%%handle%%&quot; + int.Parse(gsPipedOutput.ClientHandle).ToString(&quot;X2&quot;);</code></p>
  <p>so I no longer get the above error about not being able to access the file.  Instead, my breakpoint on this line is reached</p>
  <pre><code>byte[] rawDocumentData = gsPipedOutput.Data;
  </code></pre>
  <p>but upon trying to copy the value of that Data field to my variable, it completely breaks out of my thread... skipping my catch, and even my finally block.  I don't see any messages as to why it broke out in the output console.  some kind of GhostScript pointer error????</p>
  <p>I'm still at a loss.  I don't even know what %handle% is supposed to signify or why I need to use it.   Without that knowledge, I'm operating blind and just throwing stuff hoping it works.</p>
  <p>Thank you.</p>