Reputation: 339
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.
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:
C:\\aaa\\PDFTemplates\\ScribusPDF2.tpdf not found as file or resource.
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:
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.");
}
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:
<Extensions>
<Extension Category="windows.fileTypeAssociation">
<FileTypeAssociation Name="pdf_template">
<DisplayName> PDF Template</DisplayName>
<SupportedFileTypes>
<FileType>.tpdf</FileType
</SupportedFileTypes>
</FileTypeAssociation>
</Extension>
</Extensions>
And
<Capability Name="documentsLibrary" />
<Capability Name="internetClient" />
</Capabilities>
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:
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);
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?
Below is the full stack trace:
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.<findFile>d__0.MoveNext()
EDIT: Answer
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:
var reader = new PdfReader(path);
to this:
StorageFile file = await StorageFile.GetFileFromPathAsync(templatePath);
var buf = await FileIO.ReadBufferAsync(file);
var reader = new PdfReader(buf.ToArray());
and everything worked as expected.
Upvotes: 0
Views: 4749
Reputation: 339
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:
var reader = new PdfReader(path);
to this:
StorageFile file = await StorageFile.GetFileFromPathAsync(templatePath);
var buf = await FileIO.ReadBufferAsync(file);
var reader = new PdfReader(buf.ToArray());
and everything worked as expected.
Upvotes: 2