I am a developer working with PDF-XChange Editor SDK V7 which is used in software our company has been developing in VB.net.
Currently we are working on a new feature of our software which comes after a programmatic search (that doesn't in itself use the PDF-XChange Editor) with a variable set of search terms. The result of that search is generally a list of PDF documents, any of which the user can click on, and at that point PDF-XChange Editor comes in to display the corresponding document. The new feature is to highlight any of the search terms of the preceding search in the PDF document that is being displayed (not all of the matches necessarily occur in the text content of the PDF because the search also is performed on certain metadata other than the PDF content). Everything I've written so far I have been able to achieve.
Now to the part that I am struggling with. When manually searching within a document using its own GUI, the PDF-XChange Editor also allows (via advanced search options) to highlight search results in multiple colors, which we also would very much like to use for highlighting the matches of our programmatical search, but I haven't managed to get that to work.
Admittedly, I am not clear on what the programmatical equivalent of the distinction between "Find" and "Advanced Search" is, if such a distinction exists, and if it does, which one of the both I have been using in my solution.
Here is a stripped-down version of the code I have (with an exemplary list of search terms hard-coded). I have based my solution on the example given in https://sdkhelp.pdf-xchange.com/view/PXV:op_search, converted it to VB.net and made some minor adaptations. I also attempted to employ the solution from this thread, but it made no difference: the search terms are all highlighted in the same color either way.
Code: Select all
...
Friend docPageToSelect As Integer = -1
'helps to make sure that on a multiple-page doc the current page gets set to the first, not the last, one with a search result
...
Friend Sub SearchOperation(Doc As PDFXEdit.IPXV_Document, Inst As PDFXEdit.PXV_Inst)
Dim nID As Integer = Inst.Str2ID("op.search", False)
Dim Op As PDFXEdit.IOperation = Inst.CreateOp(nID)
Dim input As PDFXEdit.ICabNode = Op.Params.Root("Input")
input.Add().v = Doc.CoreDoc
'begin this-does-not-help
Dim b As Boolean = Inst.Settings("Misc.ColoredSearchRes").Bool
If Not b Then
Inst.ExecUICmd("cmd.search.coloredResults")
End If
'end this-does-not-help
Dim options As PDFXEdit.ICabNode = Op.Params.Root("Options")
options("OR").Add().v = "quick"
options("OR").Add().v = "fox"
options("OR").Add().v = "alfuqarlgfbqaewr" 'does actually not appear in document contents
options("OR").Add().v = "lazy"
Dim clbk As SearchCallback = New SearchCallback(frmMain.IoPdfEditor.Inst)
options("Callback").v = clbk
options("Flags").v = (CInt(PDFXEdit.PXV_SearchFlags.PXV_SearchFlag_GetTextQuads) Or CInt(PDFXEdit.PXV_SearchFlags.PXV_SearchFlag_IncludePageText))
docPageToSelect = -1
Op.Do()
End Sub
...
Public Class SearchCallback
Implements PDFXEdit.IPXV_SearchCallback
Private m_Inst As PDFXEdit.IPXV_Inst = Nothing
Public Sub New(Inst As PDFXEdit.IPXV_Inst)
m_Inst = Inst
End Sub
Public Sub OnStart() Implements IPXV_SearchCallback.OnStart
End Sub
Public Sub OnFinish(nResCode As Integer) Implements IPXV_SearchCallback.OnFinish
End Sub
Public Sub OnStartPtr(pPtr As IPXV_SearchPtr) Implements IPXV_SearchCallback.OnStartPtr
End Sub
Public Sub OnStopPtr(pPtr As IPXV_SearchPtr, bIncomplete As Boolean) Implements IPXV_SearchCallback.OnStopPtr
End Sub
Public Sub OnNewEntry(pEntry As IPXV_SearchEntry) Implements IPXV_SearchCallback.OnNewEntry
'Highlighting items in the document
Dim i As UInteger = 0
Dim doc As PDFXEdit.IPXC_Document = Nothing
Dim vDoc As PDFXEdit.IPXV_Document = Nothing
Dim page As PDFXEdit.IPXC_Page = Nothing
Dim highlighter As PDFXEdit.IPXV_DocHighlighter = Nothing
While i < pEntry.Count
Dim item As PDFXEdit.IPXV_SearchEntryItem = pEntry(i)
Dim ptr As PDFXEdit.IPXV_SearchPtr = item.Ptr
For j As UInteger = 0 To ptr.Count - 1
Dim chunk As PDFXEdit.PXV_SearchPtrChunk = ptr(j)
'First we get the document
If chunk.nType = CUInt(PDFXEdit.PXV_SearchPtrChunkType.PXV_SearchPtrChunk_Document) Then
doc = ptr.Doc(chunk.nValue)
vDoc = m_Inst.FindDocByCoreDoc(doc)
'Now we'll need to check whether the document is opened so that we can highlight it
If Not IsNothing(vDoc) Then
highlighter = vDoc.AddNewHighlighter(PDFXEdit.PXV_DocHighlightType.PXV_DocHighlight_Page)
End If
ElseIf chunk.nType = CUInt(PDFXEdit.PXV_SearchPtrChunkType.PXV_SearchPtrChunk_Page) Then
If Not IsNothing(doc) Then
page = doc.Pages(chunk.nValue)
End If
If Not IsNothing(page) Then
'Now we have the page where the text is found
For k As UInteger = 0 To item.TextRangesCount - 1
'Now we need to get the coordinates of the highlight boxes
Dim quads As PDFXEdit.IPXC_QuadsF = item.GetTextRangeQuads(k)
'Highlighting the search results
If Not IsNothing(highlighter) Then
Dim ha As PDFXEdit.PXV_DocHighlightAdvanced = New PDFXEdit.PXV_DocHighlightAdvanced()
highlighter.Add(page, quads, Nothing, Nothing, ha, 0)
'Setting the current page of the document to the first page with a search result
If docPageToSelect = -1 Then
docPageToSelect = page.Number
vDoc.ActiveView.PagesView.Layout.CurrentPage = page.Number
End If
End If
Next
End If
End If
Next
i += 1
End While
End Sub
End Class