Search for text in a PDF on Server/Desktop

To search for text in a PDF using regular expression and then apply a link annotation on the highlighted result.

In this example, we add a link annotation but any other types of annotations can be applied here such as redaction annotations in the case of a search and redact workflow.

1doc := NewPDFDoc(filename)
2txtSearch := NewTextSearch()
3mode := TextSearchE_whole_word | TextSearchE_page_stop
4pattern := ""
5
6mode = mode | TextSearchE_reg_expression | TextSearchE_highlight
7txtSearch.SetMode(uint(mode))
8pattern := "\\d{4}-\\d{4}-\\d{4}-\\d{4}" //or "(\\d{4}-){3}\\d{4}"
9txtSearch.SetPattern(pattern)
10
11// call Begin() method to initialize the text search.
12txtSearch.Begin(doc, pattern, uint(mode))
13searchResult := txtSearch.Run()
14
15if searchResult.IsFound(){
16 // add a link annotation based on the location of the found instance
17 hlts := searchResult.GetHighlights()
18 hlts.Begin(doc)
19
20 for hlts.HasNext(){
21 curPage := doc.GetPage(uint(hlts.GetCurrentPageNumber()))
22 quadsInfo := hlts.GetCurrentQuads()
23
24 i := 0
25 for i < int(quadsInfo.Size()){
26 q := quadsInfo.Get(i)
27 // assume each quad is an axis-aligned rectangle
28 x1 := Min(Min(Min(q.GetP1().GetX(), q.GetP2().GetX()), q.GetP3().GetX()), q.GetP4().GetX())
29 x2 := Max(Max(Max(q.GetP1().GetX(), q.GetP2().GetX()), q.GetP3().GetX()), q.GetP4().GetX())
30 y1 := Min(Min(Min(q.GetP1().GetY(), q.GetP2().GetY()), q.GetP3().GetY()), q.GetP4().GetY())
31 y2 := Max(Max(Max(q.GetP1().GetY(), q.GetP2().GetY()), q.GetP3().GetY()), q.GetP4().GetY())
32 hyperLink := LinkCreate(doc.GetSDFDoc(), NewRect(x1, y1, x2, y2), ActionCreateURI(doc.GetSDFDoc(), "http://www.apryse.com"))
33 curPage.AnnotPushBack(hyperLink)
34 i = i + 1
35 }
36 hlts.Next()
37 }
38}

Search PDF files for text - Full Sample
Full code sample which shows how to use TextSearch to search text on PDF pages using regular expressions.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales