2

我正在使用下面的代码生成 pdf。然后它会填充一个网格视图,以便用户可以单击刚刚生成的 PDF。我收到一条异常消息:Exception type: IOException

该进程无法访问文件“\Server\PDFs\PE10091026-Rev.pdf”,因为它正被另一个进程使用。

如果我生成另一个 PDF,则可以打开前一个 PDF。

如果没有人打开文件,知道是什么锁住了它吗?

Dim Doc1 As New Document
Dim path As String = "\\server\PDFs\"
Dim myUniqueFileName = String.Format("{0}.pdf", Session("FileName") & "-Rev")
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & myUniqueFileName, FileMode.Create))

Doc1.Open()
Dim test As String
test = Session("PDF")
Dim PDFHeader As String
PDFHeader = Session("Header")
Dim imagepath As String = Server.MapPath(".") & "/images/Header.png"
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath)
image.ScalePercent(70.0F)
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)

Doc1.Add(image)
Doc1.Add(New Paragraph(PDFHeader))
Doc1.Add(New Chunk(line1))
Doc1.Add(New Paragraph(test))
Doc1.Close()
Doc1.Dispose()
4

1 回答 1

2

您可能需要处理FileStreamusingFileStream.Dispose()或 by usingUsing语句。目前, 当您第二次尝试打开它时,它FileStream仍以创建模式打开。

Using fs As New FileStream(Path & myUniqueFileName, FileMode.Create)

    Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, fs)
    Doc1.Open()

    Dim test As String
    test = Session("PDF")
    Dim PDFHeader As String
    PDFHeader = Session("Header")
    Dim imagepath As String = Server.MapPath(".") & "/images/Header.png"
    Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath)
    image.ScalePercent(70.0F)
    Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)

    Doc1.Add(image)
    Doc1.Add(New Paragraph(PDFHeader))
    Doc1.Add(New Chunk(line1))
    Doc1.Add(New Paragraph(test))
    Doc1.Close()
    Doc1.Dispose()

End Using
于 2013-09-23T14:11:46.673 回答