I am using iTextSharp and the below code worked up to last week so I am stumped, I suspect an iTextSharp update.
PDF file is found but then will not open for editing..
Error line (full error at the bottom):
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 is from the same website: www.website.com/folder/pdftest.pdf and I have tried local as well i.e. c:'... pdftest.pdf
All code:
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()
Error:
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:
Check to see if the itextsharp.dll file is being blocked in Windows. Right click the itextsharp.dll file and choose properties. At the bottom of the general tab there is probably an Unblock button. Click that button.
This would explain why System.IO can read the file but iTextSharp cannot.
Dim myBytes = System.IO.File.ReadAllBytes(sourceFile) reader = New iTextSharp.text.pdf.PdfReader(myBytes)
from @Chris-Haas was the answer without changing any settings.