Hi everyone,
I'm building an application that converts PS-files to the PDF format using Distiller Automation. I've written a testprogram and everything works fine. ONCE! If I try to run the program again, it throws an exeption:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in DistillerAPI_Converter.exe
This is because when I fire up the program the first time, it creates a acrodist.exe process(the actual Adobe Distiller), but I don't seem to succeed in closing that process down. :-(
When I close my program, the acrodist process ends like it should, but the problem occurs when I have converted one, or several files, and try to do this again without restarting the program.
Below the code I use:
Obviously I need to release the object one way or another, but I'm out of ideas here :-s
Any information would be greatly appreciated.
Thanx in advance.
I'm building an application that converts PS-files to the PDF format using Distiller Automation. I've written a testprogram and everything works fine. ONCE! If I try to run the program again, it throws an exeption:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in DistillerAPI_Converter.exe
This is because when I fire up the program the first time, it creates a acrodist.exe process(the actual Adobe Distiller), but I don't seem to succeed in closing that process down. :-(
When I close my program, the acrodist process ends like it should, but the problem occurs when I have converted one, or several files, and try to do this again without restarting the program.
Below the code I use:
Code:
[COLOR=green]'Author: Tim Wuytens
'Goal: Convert *.ps to *.pdf using adobe acrobat distiller automation
'Date: 2005-10-25[/COLOR]
Imports System.IO
Imports ACRODISTXLib
Public Class frmDistillerAPI_Converter
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
[COLOR=green]'omitted code for form generation
'it's a simple form with a textbox, a listbox and 2 buttons(start and browse)[/color]
#End Region
[COLOR=green]'Declare object with its events[/color]
Public WithEvents objDistil As PdfDistiller6
[COLOR=green]'Handler for the Click event on the Browse button
'Shows a filedialog where you can select one or multiple files
'and puts these filenames in a listbox[/color]
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnBrowse.Click
Dim str As String
With OpenFileDialog1
.CheckFileExists = True
.CheckPathExists = True
.Multiselect = True
If DialogResult.OK = .ShowDialog Then
For Each str In .FileNames
lstPad.Items.Add(str)
Next
End If
End With
End Sub
[COLOR=green]'Handler for the Click event on the Start button[/color]
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnStart.Click
Dim InputPS As String
Dim str, OutputPDF, JobOptions As String
[COLOR=green]'set reference to new object[/color]
objDistil = New PdfDistiller6
[COLOR=green]'iterate through all listitems[/color]
For Each str In lstPad.Items
InputPS = str
OutputPDF = InputPS.Substring(0, InputPS.LastIndexOf(Path.DirectorySeparatorChar))
OutputPDF &= "\pdf\"
If Not Directory.Exists(OutputPDF) Then
Directory.CreateDirectory(OutputPDF)
End If
[COLOR=green]'catch result and show in textbox[/color]
Dim res As Short
[COLOR=green]'the FileToPDF function does the actual conversion
'it works fine, even when multiple files are selected
'however, when the button is clicked again
'the exception mentioned above is thrown
'I know for sure this is because the acrodist.exe process is still running
'because I tried manually ending it between two clicks, and then
'it works fine again[/color]
res = objDistil.FileToPDF(InputPS, OutputPDF, JobOptions)
txtUit.AppendText(res)
Next
[COLOR=green]'release the object reference
'(was meant for ending the process acrodist.exe, but doesn't do its job)[/color]
objDistil = Nothing
End Sub
[COLOR=green]'OnLogMessage catches the logmessages that normally would appeat in the distiller log[/color]
Private Sub objDistil_OnLogMessage(ByVal strMessage As String) Handles objDistil.OnLogMessage
strMessage = strMessage.Replace(vbLf, vbNewLine)
txtUit.AppendText(strMessage)
End Sub
End Class
Any information would be greatly appreciated.
Thanx in advance.