2014-06-23 67 views
1

我正在使用iTextSharp和下面的代碼工作到上週,所以我很難過,我懷疑iTextSharp更新。PDF找到但未能打開iTextSharp

PDF文件被發現,但隨後不打開編輯..

錯誤行(底部完整的錯誤):

If System.IO.File.Exists(sourceFile) Then ' found here 
    reader = New iTextSharp.text.pdf.PdfReader(sourceFile) 'fails here, see error at bottom of query 

的資源文件是從同一網站:www.website.com /folder/pdftest.pdf我已經試過當地以及即C:」 ...... pdftest.pdf

所有代碼:

Dim reader As iTextSharp.text.pdf.PdfReader = Nothing 
Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing 
Dim img As iTextSharp.text.Image = Nothing 
Dim img1 As iTextSharp.text.Image = Nothing 
Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing 
Dim overContent As iTextSharp.text.pdf.PdfContentByte = Nothing 
Dim rect As iTextSharp.text.Rectangle = Nothing 

'Dim X, Y As Single 
Dim pageCount As Integer = 0 


If System.IO.File.Exists(sourceFile) Then 
    reader = New iTextSharp.text.pdf.PdfReader(sourceFile) 
    rect = reader.GetPageSizeWithRotation(1) 
    stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputFile, System.IO.FileMode.Create)) 
    pageCount = reader.NumberOfPages() 

    For i As Integer = 1 To pageCount 

     '############# 
     overContent = stamper.GetOverContent(i) ' can be over or under the existing layers 
     watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED) 
     watermarkFontColor = iTextSharp.text.Basecolor.BLACK 
     overContent.BeginText() ' black set text first 
     overContent.SetFontAndSize(watermarkFont, 22) 
     overContent.SetColorFill(watermarkFontColor) 
     overContent.ShowTextAligned(Element.ALIGN_CENTER, "This is test", 300, 625, 0) 
     overContent.ShowTextAligned(Element.ALIGN_CENTER, "Successfully completed", 300, 475, 0) 
     overContent.ShowTextAligned(Element.ALIGN_CENTER, "A PDF Text", 300, 325, 0) 
     overContent.ShowTextAligned(Element.ALIGN_CENTER, "on", 300, 275, 0) 
     overContent.EndText() 

    Next 

    stamper.Close() 
    reader.Close() 

錯誤:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.IOException: C:\sites\www\gateway\admin\maintenance\admin\blank.pdf not found as file or resource. 

Source Error: 

Line 229: 
Line 230:  If System.IO.File.Exists(sourceFile) Then 
Line 231:   reader = New iTextSharp.text.pdf.PdfReader(sourceFile) 
Line 232: 
Line 233: 
+0

也許文件存在,但您沒有權限讀取它。請參閱http://stackoverflow.com/questions/21649197/itextsharp-cant-find-pdf和http://stackoverflow.com/questions/16699386/determine-what-user-account-itextsharp-runs-as和http:/ /stackoverflow.com/questions/1637505/read-pdf-using-itext –

+1

嘗試將該文件移到網站目錄之外的另一個文件夾中,看看是否可以這樣做? – Codexer

+0

有沒有給定名稱的目錄?一個目錄確實存在,但不能像文件一樣打開閱讀。 – mkl

回答

1

Dim myBytes = System.IO.File.ReadAllBytes(sourceFile) reader = New iTextSharp.text.pdf.PdfReader(myBytes)來自@ Chris-Haas的答案是沒有改變任何設置。

+1

對於我來說,這比解決DLL和權限問題更加清晰。 – ilasno

0

檢查是否在Windows中阻止了itextsharp.dll文件。右鍵單擊itextsharp.dll文件並選擇屬性。在常規選項卡的底部可能有一個Unblock按鈕。點擊那個按鈕。

這將解釋爲什麼System.IO可以讀取文件,但iTextSharp不能。