PDF Found but failed to open for iTextSharp

為{幸葍}努か 提交于 2019-12-13 00:45:04

问题


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:

回答1:


Dim myBytes = System.IO.File.ReadAllBytes(sourceFile) reader = New iTextSharp.text.pdf.PdfReader(myBytes) from @Chris-Haas was the answer without changing any settings.




回答2:


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.



来源:https://stackoverflow.com/questions/24368270/pdf-found-but-failed-to-open-for-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!