找到PDF,但无法为iTextSharp打开 [英] PDF Found but failed to open for iTextSharp

查看:149
本文介绍了找到PDF,但无法为iTextSharp打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp,下面的代码工作到上周为止,所以我很困惑,我怀疑是iTextSharp更新.

I am using iTextSharp and the below code worked up to last week so I am stumped, I suspect an iTextSharp update.

找到PDF文件,但将无法打开该文件进行编辑.

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

源文件来自同一网站:www.website.com/folder/pdftest.pdf,我也尝试过本地,即c:'... pdftest.pdf

Sourcefile is from the same website: www.website.com/folder/pdftest.pdf and I have tried local as well i.e. 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:

来自@ Chris-Haas的

推荐答案

Dim myBytes = System.IO.File.ReadAllBytes(sourceFile) reader = New iTextSharp.text.pdf.PdfReader(myBytes)是没有更改任何设置的答案.

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

这篇关于找到PDF,但无法为iTextSharp打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆