我正在使用iTextSharp并且上面的代码在上周工作,所以我很难过,我怀疑是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
Sourcefile来自同一个网站: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 :(得分:1)
Dim myBytes = System.IO.File.ReadAllBytes(sourceFile) reader = New iTextSharp.text.pdf.PdfReader(myBytes)
是没有改变任何设置的答案。
答案 1 :(得分:0)
检查是否在Windows中阻止了itextsharp.dll文件。右键单击itextsharp.dll文件并选择属性。在常规选项卡的底部可能存在“取消阻止”按钮。单击该按钮。
这可以解释为什么System.IO可以读取文件,但iTextSharp不能。