ItextSharp锁定所有文件

时间:2014-12-02 18:38:59

标签: itextsharp

我使用itext sharp来合并一些pdf。之后,我想删除它们。 但是,itextsharp不会关闭文件,而File.Delete会抛出异常。

这是我的代码:

 Dim mergedPdf As Byte() = Nothing
        Using ms As New MemoryStream()
            Using document As New Document()
                Using copy As New PdfCopy(document, ms)
                    document.Open()

                    '_listaPath is a List (of String) with the paths off all pdf to merge
                    For i As Integer = 0 To _listaPDF.Count - 1
                        Dim reader As New PdfReader(_listaPDF(i))
                        ' loop over the pages in that document
                        Dim n As Integer = reader.NumberOfPages
                        Dim page As Integer = 0
                        While page < n
                            copy.AddPage(copy.GetImportedPage(reader, System.Threading.Interlocked.Increment(page)))
                        End While
                    Next

                End Using

            End Using
            mergedPdf = ms.ToArray()

        End Using
        File.WriteAllBytes(fileexplorer.FileName, mergedPdf)
         For Each pdfTMP In _listaPDF
            If File.Exists(pdfTMP) Then
                File.Delete(pdfTMP)
            End If
        Next
        _listaPDF = New List(Of String)

1 个答案:

答案 0 :(得分:3)

我发现了问题。

这一行

Dim reader As New PdfReader(_listaPDF(i))

应该是

Using reader As New PdfReader(_listaPDF(i))

结论,我需要更多咖啡