title: Opening a PDF file as a raw text document
body: <p>I have a PDF document which becomes encrypted. During encryption a code is embedded the then end of the filestream before writing to a file.</p>
  
  <p>This PDF is later decrypted and the details are view-able in any PDF viewer. </p>
  
  <p>The issue is the embedded code is also then visible in the decrypted PDF and it needs removing.</p>
  
  <p>I'm looking to decrypt the PDF document, remove the embedded document code then save it to a filename.</p>
  
  <pre><code>//Reading the PDF
  Encoding enc = Encoding.GetEncoding("us-ascii");
  while ((read = cs.Read(buffer, 0, buffer.Length)) &gt; 0)
  {
  System.Text.Encoding.UTF8.GetString(buffer);
  x = x + enc.GetString(buffer);
  }
  
  //Remove the code
  x = x.Replace("CODE","");
  
  //Write file
  byte[] bytes = enc.GetBytes(x);
  File.WriteAllBytes(@filePath, bytes);
  </code></pre>
  
  <p>When the original file is generated it appears to be using a different encoder because the first line on the original file reads %PDF-1.6%âãÏÓ and on the decoded file %PDF-1.6 %????.</p>
  
  <p>I have tried ascii, us-ascii, UTF8 and Unicode but upon removal of the embedded CODE the file stoped opening due to corruption. Note the embedded code sits in the raw file after the PDF %%EOF tag.</p>
  
  <p>Has anyone any ideas?</p>