Unable to delete file when convert word to PDF ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, I have x.pdf file in the C:
When I open my application first time and deleting and saving same file name like "x.pdf" under same path (C:).
Its working means that able to delete and saved.

Again If i try to save again in to same path & file name then throwing error

"The process cannot access the file because it is being used by another process " .

Code :
Using Syncfusion dll for convert word to PDF Document.​
Public Function GenratePdf(ByVal TaskNo As String, ByVal OrginPath As String, ByVal Dest As String, ByVal FileName As String, ByVal DtTable As DataTable, ByVal Docum As WordDocument, ByVal ServerPath As String) As String
Dim converter As New DocToPDFConverter
Dim doc As PdfDocument = converter.ConvertToPDF(Docum)
Try
Set font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 8)
Create Pdf ben for drawing broder
Dim borderPen As PdfPen = New PdfPen(PdfBrushes.DarkBlue)
borderPen.Width = 0
doc.PageSettings.Orientation = PdfPageOrientation.Landscape
doc.PageSettings.Margins.All = 20.0F
Create brush
Dim color As PdfColor = New PdfColor(192, 201, 219)
Dim brush As PdfSolidBrush = New PdfSolidBrush(color)
Dim defStyle As PdfCellStyle = New PdfCellStyle()
defStyle.Font = font
defStyle.BackgroundBrush = PdfBrushes.White
defStyle.BorderPen = borderPen
Create DataTable for source
Dim page As PdfPage = doc.Pages.Add()
Dim DatePrinted As String = String.Format("{0:dd/MM/yyyy}", DateTime.Now)
Dim discription As String = "Our Reference:" & TaskNo & vbCrLf & "Date Printed:" & DatePrinted & vbCrLf & "Status of Classes of Insurance held covered / not held covered"
Adding Header
Me.AddHeader(doc, "", discription, ServerPath)
Dim StrFooterVal As String = System.Configuration.ConfigurationManager.AppSettings("DocumentFooterValue1").ToString & vbCrLf & System.Configuration.ConfigurationManager.AppSettings("DocumentFooterValue2").ToString & vbCrLf & System.Configuration.ConfigurationManager.AppSettings("DocumentFooterValue3").ToString
Me.AddFooter(doc, StrFooterVal)
Dim table As PdfLightTable = New PdfLightTable()
Set Data source
table.DataSource = DtTable

Set table alternate row style
table.Style.AlternateStyle = altStyle

Set default style
table.Style.DefaultStyle = defStyle

Set header row style
table.Style.HeaderStyle = headerStyle

Show the header row
table.Style.ShowHeader = True

Repeate header in all the pages
table.Style.RepeatHeader = True

Set header data from column caption
table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions

table.Style.BorderPen = borderPen
table.Style.BorderPen = borderPen
table.Style.CellPadding = 2
table.Columns(0).Width = 4
table.Columns(1).Width = 10
table.Columns(2).Width = 20
table.Columns(3).Width = 13
table.Columns(4).Width = 7
table.Columns(5).Width = 9
table.Columns(6).Width = 5


Set layout properties
Dim format As PdfLayoutFormat = New PdfLayoutFormat()
format.Break = PdfLayoutBreakType.FitElement
format.Layout = PdfLayoutType.Paginate

Draw table
table.Draw(page, PointF.Empty, format)
For i As Integer = 0 To doc.Pages.Count - 1
If i = doc.Pages.Count - 1 Then
Exit For
End If
Dim g As PdfGraphics = doc.Pages(i).Graphics
Draw the watermark image.
Dim image As PdfImage = New PdfBitmap(ResolveApplicationDataPath("BIB.gif", ServerPath))
Setting Transparency level
g.SetTransparency(0.21F)
g.DrawImage(image, 0, 0, 600.0, page.Graphics.ClientSize.Height)
Next

Start : 03/04/2013

Dim dr As DirectoryInfo
dr = New DirectoryInfo(Dest)
If dr.Exists Then
Dim FileExt As New FileInfo(Dest & FileName & ".pdf")
If FileExt.Exists Then
Getting error here second time.
FileExt.Delete()
doc.Save(Dest & FileName & ".pdf")
Else
doc.Save(Dest & FileName & ".pdf")
End If

Else
dr.Create()
doc.Save(Dest & FileName & ".pdf")
End If

FilName = FileName & ".pdf"


Catch ex As Exception
MsgBox(ex.Message)
Finally
doc.Close()
End Try
Return FilName
End Function



If i close my application and reopen then able to delete & save for one time... :(
Can u help me do solve this problem .. Thanks in advance..​

View the full article
 
Back
Top