我正在尝试找到一种方法来关闭持有我正在创建的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
答案 0 :(得分:2)
New FileStream
中有Dim pdfWrite As PdfWriter =...
无法关闭它。
我建议你使用Using构造来处理你的事情,并利用Try..Catch中的Finally子句,如下所示:
Sub X()
'Create the original pdf
Using doc1 As New Document
Dim myFile As String = Path.Combine(myPath, myUniqueFileName)
Using fs As New FileStream(myFile, FileMode.Create)
Using pdfWrite As PdfWriter = PdfWriter.GetInstance(doc1, fs)
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()
End Using
End Using
End Using
End Sub
Public Function FileInUse(ByVal sFile As String) As Boolean
Dim thisFileInUse As Boolean = False
If File.Exists(sFile) Then
Dim fs As FileStream = Nothing
Try
fs = File.Open(sFile, FileMode.Open, FileAccess.Write, FileShare.None)
Catch
thisFileInUse = True
Finally
If fs IsNot Nothing Then
fs.Dispose()
End If
End Try
End If
Return thisFileInUse
End Function
Try..Catch的Last部分始终运行。
编辑:您当然会检查一个Class实例上的.Dispose()方法是否会处理需要完成的任何家务,例如我检查{{3在没有做一个明确的.Close()之前。我没有提供ITextSharp方法的文档,所以你必须自己检查一下。
答案 1 :(得分:0)
你能不能只创建一个具有唯一名称的文件并更新它。我没有尝试过,但我不喜欢不断尝试在循环中打开文件以检查是否可以写入文件。也许这会导致问题
这样的东西?:
'Create the original pdf
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & myUniqueFileName, FileMode.Create))
Dim rdm As New Random()
Dim newName As String
newName = myUniqueFileName + rdm.Next(0, 100000).ToString("00000")
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & newName , 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))
Doc1.Close()
Doc1.Dispose()