0

我正在尝试找到一种方法来关闭保存我正在创建的 pdf 文件的进程。我试图在创建流后处理和关闭流,但我收到消息说进程无法访问文件'\server\PDFs\test.pdf',因为它正在被另一个进程使用。基本上,一旦最初创建了 test.pdf,我就会有一个页面,他们可以在其中编辑数据并重新创建修改后的 test.pdf。知道如何关闭该过程吗?我尝试做一个while语句循环来继续尝试但没有运气?

'Create the original pdf
 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))
    pdfWrite.Flush()
    Doc1.Close()
    Doc1.Dispose()
    pdfWrite.Dispose()
    pdfWrite.Close()

编辑数据后重新创建pdf:

 Dim _fileInUse As Boolean = True
    Do While _fileInUse
        _fileInUse = FileInUse(myUniqueFileName)
        If Not _fileInUse Then
            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))
        End If
    Loop



    Doc1.Close()
    Doc1.Dispose()

Public Function FileInUse(ByVal sFile As String) As Boolean

    Dim thisFileInUse As Boolean = False
    Dim fs As FileStream

    If File.Exists(sFile) Then
        Try
            fs = File.Open(sFile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)

            fs.Close()
            fs.Dispose()
        Catch
            Try
                fs.Close()
                fs.Dispose()
            Catch

            End Try

            thisFileInUse = True
        End Try
    End If
    Return thisFileInUse
End Function
4

2 回答 2

2

你有一个New FileStream没有Dim pdfWrite As PdfWriter =...办法关闭它的入口。

我建议您使用 Using 构造来处理您的事情,并利用 Try..Catch 中的 finally 子句,如下所示:

Sub X()
    'Create the original pdf
    Using doc1 As New Document
        Dim myFile As String = Path.Combine(myPath, myUniqueFileName)
        Using fs As New FileStream(myFile, FileMode.Create)
            Using 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))
                pdfWrite.Flush()

            End Using
        End Using
    End Using

End Sub

Public Function FileInUse(ByVal sFile As String) As Boolean

    Dim thisFileInUse As Boolean = False

    If File.Exists(sFile) Then
        Dim fs As FileStream = Nothing
        Try
            fs = File.Open(sFile, FileMode.Open, FileAccess.Write, FileShare.None)
        Catch
            thisFileInUse = True
        Finally
            If fs IsNot Nothing Then
                fs.Dispose()
            End If
        End Try
    End If

    Return thisFileInUse

End Function

Try..Catch 的最后部分总是运行。

编辑:您当然会检查类实例上的 .Dispose() 方法是否会处理任何需要完成的家务,例如,我在不执行显式 .Close() 之前检查了Stream.Dispose 方法. 我没有关于 ITextSharp 方法的文档,因此您必须自己检查。

于 2013-10-08T19:36:47.537 回答
0

您能否仅创建另一个具有唯一名称的文件并对其进行更新。我没有尝试过,但我不喜欢不断尝试循环打开文件以检查您是否可以写入它的想法。也许这本身就引起了问题

像这样的东西?:

'Create the original pdf
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path &          myUniqueFileName, FileMode.Create))
Dim rdm As New Random()
Dim newName As String
newName =  myUniqueFileName + rdm.Next(0, 100000).ToString("00000")

Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & newName , 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()
于 2013-10-08T19:36:34.120 回答