我正在尝试找到一种方法来关闭保存我正在创建的 pdf 文件的进程。我试图在创建流后处理和关闭流,但我收到消息说进程无法访问文件'\server\PDFs\test.pdf',因为它正在被另一个进程使用。基本上,一旦最初创建了 test.pdf,我就会有一个页面,他们可以在其中编辑数据并重新创建修改后的 test.pdf。知道如何关闭该过程吗?我尝试做一个while语句循环来继续尝试但没有运气?
'Create the original pdf
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & myUniqueFileName, FileMode.Create))
Doc1.Open()
Dim test As String
test = Session("PDF")
Dim PDFHeader As String
PDFHeader = Session("Header")
Dim imagepath As String = Server.MapPath(".") & "/images/Header.png"
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath)
image.ScalePercent(70.0F)
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)
Doc1.Add(image)
Doc1.Add(New Paragraph(PDFHeader))
Doc1.Add(New Chunk(line1))
Doc1.Add(New Paragraph(test))
pdfWrite.Flush()
Doc1.Close()
Doc1.Dispose()
pdfWrite.Dispose()
pdfWrite.Close()
编辑数据后重新创建pdf:
Dim _fileInUse As Boolean = True
Do While _fileInUse
_fileInUse = FileInUse(myUniqueFileName)
If Not _fileInUse Then
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & myUniqueFileName, FileMode.Create))
Doc1.Open()
Dim test As String
test = Session("PDF")
Dim PDFHeader As String
PDFHeader = Session("Header")
Dim imagepath As String = Server.MapPath(".") & "/images/Header.png"
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath)
image.ScalePercent(70.0F)
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)
Doc1.Add(image)
Doc1.Add(New Paragraph(PDFHeader))
Doc1.Add(New Chunk(line1))
Doc1.Add(New Paragraph(test))
End If
Loop
Doc1.Close()
Doc1.Dispose()
Public Function FileInUse(ByVal sFile As String) As Boolean
Dim thisFileInUse As Boolean = False
Dim fs As FileStream
If File.Exists(sFile) Then
Try
fs = File.Open(sFile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)
fs.Close()
fs.Dispose()
Catch
Try
fs.Close()
fs.Dispose()
Catch
End Try
thisFileInUse = True
End Try
End If
Return thisFileInUse
End Function