0
我試圖用打開文件的對話和iTextSharp的一個函數來保存PDF:保存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字節。
我在做什麼錯,我該如何解決?
很有可能某個流在某處處於打開狀態。儘量確保你正在處理與他們完成的流 – Nkosi
是的,就是這樣。您正在將兩個文件流打開到同一個文件。一旦你用對話框打開文件,並再次創建PDF編寫器的實例時。 – Nkosi