We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Unable to delete file when convert word to PDF ?

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:\).
 It's 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 :

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)
            '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.DrawImage(image, 0, 0, 600.0, page.Graphics.ClientSize.Height)

            '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
                    FileExt.Delete()                                             ' Getting error here second time.
                    doc.Save(Dest & FileName & ".pdf")
                    doc.Save(Dest & FileName & ".pdf")
                End If

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

            FilName = FileName & ".pdf"

        Catch ex As Exception
        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..

4 Replies

NA Narsa April 4, 2013 01:21 AM UTC

  doc.Close(True) is also not working throwing same error.

KC Karthikeyan Chandrasekar Syncfusion Team April 8, 2013 04:50 AM UTC

Hi Narsa,
Thank you for using Syncfusion products.

We are not able to reproduce the problem, for deleting the file.
I also attached a sample project for your reference.

Could you please modify the sample project to reproduce the issue.
Please let us know if you have any concern.



NA Narsa April 9, 2013 09:59 AM UTC


Attached project is working fine , but my one is asp.net application and giving error If try to delete manually also.

KC Karthikeyan Chandrasekar Syncfusion Team April 12, 2013 04:16 AM UTC

Hi Narsa,

Thank you for your update.


We are not able to reproduce the issue.

I have attached a sample project, could you please modify it to reproduce your issue.



Please let us know if you have any concern.




Live Chat Icon For mobile
Up arrow icon