What I did was wrap both the application and document in classes.
Wrapping the application was more for tidiness for me. You may not need
it.
When reading a document I would create a new instance of the document
wrapper class
e.g.
_adobeDoc = New AdobeDocument(fName, createCopy, storagePath)
And then when closing it I would dispose of it
_adobeDoc.Dispose()
_adobeDoc = nothing
The Dispose calls close on the avDoc and pdDoc and also decrements the
COM reference count (that's basically what the ReleaseComObject()
call does)
and then sets the reference to the COM object to nothing.
At this point we should have totally released the document and you
should be able to delete it.
Worked for me,
Hope it works for you.
Alan.
Application class
=================
Public Class AdobeApplication
Implements IDisposable
#Region "Constants"
'
========================================================================
'* AVZoomType -- Variable zoom "verbs", corresponding to View menu
items *'
Public Const AVZoomNoVary As Short = 0 ' no variable zoom - use
this for XYZ zoom
Public Const AVZoomFitPage As Short = 1 ' fit page to window
Public Const AVZoomFitWidth As Short = 2 ' fit page width to window
Public Const AVZoomFitHeight As Short = 3 ' fit page height to
window
Public Const AVZoomFitVisibleWidth As Short = 4 ' fit visible width
to window
Public Const AVZoomPreferred As Short = 5 '/* use page's preferred
zoom */
Public Const AV_EXTERNAL_VIEW As Short = 1 ' Open the document with
tool bar visible
Public Const AV_DOC_VIEW As Short = 2 ' Draw the page pane and
scrollbars
Public Const AV_PAGE_VIEW As Short = 4 ' Draw only the page pane
'********************************* PD Things
***********************************'
'* PDPageMode -- Variable for how the file opens - bookmarks,
thumbnails, full screen, none *'
Public Const PDDontCare As Short = 0
Public Const PDUseNone As Short = 1
Public Const PDUseThumbs As Short = 2
Public Const PDUseBookmarks As Short = 3
Public Const PDFullScreen As Short = 4
'* PDLayoutMode -- Variable for how the file is opened - single
page, one column, two column *'
Public Const PDLayoutDontCare As Short = 0
Public Const PDLayoutSinglePage As Short = 1
Public Const PDLayoutOneColumn As Short = 2
Public Const PDLayoutTwoColumnLeft As Short = 3
Public Const PDLayoutTwoColumnRight As Short = 4
'
========================================================================
Public Const MIN_ZOOM_PCT As Integer = 50
Public Const MAX_ZOOM_PCT As Integer = 600
Public Const ERR_MSG_INVALID_ZOOM As String = _
"Allowable Zoom Percentages are {0}% to {1}%."
'
========================================================================
Public Const CLASS_NAME_APPLICATION As String = "AcroExch.App"
Public Const CLASS_NAME_AVDOC As String = "AcroExch.AVDoc"
Public Const CLASS_NAME_PDDOC As String = "AcroExch.PDDoc"
Public Const CLASS_NAME_PDPAGE As String = "AcroExch.PDPage"
Public Const CLASS_NAME_ACRO_RECT As String = "AcroExch.Rect"
Private Const ERR_MSG_CREATE_FAIL As String = _
"Unable to create an instance of the Acrobat application
because: {0}"
Private Const ERR_MSG_UNKNOWN As String = _
"Unknown Reason."
Private Const ERR_MSG_UNABLE_TO_LOCK As String = _
"Unable to lock the Acrobat Instance."
Private Const ERR_MSG_UNABLE_TO_CREATE_DOC As String = _
"Unable to create the document object."
Private Const ERR_MSG_UNABLE_TO_OPEN_FILE As String = _
"Unable to open the {0}. Reason = {1}."
#End Region
#Region "ctors"
Public Sub New()
Me.New(False)
End Sub
Public Sub New( _
ByVal bLock As Boolean _
)
_acroApp = GetAcrobatAppInstance()
_appInstanceName = String.Empty
If (bLock) Then
_locked = CType(_acroApp.Lock(AppInstanceName), Boolean)
If (Not _locked) Then
Throw New
Exception(String.Format(ERR_MSG_UNABLE_TO_LOCK))
End If
End If
End Sub
#End Region
#Region "Properties"
Public ReadOnly Property AppInstanceName() As String
Get
If (_appInstanceName Is Nothing _
OrElse _appInstanceName.Length = 0) Then
_appInstanceName = CreateAppInstanceName()
End If
Return _appInstanceName
End Get
End Property
#End Region
#Region "Methods"
Public Function LoadPDDocument( _
ByVal fName As String _
) As Acrobat.CAcroPDDoc
Dim pdDoc As Acrobat.CAcroPDDoc
pdDoc = CType(CreateObject(CLASS_NAME_PDDOC),
Acrobat.CAcroPDDoc)
If (pdDoc Is Nothing) Then
Throw New
Exception(String.Format(ERR_MSG_UNABLE_TO_CREATE_DOC))
End If
Dim bOK As Boolean
Dim strReason As String = ERR_MSG_UNKNOWN
Dim except As Exception
Try
bOK = CType(pdDoc.Open(fName), Boolean)
Catch ex As Exception
strReason = ex.Message
End Try
If (Not bOK) Then
Throw New
Exception(String.Format(ERR_MSG_UNABLE_TO_OPEN_FILE, _
fName, _
strReason), _
except)
End If
Return pdDoc
End Function
Private Function CreateAppInstanceName() As String
Return System.Guid.NewGuid.ToString
End Function
' creates an application instance and returns it.
' throws an exception if unable to create
Private Function GetAcrobatAppInstance() As Acrobat.CAcroApp
Dim appInstance As Acrobat.CAcroApp
Dim strReason As String = String.Empty
Try
appInstance = CType(CreateObject(CLASS_NAME_APPLICATION),
Acrobat.CAcroApp)
If (appInstance Is Nothing) Then
strReason = ERR_MSG_UNKNOWN
End If
Catch ex As Exception
strReason = ex.Message
Finally
If (strReason.Length > 0) Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(appInstance)
Throw New Exception(String.Format(ERR_MSG_CREATE_FAIL,
strReason))
End If
End Try
Return appInstance
End Function
#End Region
#Region "Attributes"
Private _locked As Boolean
Private _appInstanceName As String
Private _acroApp As Acrobat.CAcroApp
#End Region
#Region " IDISPOSABLE IMPLEMENTATION "
#Region "Methods"
Protected Overrides Sub Finalize()
Dispose(False)
End Sub
Public Overloads Sub Dispose() Implements
System.IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Public Overloads Sub Dispose( _
ByVal bDisposing As Boolean _
)
If (Not _disposed) Then
If (bDisposing) Then
'disposed Managed resources
End If
' dispose unmanaged resources
If (_locked) Then
_acroApp.UnlockEx(AppInstanceName)
End If
System.Runtime.InteropServices.Marshal.ReleaseComObject(_acroApp)
End If
_disposed = True
End Sub
#End Region
#Region "Attributes"
Private _disposed As Boolean
#End Region
#End Region
#Region "Shared Functionality"
#Region "Methods"
Public Shared Function IsValidZoomPct( _
ByVal zoomPct As Integer _
) As Boolean
Return (zoomPct >= MIN_ZOOM_PCT _
And zoomPct <= MAX_ZOOM_PCT)
End Function
Public Shared Function InvalidZoomPctMessage() As String
Return String.Format(ERR_MSG_INVALID_ZOOM, MIN_ZOOM_PCT,
MAX_ZOOM_PCT)
End Function
#End Region
#End Region
End Class
Document Class:
===============
Public Class AdobeDocument
Implements IDisposable
#Region "Konstants"
Private Const ERR_MSG_UNKNOWN As String = _
"Unknown Reason."
Private Const ERR_MSG_UNABLE_TO_CREATE_PDDOC As String = _
"Unable to create the PD Document object."
Private Const ERR_MSG_UNABLE_TO_CREATE_AVDOC As String = _
"Unable to create the AV Document object."
Private Const ERR_MSG_UNABLE_TO_OPEN_FILE As String = _
"Unable to open the {0}. Reason = {1}."
Private Const ERR_MSG_NO_SIZE As String = _
"Unable to generate a size for the page."
Private Const ERR_MSG_UNABLE_TO_CREATE_PAGE As String = _
"Unable to read page {0} for the document."
#End Region
#Region "Ctors"
Public Sub New( _
ByVal fName As String, _
ByVal createCopy As Boolean _
)
Me.New(fName, createCopy, String.Empty)
End Sub
Public Sub New( _
ByVal fName As String, _
ByVal createCopy As Boolean, _
ByVal storagePath As String _
)
Try
_doc = New TemporaryFile(fName, createCopy, storagePath)
_pdDoc = LoadPDDocument(_doc.FileName)
_avDoc = LoadAVDocument(_doc.FileName)
Finally
If (_pdDoc Is Nothing OrElse _avDoc Is Nothing OrElse _doc
Is Nothing) Then
Dispose(True)
End If
End Try
End Sub
#End Region
#Region "Properties"
Public ReadOnly Property PageCount() As Integer
Get
Return _pdDoc.GetNumPages
End Get
End Property
Public Property DeleteOnClose() As Boolean
Get
Return _doc.DeleteOnClose
End Get
Set(ByVal Value As Boolean)
_doc.DeleteOnClose = Value
End Set
End Property
#End Region
#Region "Methods"
Public Overloads Function PrintSilent() As Boolean
Return PrintSilent(1, PageCount)
End Function
Public Overloads Function PrintSilent( _
ByVal nStartpage As Integer, _
ByVal nEndPage As Integer _
) As Boolean
Dim bRet As Boolean
bRet = CType(_avDoc.PrintPages(nStartpage - 1, _
nEndPage - 1, _
0, _
0, _
0), Boolean)
Return bRet
End Function
Private Function LoadAVDocument( _
ByVal fname As String _
) As Acrobat.CAcroAVDoc
' Create the doc object
Dim avDoc As Acrobat.CAcroAVDoc
avDoc = CType(CreateObject(AdobeApplication.CLASS_NAME_AVDOC),
Acrobat.CAcroAVDoc)
If (avDoc Is Nothing) Then
Throw New
Exception(String.Format(ERR_MSG_UNABLE_TO_CREATE_AVDOC))
End If
' load the file
Dim bOK As Boolean
Dim strReason As String = ERR_MSG_UNKNOWN
Dim except As Exception
Try
bOK = CType(avDoc.Open(fname, fname + "tmp"), Boolean)
Catch ex As Exception
strReason = ex.Message
End Try
If (Not bOK) Then
Throw New
Exception(String.Format(ERR_MSG_UNABLE_TO_OPEN_FILE, _
fname, _
strReason), _
except)
End If
Return avDoc
End Function
'Create the PDDocument Object and loads the file
Private Function LoadPDDocument( _
ByVal fName As String _
) As Acrobat.CAcroPDDoc
' Create the doc object
Dim pdDoc As Acrobat.CAcroPDDoc
pdDoc = CType(CreateObject(AdobeApplication.CLASS_NAME_PDDOC),
Acrobat.CAcroPDDoc)
If (pdDoc Is Nothing) Then
Throw New
Exception(String.Format(ERR_MSG_UNABLE_TO_CREATE_PDDOC))
End If
' load the file
Dim bOK As Boolean
Dim strReason As String = ERR_MSG_UNKNOWN
Dim except As Exception
Try
bOK = CType(pdDoc.Open(fName), Boolean)
Catch ex As Exception
strReason = ex.Message
End Try
If (Not bOK) Then
Throw New
Exception(String.Format(ERR_MSG_UNABLE_TO_OPEN_FILE, _
fName, _
strReason), _
except)
End If
Return pdDoc
End Function
Public Function GetPage( _
ByVal nPageNbr As Integer _
) As Acrobat.CAcroPDPage
Dim pdPage As Acrobat.CAcroPDPage
If (Not _pdDoc Is Nothing) Then
pdPage = CType(_pdDoc.AcquirePage(nPageNbr),
Acrobat.CAcroPDPage)
If (pdPage Is Nothing) Then
Throw New
Exception(String.Format(ERR_MSG_UNABLE_TO_CREATE_PAGE, _
nPageNbr))
End If
End If
Return pdPage
End Function
#End Region
#Region "Attributes"
Private _pdDoc As Acrobat.CAcroPDDoc
Private _avDoc As Acrobat.CAcroAVDoc
Private _doc As TemporaryFile
#End Region
#Region " IDISPOSABLE IMPLEMENTATION "
#Region "Methods"
Public Overloads Sub Dispose() Implements
System.IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Public Overloads Sub Dispose( _
ByVal bDisposing As Boolean _
)
If (Not _disposed) Then
' need to do this one before the _doc as the _doc will try
to delete the file
' Don't know that we can ever get here with a Nothing value
in _pdDdoc
' but it costs little to check
If (Not _pdDoc Is Nothing) Then
Trace.WriteLine("Releasing the PD DOC")
_pdDoc.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(_pdDoc)
_pdDoc = Nothing
End If
If Not _avDoc Is Nothing Then
Trace.WriteLine("Releasing the AV DOC")
_avDoc.Close(1)
System.Runtime.InteropServices.Marshal.ReleaseComObject(_avDoc)
_avDoc = Nothing
End If
If (bDisposing) Then
_doc.Dispose()
End If
End If
_disposed = True
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
End Sub
#End Region
#Region "Attributes"
Private _disposed As Boolean
#End Region
#End Region
End Class