保存 PDF 文件失败,因为它正被另一个进程使用



我正在尝试使用打开文件对话框和带有以下功能的iTextSharp保存pdf:

Private Sub saveFileDialog(saveType As String)
    ' Displays a SaveFileDialog
    Dim saveFileDialog1 As New SaveFileDialog()
    Select Case saveType
        Case "PDF"
            saveFileDialog1.Filter = "PDF File|*.pdf"
            saveFileDialog1.Title = "Save a PDF File"
        Case "Image"
            saveFileDialog1.Filter = "PNG Image|*.png"
            saveFileDialog1.Title = "Save an Image File"
    End Select
    saveFileDialog1.ShowDialog()
    ' If the file name is not an empty string open it for saving.
    If saveFileDialog1.FileName <> "" Then
        ' Saves the Image via a FileStream created by the OpenFile method.
        Dim fs As System.IO.FileStream = CType(saveFileDialog1.OpenFile(), System.IO.FileStream)
        Select Case saveType
            Case "PDF"
                Dim doc As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, Bounds.Left, Bounds.Right, Bounds.Top, Bounds.Bottom)
                Dim wri As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, New FileStream(saveFileDialog1.FileName, FileMode.Create))
                doc.Open()
                Dim Image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Png)
                doc.Add(Image)
                doc.Close()
            Case "Image"
                bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png)
        End Select
        fs.Close()
    End If
End Sub

一切似乎都正常工作,直到我在文件保存对话框中单击保存,出现错误:

"The process cannot access the file 'C:UsersDaisyDesktopf.pdf' because it is being used by another process."

然后,文件会保存到该位置,但不可打开且为 0 字节。

做错了什么,我该如何解决?

您正在打开指向同一文件的两个文件流。一次是在使用对话框打开文件时,另一次是在创建 pdf 编写器的实例时。

Private Sub saveFileDialog(saveType As String)
    ' Displays a SaveFileDialog
    Dim saveFileDialog1 As New SaveFileDialog()
    Select Case saveType
        Case "PDF"
            saveFileDialog1.Filter = "PDF File|*.pdf"
            saveFileDialog1.Title = "Save a PDF File"
        Case "Image"
            saveFileDialog1.Filter = "PNG Image|*.png"
            saveFileDialog1.Title = "Save an Image File"
    End Select
    saveFileDialog1.ShowDialog()
    ' If the file name is not an empty string open it for saving.
    If saveFileDialog1.FileName <> "" Then
        ' Saves the Image via a FileStream created by the OpenFile method.
        Dim fileStream As System.IO.Stream = saveFileDialog1.OpenFile()
        Select Case saveType
            Case "PDF"
                Dim doc As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, Bounds.Left, Bounds.Right, Bounds.Top, Bounds.Bottom)
                Dim wri As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, fileStream)
                doc.Open()
                Dim Image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Png)
                doc.Add(Image)
                doc.Close()
            Case "Image"
                bmp.Save(fileStream, System.Drawing.Imaging.ImageFormat.Png)
        End Select
        fileStream.Close()
    End If
End Sub

相关内容

最新更新