-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.vb
More file actions
69 lines (60 loc) · 3.24 KB
/
Copy pathProgram.vb
File metadata and controls
69 lines (60 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Imports DevExpress.Drawing
Imports DevExpress.Pdf
Imports System.Diagnostics
Imports System.Drawing
Namespace HighlightSearchResults
Class Program
Shared Sub Main(ByVal args As String())
'Create a PDF document processor.
Using documentProcessor As New PdfDocumentProcessor()
'Define search words
Dim words As String() = {"Get", "DX-RX809", "HD", "DX-B5000"}
'Load a PDF document
documentProcessor.LoadDocument("..\..\..\Document.pdf")
'Specify the search parameters
Dim searchParameters As New PdfTextSearchParameters()
searchParameters.CaseSensitive = True
searchParameters.WholeWords = True
For Each word As String In words
Dim result As PdfTextSearchResults
'Get the search results from the FindText method call with search text and search parameters
Do While InlineAssignHelper(result, documentProcessor.FindText(word, searchParameters)).Status = PdfTextSearchStatus.Found
'HighlightResultWithGraphics(documentProcessor, result)
HighlightResultWithAnnotations(documentProcessor, result)
Loop
Next
'Save the document
documentProcessor.SaveDocument("..\..\..\Result.pdf")
Process.Start(New ProcessStartInfo("..\..\..\Result.pdf") With {.UseShellExecute = True})
End Using
End Sub
'This method uses PdfGraphics to highlight text
Private Shared Sub HighlightResultWithGraphics(ByVal processor As PdfDocumentProcessor, ByVal result As PdfTextSearchResults)
Using graphics = processor.CreateGraphicsPageSystem()
Using brush = New DXSolidBrush(Color.FromArgb(130, 55, 155, 255))
For Each rect In result.Rectangles
Dim fillRectangle As New RectangleF(CSng(rect.Left), CSng(rect.Top) - CSng(rect.Height), CSng(rect.Width), CSng(rect.Height))
graphics.FillRectangle(brush, fillRectangle)
Next
graphics.AddToPageForeground(result.Page)
End Using
End Using
End Sub
'This method uses annotations to highlight text
Private Shared Sub HighlightResultWithAnnotations(ByVal processor As PdfDocumentProcessor, ByVal result As PdfTextSearchResults)
Dim facade As PdfDocumentFacade = processor.DocumentFacade
Dim page As PdfPageFacade = facade.Pages(result.Page.GetPageIndex())
For i As Integer = 0 To result.Rectangles.Count - 1
Dim annotation As PdfTextMarkupAnnotationFacade = page.AddTextMarkupAnnotation(result.Rectangles(i), PdfTextMarkupAnnotationType.Highlight)
If annotation IsNot Nothing Then
annotation.Color = New PdfRGBColor(0.2, 0.6, 0)
End If
Next
End Sub
'Helper function to assign and return value in Do While loop
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
End Class
End Namespace