2015-02-23 105 views
0

我在一起使用C#和ITextSharp庫在Windows 8.1平板電腦上運行的概念驗證應用程序。概念驗證的要求是能夠使用FileOpenPicker類來選擇PDF文件,將文件加載到內存,以編程方式填充PDF的表單域,然後將文件保存到新的位置。ITextSharp PdfReader未打開PDF文件

如果源PDF文件與應用程序本身一起安裝在應用程序安裝位置(Windows.ApplicationModel.Package.Current.InstalledLocation.Path)中,但是我將源PDF放在其他位置,並使用FileOpenPicker來讓用戶選擇的PDF文件時,PdfReader引發以下異常:

C:\\aaa\\PDFTemplates\\ScribusPDF2.tpdf not found as file or resource. 

注意我用的是TPDF擴展,以表示我的申請一個PDF模板文件,然而,這仍然無法正常工作即使我使用標準的pdf擴展名。 這裏是我的文件選擇器代碼:

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."); 
} 

這是VAR讀卡器=新PdfReader(路徑);導致異常的行。我首先想到的是我有一個權限問題,所以我增加了.tpdf延伸和文檔庫能力清單文件是這樣的:

<Extensions> 
    <Extension Category="windows.fileTypeAssociation"> 
     <FileTypeAssociation Name="pdf_template"> 
     <DisplayName> PDF Template</DisplayName> 
     <SupportedFileTypes> 
      <FileType>.tpdf</FileType 
     </SupportedFileTypes> 
     </FileTypeAssociation> 
    </Extension> 
</Extensions> 

而且

<Capability Name="documentsLibrary" /> 
    <Capability Name="internetClient" /> 
</Capabilities> 

我仍然得到了同樣的錯誤,以便我編寫了一個測試例程,以查看我是否可以訪問iTextSharp之外的文件。這條路線有以下代碼:

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); 

此代碼工作完全和不因此拋出一個錯誤,我知道該文件(路徑)存在,我有權限訪問該文件但與iTextSharp的的PdfReader仍然拋出上面的例外,當它試圖讀取文件。 有誰知道可能會導致這個問題?

以下是完整的堆棧跟蹤:

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() 

編輯:回答

基於從克里斯·哈斯的評論,我看了昨天晚上我結束了不依靠PdfReader還有其他信息閱讀文件。我改變了這一行:

var reader = new PdfReader(path); 

這樣:

StorageFile file = await StorageFile.GetFileFromPathAsync(templatePath); 
var buf = await FileIO.ReadBufferAsync(file); 
var reader = new PdfReader(buf.ToArray()); 

,一切工作正常。

+0

我會開始通過調試來源。你會發現自己正在看[RandomAccessSourceFactory.cs](http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text/io/RandomAccessSourceFactory.cs#l182 )並最終[StreamUtil](http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text/io/StreamUtil.cs#l114)。你可以嘗試的一個選擇是使用你自己的IO代碼並將原始字節傳遞給iText。 – 2015-02-23 19:00:31

+0

謝謝克里斯,我更新了這個問題,我根據你昨天晚上讀的評論和其他內容解決了這個問題。 – 2015-02-24 15:32:04

回答

2

根據Chris Hass的評論以及我昨晚讀到的其他信息,我最終沒有依賴PdfReader來讀取文件。我改變了這一行:

var reader = new PdfReader(path); 

這樣:

StorageFile file = await StorageFile.GetFileFromPathAsync(templatePath); 
var buf = await FileIO.ReadBufferAsync(file); 
var reader = new PdfReader(buf.ToArray()); 

,一切工作正常。