Program crashes because writing to adobe Acrobat is open!

  • Thread starter Thread starter Rocky48
  • Start date Start date
R

Rocky48

Guest
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:

System.IO.IOException
HResult=0x80070020
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.
Source=mscorlib

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)
Else
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"
document.Save(filename)

' ...and start a viewer.
Process.Start(filename)

Me.Close()
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?




TEH

Continue reading...
 
Back
Top