943,095 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 1509
  • VB.NET RSS
Jan 14th, 2010
0

Acrobat 7 not closing when open with OLE

Expand Post »
Hi All,

I'm writing an app that prints of batches of documents to clients for posting. The printer uses OMR to collate and fold the documents into envelopes automatically.

The document inputs will either be Word or Adobe Acrobat. I have routines to print off each document type while adding OMR on the fly.

The issue I have is that when the routine for PDF is run, the Adobe file is not closing properly. So when the next clients batch is being printed, there is an error saying that the document is locked by another user or process.

I'm using the Acrobat 7 reference library and have Acrobat 7 installed on my machine (full blown product, not reader,)

I'm closing the PDDocument, the AVDocument and the application in the code and I've even wrote a routine to kill the process to no avail.

Basically my code copies the source file into a new file.
It then parses through the pages building a string called strOMR in the format OMR_?_SeqNO for each page.
Where SeqNo is a number between 0 and 7 relating to the position of the page in the overall batch of documents. If on the last page of the last document, then ? is given the value I (for insert) else S.

I now look in a template of precreated PDF Annots containing the necessary OMR markup and copy the one thats title matches the string I have built onto the page.


VB.NET Syntax (Toggle Plain Text)
  1. Private sub PrintAdobe(sFile as string)
  2. Dim AcroAVDoc, AcroAVDoc1 As Acrobat.AcroAVDoc
  3. Dim AcroPDDoc, AcroPDDoc1 As Acrobat.AcroPDDoc
  4. Dim srcPage, destPage As Acrobat.AcroPDPage
  5. Dim srcFile, destFile, strOMR As String
  6. Dim intPage, destPageNum, intSeqNo, intAnnot, PageHeight As Integer
  7. Dim srcAnnot, destAnnot As AcroPDAnnot
  8. Dim AcroRect As AcroRect
  9. Dim PageSize As Object
  10. On Error GoTo errHandler
  11. If Not File.Exists(sFile) Then
  12. Exit Sub
  13. End If
  14. destFile = Replace(sFile, ".pdf", "_OMR.pdf")
  15. If File.Exists(destFile) Then File.Delete(destFile)
  16. File.Copy(sFile, destFile)
  17. AcroAVDoc = CreateObject("AcroExch.AVDoc")
  18. AcroAVDoc.Open(destFile, Dir(destFile))
  19. AcroPDDoc = AcroAVDoc.GetPDDoc
  20. AcroAVDoc1 = CreateObject("AcroExch.AVDoc")
  21. AcroAVDoc1.Open("\\...\Templates\OMR Template.pdf", "OMR Template.pdf")
  22. AcroPDDoc1 = AcroAVDoc1.GetPDDoc
  23. AcroPDDoc.InsertPages(-2, AcroPDDoc1, 0, 1, False)
  24. destPageNum = AcroPDDoc.GetNumPages - 1
  25. srcPage = AcroPDDoc.AcquirePage(destPageNum)
  26. For intPage = 0 To destPageNum - 1
  27. strOMR = "OMR_"
  28. If intPage = destPageNum - 1 Then
  29. 'insert
  30. strOMR = strOMR & "I_"
  31. Else
  32. 'store
  33. strOMR = strOMR & "S_"
  34. End If
  35. strOMR = strOMR & intSeqNo
  36. destPage = AcroPDDoc.AcquirePage(intPage)
  37. 'get size of page, N.B. adobe grid starts in bottom left corner
  38. PageSize = destPage.GetSize()
  39. 'Assume 72 pt per inch - from adobe example A4 =842.4
  40. PageHeight = PageSize.y
  41. For intAnnot = 0 To srcPage.GetNumAnnots - 1
  42. srcAnnot = srcPage.GetAnnot(intAnnot)
  43. If strOMR = srcAnnot.GetTitle Then
  44. destPage.AddAnnot(-2, srcAnnot)
  45. destAnnot = destPage.GetAnnot(0)
  46. AcroRect = destAnnot.GetRect
  47. With AcroRect
  48. .Left = 3.3
  49. .right = 54
  50. If PageHeight > 253.85 Then
  51. .bottom = PageHeight - 253.85
  52. .Top = PageHeight - 163.75
  53. End If
  54. End With
  55. destAnnot.SetRect(AcroRect)
  56. AcroRect = Nothing
  57. destAnnot = Nothing
  58. srcAnnot = Nothing
  59. intSeqNo = intSeqNo + 1
  60. If intSeqNo > 7 Then intSeqNo = 0
  61. System.Threading.Thread.Sleep(1000)
  62. Exit For
  63. End If
  64. Next
  65. Next
  66. AcroPDDoc.DeletePages(destPageNum, destPageNum)
  67. AcroPDDoc.Save(PDSaveFlags.PDSaveFull, destFile)
  68.  
  69. Out:
  70. 'general housekeeping close everything and set it all back to normal now we've finished
  71. If Not AcroPDDoc1 Is Nothing Then
  72. AcroPDDoc1.Close()
  73. AcroPDDoc1 = Nothing
  74. End If
  75. If Not AcroAVDoc1 Is Nothing Then
  76. AcroAVDoc1.Close(True)
  77. AcroAVDoc1 = Nothing
  78. End If
  79. If Not AcroPDDoc Is Nothing Then
  80. AcroPDDoc.Close()
  81. AcroPDDoc = Nothing
  82. End If
  83. If Not AcroAVDoc Is Nothing Then
  84. AcroAVDoc.Close(True)
  85. AcroAVDoc = Nothing
  86. End If
  87. KillAcrobat()
  88. Exit Sub
  89. errHandler:
  90. On Error Resume Next
  91. MsgBox("PDF Print error:" & Err.Number & ":" & Err.Description, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
  92. Resume Out
  93. End Sub

I have wrote a routine to go through the system processes on my machine and kill it off but it still sits there.....

VB.NET Syntax (Toggle Plain Text)
  1. Public Sub KillAcrobat()
  2. Dim myProcess As Process
  3. On Error Resume Next
  4. If Process.GetProcessesByName("Acrobat").Length > 0 Then
  5. 'acrobat is still open
  6. Do Until Process.GetProcessesByName("Acrobat").Length = 0
  7. 'get each instance of Acrobat
  8. For Each myProcess In Process.GetProcessesByName("Acrobat")
  9. 'Attempt to close it
  10. myProcess.Close()
  11. Threading.Thread.Sleep(1000) 'wait either a second
  12. If Not myProcess.HasExited Then 'still not gone
  13. myProcess.Kill() ' force closure
  14. End If
  15. myProcess = Nothing
  16. Next
  17. Loop
  18. End If
  19. myProcess = Nothing
  20. End Sub
Similar Threads
Reputation Points: 37
Solved Threads: 24
Posting Whiz in Training
G_Waddell is offline Offline
207 posts
since Nov 2009
Jan 20th, 2010
0
Re: Acrobat 7 not closing when open with OLE
I've given up and no longer allow the users to specify OMR output with PDF documents.

I even tried encapsulating the whole PDF printing in a class object and setting up dispose methods but the same document remained open.
Reputation Points: 37
Solved Threads: 24
Posting Whiz in Training
G_Waddell is offline Offline
207 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Richtextbox-how to limit input of text
Next Thread in VB.NET Forum Timeline: Detecting Processor Arcitecture





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC