Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem searching in PDF with vb

Status
Not open for further replies.

alukard

Technical User
Apr 15, 2003
7
US
I have an access database, and have it setup to automate the printing of a few reports for storage in a PDFs.
It prints 2 reports to a temp directory.
Merges them into a final PDF.
Then opens it for the user to scan in extra files (usually drawings marked up with inspection problems).
He alsways sets the pages in the exact order (Access Report 1(1-2 pages long), Access Report 2(always 1 page), scans)

We wanted to make it so he could replace the pages printed from the Access reports.
Since there is scanned items in these PDFs i cant just reprint it or he will loose the scanned pages.
So i am going to just print the new pdf page to a temp file and do a page replacement, then delete the temp PDF (which i have working code for).
Each of the pages from the Access reports contains hidden text, so i can just search for it within a PDF, get that pages number and then use it to replace the pages.

The problem i have is the code im using to search the PDF doesnt always work. Sometimes it works great and i get my page value(s), but sometimes it just locks up, and i have to go to the Task Manager and kill Acrobat.exe. Then it will unfreeze Access and give me an error.
It seems to work more often if i only run it once, but i need to run it twice though. Once to find the last report page in the original PDF and then again to find the last page(in case the report prints 2 pages) in the replacement PDF. Then after this im going to compare them to determine the appropriate way to replace the page(s)

these are what the variables are for
strDir = directory PDF to search is in. (ex. c:\temp\)
strFileName = filename (ex. 1055.pdf)
strSearch = (ex. xxxyyyzzz)

Public Function SearchPDF(strDir As String, strFileName As String, strSearch As String) As Long

Dim AcroApp As Object
Dim AcroAvDoc As Object
Dim AVPage As CAcroAVPageView
Dim bFound As Boolean

Set AcroApp = CreateObject("AcroExch.App")
Set AcroAvDoc = CreateObject("AcroExch.AVDoc")
Call AcroAvDoc.Open(strDir & strFileName, "")

Set AcroAvDoc = AcroApp.GetActiveDoc

If AcroAvDoc.IsValid Then

bFound = AcroAvDoc.FindText(strSearch, 0, 0, 1) '<----this is the line it highlights after i kill acrobat.exe

If bFound = True Then
Set AVPage = AcroAvDoc.GetAVPageView
SearchPDF = AVPage.GetPageNum
Set AVPage = Nothing
End If

End If
AcroAvDoc.Close True
AcroApp.Exit

End Function
 
I decided to add the button that triggers this code to see if it sparks any ideas.
This is from a form with nothing but a command button and a a frame with 4 options(cases 1-4)

Private Sub Command27_Click()

Dim objFSO As Object
Dim strDir(2) As String
Dim rptName(3) As String
Dim strFileName(3) As String
Dim strPDFName(3) As String
Dim icount As Integer
Dim strSearch(2) As String
Dim lngOrigPage(2) As Long
Dim lngRepPage(2) As Long

strDir(1) = Forms!frmIQIR!PDFLocTextBox
strDir(2) = strDir(1) & "IRIQTempXXXYYYZZZ\"

rptName(2) = "rpt IQIR Report Form"
rptName(3) = "rpt IQIRR Letter"

strFileName(1) = Forms!frmIQIR![IQIR #] & ".pdf"
strFileName(2) = "1 Report Form.pdf"
strFileName(3) = "2 IQIRR Leter.pdf"

strPDFName(1) = strDir(1) & strFileName(1)
strPDFName(2) = strDir(2) & strFileName(2)
strPDFName(3) = strDir(2) & strFileName(3)

strSearch(1) = "IQIRReport"
strSearch(2) = "IQIRLetter"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDir(2)) Then
Call DeleteFolder(strDir(2))
MkDir strDir(2)
Else
MkDir strDir(2)
End If

Select Case Me.Frame0 'PDF exist - select option
Case 1 'Open existing PDF

Case 2 'Replace first report in PDF
Call ReportToDistiller(rptName(2), strPDFName(2), strDir(2)) 'Prints new first report

lngOrigPage(1) = SearchPDF(strDir(1), strFileName(1), strSearch(1)) 'Finds end of first report in original PDF

lngOrigPage(2) = SearchPDF(strDir(1), strFileName(1), strSearch(2)) 'Finds second report in original PDF

Case 3 'Replace second report in PDF

Case 4 'Replace both reports in PDF

End Select

End Sub
 
Ok, update, maybe this might spark something. I found out that the SearchPDF Function had some unnecessary lines of code and removing these lines of code prevent it from freezing. I did not need to open the 'AcroApp' object to use 'AvDoc' object. so here is the new version of the function.
**********************************************************************
Public Function SearchPDF(strDir As String, strFileName As String, strSearch As String) As Long

Dim AcroAvDoc As Object
Dim AVPage As CAcroAVPageView
Dim bFound As Boolean

Set AcroAvDoc = CreateObject("AcroExch.AVDoc")
Call AcroAvDoc.Open(strDir & strFileName, "")

If AcroAvDoc.IsValid Then

bFound = AcroAvDoc.FindText(strSearch, 0, 0, 1)

If bFound = True Then
Set AVPage = AcroAvDoc.GetAVPageView
SearchPDF = AVPage.GetPageNum
Set AVPage = Nothing
End If

End If
AcroAvDoc.Close True

End Function
*****************************************************************
This is what is triggered 2 times in 'case 2' in the post above

Now it works, but..... with two searches it takes 2 minutes to complete, exactly 2 minutes every time.
If i do just a single search it takes 1 sec max., but as soon as i add the second search it goes to 2 minutes.

These are the two lines that trigger the searches:
lngOrigPage(1) = SearchPDF(strDir(1), strFileName(1), strSearch(1))
lngOrigPage(2) = SearchPDF(strDir(1), strFileName(1), strSearch(2))

So if i put a ' in front of either line (removing that line from executable code) the code runs with 1 search very fast, but together they take exactly 2 minutes.

I added an else condition to ' If bFound = True Then', so if nothing was found it would return 999 to me, and i found out that the hang up is when it is not finding the string searched for in one or both of the searches.

It is still rare for only a single search to fail and take two minutes, but does happen occasionally. If run twice in a row it fails most of the time returning 999 for one or both the page results.

There is no reason that it should fail, all the variable are hard coded. The text im searching for is in the PDF, and ive used the same PDF this whole time. It does work at times, so i know its not just a poorly entered search string.

I also noticed that acrobat.exe continue to run after the code is done executing, whether it finds the page im looking for or not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top