【问题标题】:Saving PDF file failing because it's being used by another process保存 PDF 文件失败,因为它正被另一个进程使用
【发布时间】:2016-05-26 22:11:58
【问题描述】:

我正在尝试使用打开文件对话框和带有功能的 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:\Users\Daisy\Desktop\f.pdf' because it is being used by another process."

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

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

【问题讨论】:

  • 流很可能在某处处于打开状态。尝试确保在处理完流后正确处理它们
  • 是的,就是这样。您正在打开同一个文件的两个文件流。一次是在您使用对话框打开文件时,一次是在您创建 pdf 编写器的实例时。

标签: asp.net vb.net itextsharp


【解决方案1】:

您正在打开同一个文件的两个文件流。一次是在您使用对话框打开文件时,另一次是在您创建 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

【讨论】:

    猜你喜欢
    • 2022-10-14
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2018-09-06
    • 1970-01-01
    • 2023-01-30
    相关资源
    最近更新 更多