Adobe acrobat doesn't close my files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joris De Groote

    Adobe acrobat doesn't close my files?

    Hi,

    I use Adobe Acrobat to read tekst from PDF files. After that the file has
    been read, I move the file in a folder (using the date I got from the text I
    got from Acrobat). Now here is my problem. When I want to move the file, I
    get an error stating:
    System.IO.IOExc eption: The process cannot access the file
    "x:\VF\2006-01\CVF-06000007.pdf" because it is being used by another
    process.

    Acrobat just doesn't want to close the file after it has been readed. I have
    tried a few things like : (AcroApp is my AcroExch.App Object)

    AcroApp.Close()
    AcroApp.CloseAl lDocs()
    AcroApp.Exit()

    However none seem to work. Can anybody help my close the file so the program
    can move the pdf to where it belongs?

    Thanks
    Joris


  • AlanT

    #2
    Re: Adobe acrobat doesn't close my files?


    I had similar problems working with PDFs using Acrobat 7.0

    Are you using the Acrobat.CAcroPD Doc and/or Acrobat.CAcroAV Doc classes
    for opening/reading the document?

    If yes,
    Are you closing these when you are finished with them?
    Are you using IDisposable so that you control when they are
    released?

    The above
    AcroApp.Close()
    AcroApp.CloseAl lDocs()
    AcroApp.Exit()

    will close the docs 'as far as Acrobat is concerned' but if you
    have a reference to a document in your code the document will still be
    used 'as far as the OS is concerned' until the reference releases -
    which is why I ended up implementing IDisposable to control when they
    released.


    hth,
    Alan.

    Comment

    • AlanT

      #3
      Re: Adobe acrobat doesn't close my files?

      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(f Name, createCopy, storagePath)



      And then when closing it I would dispose of it



      _adobeDoc.Dispo se()

      _adobeDoc = nothing





      The Dispose calls close on the avDoc and pdDoc and also decrements the
      COM reference count (that's basically what the ReleaseComObjec t()
      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 AdobeApplicatio n

      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 AVZoomFitVisibl eWidth As Short = 4 ' fit visible width
      to window

      Public Const AVZoomPreferred As Short = 5 '/* use page's preferred
      zoom */





      Public Const AV_EXTERNAL_VIE W 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 PDLayoutDontCar e As Short = 0

      Public Const PDLayoutSingleP age As Short = 1

      Public Const PDLayoutOneColu mn As Short = 2

      Public Const PDLayoutTwoColu mnLeft As Short = 3

      Public Const PDLayoutTwoColu mnRight 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_APPL ICATION As String = "AcroExch.A pp"

      Public Const CLASS_NAME_AVDO C As String = "AcroExch.AVDoc "

      Public Const CLASS_NAME_PDDO C As String = "AcroExch.PDDoc "

      Public Const CLASS_NAME_PDPA GE As String = "AcroExch.PDPag e"

      Public Const CLASS_NAME_ACRO _RECT As String = "AcroExch.R ect"



      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 = GetAcrobatAppIn stance()

      _appInstanceNam e = String.Empty



      If (bLock) Then

      _locked = CType(_acroApp. Lock(AppInstanc eName), Boolean)

      If (Not _locked) Then

      Throw New
      Exception(Strin g.Format(ERR_MS G_UNABLE_TO_LOC K))

      End If

      End If



      End Sub



      #End Region



      #Region "Properties "



      Public ReadOnly Property AppInstanceName () As String

      Get

      If (_appInstanceNa me Is Nothing _

      OrElse _appInstanceNam e.Length = 0) Then

      _appInstanceNam e = CreateAppInstan ceName()

      End If

      Return _appInstanceNam e

      End Get

      End Property



      #End Region



      #Region "Methods"



      Public Function LoadPDDocument( _

      ByVal fName As String _

      ) As Acrobat.CAcroPD Doc



      Dim pdDoc As Acrobat.CAcroPD Doc

      pdDoc = CType(CreateObj ect(CLASS_NAME_ PDDOC),
      Acrobat.CAcroPD Doc)



      If (pdDoc Is Nothing) Then

      Throw New
      Exception(Strin g.Format(ERR_MS G_UNABLE_TO_CRE ATE_DOC))

      End If



      Dim bOK As Boolean

      Dim strReason As String = ERR_MSG_UNKNOWN

      Dim except As Exception



      Try

      bOK = CType(pdDoc.Ope n(fName), Boolean)

      Catch ex As Exception

      strReason = ex.Message

      End Try



      If (Not bOK) Then

      Throw New
      Exception(Strin g.Format(ERR_MS G_UNABLE_TO_OPE N_FILE, _

      fName, _

      strReason), _

      except)

      End If



      Return pdDoc



      End Function



      Private Function CreateAppInstan ceName() As String

      Return System.Guid.New Guid.ToString

      End Function



      ' creates an application instance and returns it.

      ' throws an exception if unable to create

      Private Function GetAcrobatAppIn stance() As Acrobat.CAcroAp p



      Dim appInstance As Acrobat.CAcroAp p



      Dim strReason As String = String.Empty

      Try

      appInstance = CType(CreateObj ect(CLASS_NAME_ APPLICATION),
      Acrobat.CAcroAp p)

      If (appInstance Is Nothing) Then

      strReason = ERR_MSG_UNKNOWN

      End If

      Catch ex As Exception

      strReason = ex.Message

      Finally

      If (strReason.Leng th > 0) Then


      System.Runtime. InteropServices .Marshal.Releas eComObject(appI nstance)

      Throw New Exception(Strin g.Format(ERR_MS G_CREATE_FAIL,
      strReason))

      End If

      End Try



      Return appInstance



      End Function





      #End Region



      #Region "Attributes "

      Private _locked As Boolean

      Private _appInstanceNam e As String

      Private _acroApp As Acrobat.CAcroAp p

      #End Region





      #Region " IDISPOSABLE IMPLEMENTATION "



      #Region "Methods"



      Protected Overrides Sub Finalize()

      Dispose(False)

      End Sub



      Public Overloads Sub Dispose() Implements
      System.IDisposa ble.Dispose

      Dispose(True)

      GC.SuppressFina lize(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.Unlock Ex(AppInstanceN ame)

      End If


      System.Runtime. InteropServices .Marshal.Releas eComObject(_acr oApp)



      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 InvalidZoomPctM essage() As String

      Return String.Format(E RR_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(f Name, 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.GetNumPa ges

      End Get

      End Property



      Public Property DeleteOnClose() As Boolean

      Get

      Return _doc.DeleteOnCl ose

      End Get

      Set(ByVal Value As Boolean)

      _doc.DeleteOnCl ose = 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.Pr intPages(nStart page - 1, _

      nEndPage - 1, _

      0, _

      0, _

      0), Boolean)

      Return bRet



      End Function





      Private Function LoadAVDocument( _

      ByVal fname As String _

      ) As Acrobat.CAcroAV Doc



      ' Create the doc object

      Dim avDoc As Acrobat.CAcroAV Doc

      avDoc = CType(CreateObj ect(AdobeApplic ation.CLASS_NAM E_AVDOC),
      Acrobat.CAcroAV Doc)



      If (avDoc Is Nothing) Then

      Throw New
      Exception(Strin g.Format(ERR_MS G_UNABLE_TO_CRE ATE_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.Ope n(fname, fname + "tmp"), Boolean)

      Catch ex As Exception

      strReason = ex.Message

      End Try



      If (Not bOK) Then



      Throw New
      Exception(Strin g.Format(ERR_MS G_UNABLE_TO_OPE N_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.CAcroPD Doc



      ' Create the doc object

      Dim pdDoc As Acrobat.CAcroPD Doc

      pdDoc = CType(CreateObj ect(AdobeApplic ation.CLASS_NAM E_PDDOC),
      Acrobat.CAcroPD Doc)



      If (pdDoc Is Nothing) Then

      Throw New
      Exception(Strin g.Format(ERR_MS G_UNABLE_TO_CRE ATE_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.Ope n(fName), Boolean)

      Catch ex As Exception

      strReason = ex.Message

      End Try



      If (Not bOK) Then



      Throw New
      Exception(Strin g.Format(ERR_MS G_UNABLE_TO_OPE N_FILE, _

      fName, _

      strReason), _

      except)

      End If



      Return pdDoc



      End Function





      Public Function GetPage( _

      ByVal nPageNbr As Integer _

      ) As Acrobat.CAcroPD Page



      Dim pdPage As Acrobat.CAcroPD Page



      If (Not _pdDoc Is Nothing) Then



      pdPage = CType(_pdDoc.Ac quirePage(nPage Nbr),
      Acrobat.CAcroPD Page)

      If (pdPage Is Nothing) Then

      Throw New
      Exception(Strin g.Format(ERR_MS G_UNABLE_TO_CRE ATE_PAGE, _

      nPageNbr))

      End If

      End If



      Return pdPage



      End Function





      #End Region





      #Region "Attributes "



      Private _pdDoc As Acrobat.CAcroPD Doc

      Private _avDoc As Acrobat.CAcroAV Doc

      Private _doc As TemporaryFile



      #End Region





      #Region " IDISPOSABLE IMPLEMENTATION "



      #Region "Methods"



      Public Overloads Sub Dispose() Implements
      System.IDisposa ble.Dispose

      Dispose(True)

      GC.SuppressFina lize(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.Releas eComObject(_pdD oc)

      _pdDoc = Nothing

      End If



      If Not _avDoc Is Nothing Then

      Trace.WriteLine ("Releasing the AV DOC")

      _avDoc.Close(1)


      System.Runtime. InteropServices .Marshal.Releas eComObject(_avD oc)

      _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

      Comment

      • Joris De Groote

        #4
        Re: Adobe acrobat doesn't close my files?

        Hi, thanks a lot!! it works now.
        I read your code and studied it and I added this:

        PDDoc.Close()

        System.Runtime. InteropServices .Marshal.Releas eComObject(PDDo c)

        avDoc.Close(1)

        System.Runtime. InteropServices .Marshal.Releas eComObject(avDo c)

        I stayed of the IDisposable things... That gave me quite a few problems
        first.

        Greetz
        Joris


        "AlanT" <alantolan@user s.com> wrote in message
        news:1144277083 .284521.20140@g 10g2000cwb.goog legroups.com...[color=blue]
        > 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(f Name, createCopy, storagePath)
        >
        >
        >
        > And then when closing it I would dispose of it
        >
        >
        >
        > _adobeDoc.Dispo se()
        >
        > _adobeDoc = nothing
        >
        >
        >
        >
        >
        > The Dispose calls close on the avDoc and pdDoc and also decrements the
        > COM reference count (that's basically what the ReleaseComObjec t()
        > 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 AdobeApplicatio n
        >
        > 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 AVZoomFitVisibl eWidth As Short = 4 ' fit visible width
        > to window
        >
        > Public Const AVZoomPreferred As Short = 5 '/* use page's preferred
        > zoom */
        >
        >
        >
        >
        >
        > Public Const AV_EXTERNAL_VIE W 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 PDLayoutDontCar e As Short = 0
        >
        > Public Const PDLayoutSingleP age As Short = 1
        >
        > Public Const PDLayoutOneColu mn As Short = 2
        >
        > Public Const PDLayoutTwoColu mnLeft As Short = 3
        >
        > Public Const PDLayoutTwoColu mnRight 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_APPL ICATION As String = "AcroExch.A pp"
        >
        > Public Const CLASS_NAME_AVDO C As String = "AcroExch.AVDoc "
        >
        > Public Const CLASS_NAME_PDDO C As String = "AcroExch.PDDoc "
        >
        > Public Const CLASS_NAME_PDPA GE As String = "AcroExch.PDPag e"
        >
        > Public Const CLASS_NAME_ACRO _RECT As String = "AcroExch.R ect"
        >
        >
        >
        > 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 = GetAcrobatAppIn stance()
        >
        > _appInstanceNam e = String.Empty
        >
        >
        >
        > If (bLock) Then
        >
        > _locked = CType(_acroApp. Lock(AppInstanc eName), Boolean)
        >
        > If (Not _locked) Then
        >
        > Throw New
        > Exception(Strin g.Format(ERR_MS G_UNABLE_TO_LOC K))
        >
        > End If
        >
        > End If
        >
        >
        >
        > End Sub
        >
        >
        >
        > #End Region
        >
        >
        >
        > #Region "Properties "
        >
        >
        >
        > Public ReadOnly Property AppInstanceName () As String
        >
        > Get
        >
        > If (_appInstanceNa me Is Nothing _
        >
        > OrElse _appInstanceNam e.Length = 0) Then
        >
        > _appInstanceNam e = CreateAppInstan ceName()
        >
        > End If
        >
        > Return _appInstanceNam e
        >
        > End Get
        >
        > End Property
        >
        >
        >
        > #End Region
        >
        >
        >
        > #Region "Methods"
        >
        >
        >
        > Public Function LoadPDDocument( _
        >
        > ByVal fName As String _
        >
        > ) As Acrobat.CAcroPD Doc
        >
        >
        >
        > Dim pdDoc As Acrobat.CAcroPD Doc
        >
        > pdDoc = CType(CreateObj ect(CLASS_NAME_ PDDOC),
        > Acrobat.CAcroPD Doc)
        >
        >
        >
        > If (pdDoc Is Nothing) Then
        >
        > Throw New
        > Exception(Strin g.Format(ERR_MS G_UNABLE_TO_CRE ATE_DOC))
        >
        > End If
        >
        >
        >
        > Dim bOK As Boolean
        >
        > Dim strReason As String = ERR_MSG_UNKNOWN
        >
        > Dim except As Exception
        >
        >
        >
        > Try
        >
        > bOK = CType(pdDoc.Ope n(fName), Boolean)
        >
        > Catch ex As Exception
        >
        > strReason = ex.Message
        >
        > End Try
        >
        >
        >
        > If (Not bOK) Then
        >
        > Throw New
        > Exception(Strin g.Format(ERR_MS G_UNABLE_TO_OPE N_FILE, _
        >
        > fName, _
        >
        > strReason), _
        >
        > except)
        >
        > End If
        >
        >
        >
        > Return pdDoc
        >
        >
        >
        > End Function
        >
        >
        >
        > Private Function CreateAppInstan ceName() As String
        >
        > Return System.Guid.New Guid.ToString
        >
        > End Function
        >
        >
        >
        > ' creates an application instance and returns it.
        >
        > ' throws an exception if unable to create
        >
        > Private Function GetAcrobatAppIn stance() As Acrobat.CAcroAp p
        >
        >
        >
        > Dim appInstance As Acrobat.CAcroAp p
        >
        >
        >
        > Dim strReason As String = String.Empty
        >
        > Try
        >
        > appInstance = CType(CreateObj ect(CLASS_NAME_ APPLICATION),
        > Acrobat.CAcroAp p)
        >
        > If (appInstance Is Nothing) Then
        >
        > strReason = ERR_MSG_UNKNOWN
        >
        > End If
        >
        > Catch ex As Exception
        >
        > strReason = ex.Message
        >
        > Finally
        >
        > If (strReason.Leng th > 0) Then
        >
        >
        > System.Runtime. InteropServices .Marshal.Releas eComObject(appI nstance)
        >
        > Throw New Exception(Strin g.Format(ERR_MS G_CREATE_FAIL,
        > strReason))
        >
        > End If
        >
        > End Try
        >
        >
        >
        > Return appInstance
        >
        >
        >
        > End Function
        >
        >
        >
        >
        >
        > #End Region
        >
        >
        >
        > #Region "Attributes "
        >
        > Private _locked As Boolean
        >
        > Private _appInstanceNam e As String
        >
        > Private _acroApp As Acrobat.CAcroAp p
        >
        > #End Region
        >
        >
        >
        >
        >
        > #Region " IDISPOSABLE IMPLEMENTATION "
        >
        >
        >
        > #Region "Methods"
        >
        >
        >
        > Protected Overrides Sub Finalize()
        >
        > Dispose(False)
        >
        > End Sub
        >
        >
        >
        > Public Overloads Sub Dispose() Implements
        > System.IDisposa ble.Dispose
        >
        > Dispose(True)
        >
        > GC.SuppressFina lize(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.Unlock Ex(AppInstanceN ame)
        >
        > End If
        >
        >
        > System.Runtime. InteropServices .Marshal.Releas eComObject(_acr oApp)
        >
        >
        >
        > 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 InvalidZoomPctM essage() As String
        >
        > Return String.Format(E RR_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(f Name, 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.GetNumPa ges
        >
        > End Get
        >
        > End Property
        >
        >
        >
        > Public Property DeleteOnClose() As Boolean
        >
        > Get
        >
        > Return _doc.DeleteOnCl ose
        >
        > End Get
        >
        > Set(ByVal Value As Boolean)
        >
        > _doc.DeleteOnCl ose = 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.Pr intPages(nStart page - 1, _
        >
        > nEndPage - 1, _
        >
        > 0, _
        >
        > 0, _
        >
        > 0), Boolean)
        >
        > Return bRet
        >
        >
        >
        > End Function
        >
        >
        >
        >
        >
        > Private Function LoadAVDocument( _
        >
        > ByVal fname As String _
        >
        > ) As Acrobat.CAcroAV Doc
        >
        >
        >
        > ' Create the doc object
        >
        > Dim avDoc As Acrobat.CAcroAV Doc
        >
        > avDoc = CType(CreateObj ect(AdobeApplic ation.CLASS_NAM E_AVDOC),
        > Acrobat.CAcroAV Doc)
        >
        >
        >
        > If (avDoc Is Nothing) Then
        >
        > Throw New
        > Exception(Strin g.Format(ERR_MS G_UNABLE_TO_CRE ATE_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.Ope n(fname, fname + "tmp"), Boolean)
        >
        > Catch ex As Exception
        >
        > strReason = ex.Message
        >
        > End Try
        >
        >
        >
        > If (Not bOK) Then
        >
        >
        >
        > Throw New
        > Exception(Strin g.Format(ERR_MS G_UNABLE_TO_OPE N_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.CAcroPD Doc
        >
        >
        >
        > ' Create the doc object
        >
        > Dim pdDoc As Acrobat.CAcroPD Doc
        >
        > pdDoc = CType(CreateObj ect(AdobeApplic ation.CLASS_NAM E_PDDOC),
        > Acrobat.CAcroPD Doc)
        >
        >
        >
        > If (pdDoc Is Nothing) Then
        >
        > Throw New
        > Exception(Strin g.Format(ERR_MS G_UNABLE_TO_CRE ATE_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.Ope n(fName), Boolean)
        >
        > Catch ex As Exception
        >
        > strReason = ex.Message
        >
        > End Try
        >
        >
        >
        > If (Not bOK) Then
        >
        >
        >
        > Throw New
        > Exception(Strin g.Format(ERR_MS G_UNABLE_TO_OPE N_FILE, _
        >
        > fName, _
        >
        > strReason), _
        >
        > except)
        >
        > End If
        >
        >
        >
        > Return pdDoc
        >
        >
        >
        > End Function
        >
        >
        >
        >
        >
        > Public Function GetPage( _
        >
        > ByVal nPageNbr As Integer _
        >
        > ) As Acrobat.CAcroPD Page
        >
        >
        >
        > Dim pdPage As Acrobat.CAcroPD Page
        >
        >
        >
        > If (Not _pdDoc Is Nothing) Then
        >
        >
        >
        > pdPage = CType(_pdDoc.Ac quirePage(nPage Nbr),
        > Acrobat.CAcroPD Page)
        >
        > If (pdPage Is Nothing) Then
        >
        > Throw New
        > Exception(Strin g.Format(ERR_MS G_UNABLE_TO_CRE ATE_PAGE, _
        >
        > nPageNbr))
        >
        > End If
        >
        > End If
        >
        >
        >
        > Return pdPage
        >
        >
        >
        > End Function
        >
        >
        >
        >
        >
        > #End Region
        >
        >
        >
        >
        >
        > #Region "Attributes "
        >
        >
        >
        > Private _pdDoc As Acrobat.CAcroPD Doc
        >
        > Private _avDoc As Acrobat.CAcroAV Doc
        >
        > Private _doc As TemporaryFile
        >
        >
        >
        > #End Region
        >
        >
        >
        >
        >
        > #Region " IDISPOSABLE IMPLEMENTATION "
        >
        >
        >
        > #Region "Methods"
        >
        >
        >
        > Public Overloads Sub Dispose() Implements
        > System.IDisposa ble.Dispose
        >
        > Dispose(True)
        >
        > GC.SuppressFina lize(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.Releas eComObject(_pdD oc)
        >
        > _pdDoc = Nothing
        >
        > End If
        >
        >
        >
        > If Not _avDoc Is Nothing Then
        >
        > Trace.WriteLine ("Releasing the AV DOC")
        >
        > _avDoc.Close(1)
        >
        >
        > System.Runtime. InteropServices .Marshal.Releas eComObject(_avD oc)
        >
        > _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
        >[/color]


        Comment

        Working...