David Tunnell David Tunnell - 6 months ago 43
Vb.net Question

Saving PDF file failing because it's being used by another process

I am trying to save a pdf using the open file dialogue and iTextSharp with a function:

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


Everything seems to be working correctly until I click save in the file save dialogue where I get the error:

"The process cannot access the file 'C:\Users\Daisy\Desktop\f.pdf' because it is being used by another process."


The file then does save to the location but isn't openable and is 0 bytes.

What am I doing wrong and how do I fix it?

Answer

You are opening two file streams to the same file. Once when you open the file with the dialog and again when you create an instance of the pdf writer.

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