Program crashes because writing to adobe Acrobat is open! RRS feed

  • Question

  • I am using PDFSharp for my project and when the PDF file is written and you try to create another PDF file the program freezes and won't work unless you stop debugging.

    Here is the exception:

      Message=The process cannot access the file 'D:\Visual Basic Projects\My Greeting\My Greeting\bin\Debug\Verse.pdf' because it is being used by another process.

    This is the form:

    Private Sub printIt(row As DataRow)
            Dim ID As Double = CDbl(row("CID"))
            Dim X As Double = CDbl(row("BoxX")) * 2.83465
            Dim W As Double = CDbl(row("Cellw")) * 2.83465
            Dim H As Double = CDbl(row("Cellh")) * 2.83465
            Dim O As String = CStr(row("ort"))
            Dim N As String = CStr(row("Narrative"))
            Dim Y As Double
            Dim NL As String = CStr(Chr(13) & Chr(10))
            Verse = txbVerse.Text
            Replace(Verse, NL, " VBCrLf ")
            Dim document As PdfDocument
            ' Create a new PDF document
            document = New PdfDocument()
            document.Info.Title = "Created with PDFsharp"
            ' Create an empty page
            Dim page As PdfPage = document.AddPage
            If O = "L" Then
                page.Orientation = CType(pageOrientation.Landscape, PdfSharp.PageOrientation)
                page.Width = XUnit.FromMillimeter(297)
                page.Height = XUnit.FromMillimeter(210)
                page.Orientation = CType(pageOrientation.Portrait, PdfSharp.PageOrientation)
                page.Width = XUnit.FromMillimeter(210)
                page.Height = XUnit.FromMillimeter(297)
            End If
            '   create the brush
            Dim xClr As XColor = XColor.FromKnownColor(CType(cboColor.SelectedItem, XKnownColor))
            Dim brush As XSolidBrush = New XSolidBrush(xClr)
            ' Draw the text
            Y = boxY * 2.834665
            Dim gfx As XGraphics
            gfx = XGraphics.FromPdfPage(page)
            Dim tf As XTextFormatter
            tf = New XTextFormatter(gfx)
            Dim font As XFont = New XFont(CFont, FSize, XFontStyle.Regular)
            Dim rect As XRect
            rect = New XRect(X, Y, W, H)
            gfx.DrawRectangle(XBrushes.SeaShell, rect)
            tf.Alignment = XParagraphAlignment.Center
            tf.DrawString(Verse, font, brush, rect, XStringFormat.TopLeft)
            ' Save the document
            Dim filename As String = "Verse.pdf"
            ' ...and start a viewer.
        End Sub

    The error is in the 'Save the document' part at the end of the script.

    I close the form to help users not to attempt another file, but this is not satisfactory.

    How can you kill the proccessing after the Adobe file has been written.

    I had used in the past, on the web, fpdf written in PHP and this would add a (1) to the file name, which I guess is a property of Adobe.

    Is there a command that will kill the proceess once the file is written?


    • Moved by Alex-KSGZ Monday, July 22, 2019 6:00 AM
    Friday, July 19, 2019 9:23 PM

All replies

  • Hello,

    Best guess is that the document variable still has a handle open, try doing 

    document = Nothing

    If that does not resolve the issue post the issue here https://forum.pdfsharp.net/

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Saturday, July 20, 2019 12:08 AM
  • Tried your suggestion Karen.

    Unfortunately it did not work!

    Did try to add a numeric value to the file name, but the loop completed it's iteration, so the file became verses10.pdf.

    Do you know how I can do a For Next loop that only updates by 1 when the form is opened again?


    Saturday, July 20, 2019 4:53 PM