title: ITextSharp PdfReader not opening PDF file
body: <p>I am putting together a proof of concept application that will run on a Windows 8.1 tablet using C# and the ITextSharp library.  The requirements for the proof of concept is the ability to use the FileOpenPicker class to select a PDF file, load the file to memory, fill in the form fields of the PDF programmatically and then save the file to a new location.</p>
  
  <p>I have the proof of concept working, if the source PDF file is installed with the application itself within the application installed location (Windows.ApplicationModel.Package.Current.InstalledLocation.Path) however if I put the source PDF someplace else, and use the FileOpenPicker to let the user pick the PDF file, the PdfReader throws the following exception:</p>
  
  <pre><code>C:\\aaa\\PDFTemplates\\ScribusPDF2.tpdf not found as file or resource.
  </code></pre>
  
  <p>Note I am using the tpdf extension to signify a PDF template file for my application however this still does not work even if I use the standard pdf extension.
  Here is my code for the file picker:</p>
  
  <pre><code>FileOpenPicker openPicker = new FileOpenPicker();
  openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
  openPicker.FileTypeFilter.Add(".tpdf");
  
  StorageFile file = await openPicker.PickSingleFileAsync();
  if (file != null)
  {
             StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFileToken", file);
      String path = file.Path.ToString();
      var reader = new PdfReader(path);
  }
  else
  {
      Debug.WriteLine("Operation cancelled.");
  }
  </code></pre>
  
  <p>It is the var reader = new PdfReader(path); line that causes the exception.  My first thought was I had a permission issue so I added the .tpdf extension and the Documents Library capability to the manifest file like this:</p>
  
  <pre><code>&lt;Extensions&gt;
      &lt;Extension Category="windows.fileTypeAssociation"&gt;
        &lt;FileTypeAssociation Name="pdf_template"&gt;
         &lt;DisplayName&gt; PDF Template&lt;/DisplayName&gt;
         &lt;SupportedFileTypes&gt;
            &lt;FileType&gt;.tpdf&lt;/FileType
         &lt;/SupportedFileTypes&gt;
        &lt;/FileTypeAssociation&gt;
       &lt;/Extension&gt;
  &lt;/Extensions&gt;
  </code></pre>
  
  <p>And </p>
  
  <pre><code>&lt;Capability Name="documentsLibrary" /&gt;
      &lt;Capability Name="internetClient" /&gt;
  &lt;/Capabilities&gt;
  </code></pre>
  
  <p>I still got the same error so I wrote a test routine to see if I could access the file outside of iTextSharp.  This route has the following code:</p>
  
  <pre><code>Debug.WriteLine("Checking:  " + templatePath);
  StorageFile file = await StorageFile.GetFileFromPathAsync(templatePath);
  var read = await FileIO.ReadBufferAsync(file);
  var prop = await file.GetBasicPropertiesAsync();
  Debug.WriteLine("Passed: " + templatePath);
  </code></pre>
  
  <p>This code works perfectly and does not throw an error therefore I know that the file (with path) exists and I have permission to access the file however the PdfReader with iTextSharp still throws the above exception when it tries to read the file.
  Does anyone know what could be causing this issue?  </p>
  
  <p>Below is the full stack trace:</p>
  
  <pre><code>   at iTextSharp.text.io.RandomAccessSourceFactory.CreateByReadingToMemory(String filename)
     at iTextSharp.text.io.RandomAccessSourceFactory.CreateBestSource(String filename)
     at iTextSharp.text.pdf.PdfReader..ctor(String filename, Byte[] ownerPassword, Boolean partial)
     at iTextSharp.text.pdf.PdfReader..ctor(String filename, Byte[] ownerPassword)
     at iTextSharp.text.pdf.PdfReader..ctor(String filename)
     at PDFFromTemplateTest.MainPage.&lt;findFile&gt;d__0.MoveNext() 
  </code></pre>
  
  <p><strong>EDIT:</strong>  Answer</p>
  
  <p>Based on the comment from Chris Hass and also other information I read last night I ended up not relying on the PdfReader to read the file.  I changed this line:</p>
  
  <pre><code>var reader = new PdfReader(path);
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>StorageFile file = await StorageFile.GetFileFromPathAsync(templatePath);
  var buf = await FileIO.ReadBufferAsync(file);
  var reader = new PdfReader(buf.ToArray());
  </code></pre>
  
  <p>and everything worked as expected.</p>