0

My program use SQlite and iTextsharp.

One button allow the user to create and display a PDF inside a WebBrowser. It works fine the first time, and sometimes more than once, but after a few clicks (every one generating a new version of the PDF with different fonts, sizes...) I get a exception saying the file PDF is allready open. After the exception the program keep running.

I tried with pauses after the click or checking if the file was already open and force the close (The "IsFileOpen" function of this post How to check if the text file is open and close the text file? ), garbage collector, but still the same.

The code looks like this:

Try
    Using (conn)
        conn.Open()

            Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(MyPDFFile, FileMode.Create))

            pdfDoc.Open()

                'All the code to create the PDF

            pdfDoc.Close()

    End Using

Catch ex As Exception
    'The code to show the exception
End Try

The Debug always point to the line "Dim pdfWrite As PdfWriter", meaning the pdfDOC is still open when the program reaches that line after a few clicks

Sometimes Adobe Reader say error 523:523 but the solution https://forums.adobe.com/thread/961724 " Is the protected mode enabled (Preferences > General > Enable Protected Mode at startup)?" didn't fix the problem

how can I fix that exception?

Community
  • 1
  • 1
fedeteka
  • 943
  • 1
  • 14
  • 33
  • 1
    Everything/anything that exposes a Dispose method ought t be used in a `Using` block to close and dispose of it. I dont know about the PDFWriter but you arent saving the stream to be able to dispose of it – Ňɏssa Pøngjǣrdenlarp Jan 24 '17 at 00:11
  • If I understand you say I need to use another Using before the "Dim pdfWrite As PdfWriter" line? – fedeteka Jan 24 '17 at 00:17
  • 1
    Yes, use it to create the FileStream, the existing `Using` on the conn object wont actually do anything since it is created elsewhere – Ňɏssa Pøngjǣrdenlarp Jan 24 '17 at 00:19
  • http://stackoverflow.com/questions/39604859/backup-sqlite-database-with-vb-net#comment66540616_39613794 – Ňɏssa Pøngjǣrdenlarp Jan 24 '17 at 00:34
  • 1
    Not familiar with iTextSharp, but you could try assigning the `FileStream` to a variable and disposing it. – SSS Jan 24 '17 at 03:14
  • @sss Thanks. I test with code like `Dim fs As System.IO.FileStream = File.Create(MyPDFFile)` `Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, fs)` and at the end `fs.dispose()` but still not working – fedeteka Jan 24 '17 at 07:28
  • 1
    *"I get a exception saying the file PDF is allready open"* - is the PDF probably still open in the web browser? – mkl Jan 24 '17 at 08:09
  • @mkl Thanks but no because I close the webbrowser and wait until is ready. Now I test the program using FoxitReader as default PDF reader and is working fine, maybe is something about Adobe Reader and security – fedeteka Jan 24 '17 at 10:23
  • After reading a lot of post about WebBrowsers troubles I solved this situation using AxAcroPDF instead of WebBrowser. Thanks – fedeteka Jan 28 '17 at 15:42

0 Answers0