|
| 1 | +/* |
| 2 | + * This example code shows how to do text searching on pdf using unipdf |
| 3 | + * |
| 4 | + * Run as: go run search_text.go <pattern> <pages> <input> |
| 5 | + * |
| 6 | + * Example: go run search_text.go "copyright law" "1,2" ./test-data/file1.pdf |
| 7 | + */ |
| 8 | + |
| 9 | +package main |
| 10 | + |
| 11 | +import ( |
| 12 | + "fmt" |
| 13 | + "os" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/unidoc/unipdf/v3/common/license" |
| 18 | + "github.com/unidoc/unipdf/v3/extractor" |
| 19 | + "github.com/unidoc/unipdf/v3/model" |
| 20 | +) |
| 21 | + |
| 22 | +func init() { |
| 23 | + // Make sure to load your metered License API key prior to using the library. |
| 24 | + // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io |
| 25 | + err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) |
| 26 | + if err != nil { |
| 27 | + panic(err) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func main() { |
| 32 | + // Ensure enough arguments are provided |
| 33 | + if len(os.Args) < 4 { |
| 34 | + fmt.Println("Usage: go run main.go <pattern> <pages> <input>") |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | + |
| 38 | + // Parse positional arguments |
| 39 | + pattern := os.Args[1] |
| 40 | + pagesArg := os.Args[2] |
| 41 | + filePath := os.Args[3] |
| 42 | + |
| 43 | + // Convert pages string to a slice of integers |
| 44 | + pageStrings := strings.Split(pagesArg, ",") |
| 45 | + pageList := []int{} |
| 46 | + for _, pageStr := range pageStrings { |
| 47 | + page, err := strconv.Atoi(pageStr) |
| 48 | + if err != nil { |
| 49 | + fmt.Printf("Invalid page number: %s\n", pageStr) |
| 50 | + os.Exit(1) |
| 51 | + } |
| 52 | + pageList = append(pageList, page) |
| 53 | + } |
| 54 | + |
| 55 | + // Create a new PDF reader |
| 56 | + reader, _, err := model.NewPdfReaderFromFile(filePath, nil) |
| 57 | + if err != nil { |
| 58 | + fmt.Printf("Failed to create PDF reader: %v\n", err) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + |
| 62 | + // Create an Editor object for searching |
| 63 | + editor := extractor.NewEditor(reader) |
| 64 | + |
| 65 | + // Perform the search for the specified pattern on the given pages |
| 66 | + matchesPerPage, err := editor.Search(pattern, pageList) |
| 67 | + if err != nil { |
| 68 | + fmt.Printf("Failed to search pattern: %v\n", err) |
| 69 | + os.Exit(1) |
| 70 | + } |
| 71 | + |
| 72 | + // Print formatted search results |
| 73 | + printSearchResults(matchesPerPage, pageList, pattern) |
| 74 | +} |
| 75 | + |
| 76 | +// printSearchResults formats and prints the search results. |
| 77 | +// It displays indexes as [beg:end] and locations as {Llx Lly Urx Ury}. |
| 78 | +// If no matches are found for a page, it prints a not found message. |
| 79 | +func printSearchResults(matchesPerPage map[int]extractor.Match, pages []int, pattern string) { |
| 80 | + foundAny := false // Flag to check if any match is found across all pages |
| 81 | + |
| 82 | + for _, page := range pages { |
| 83 | + result, exists := matchesPerPage[page] |
| 84 | + if exists && len(result.Indexes) > 0 { |
| 85 | + foundAny = true |
| 86 | + fmt.Printf("Page %d:\n", page) |
| 87 | + |
| 88 | + // Prepare index strings |
| 89 | + var indexStrings []string |
| 90 | + for _, idx := range result.Indexes { |
| 91 | + indexStrings = append(indexStrings, fmt.Sprintf("[%d:%d]", idx[0], idx[1])) |
| 92 | + } |
| 93 | + fmt.Printf("indexes: %s\n", strings.Join(indexStrings, ", ")) |
| 94 | + |
| 95 | + // Prepare location strings |
| 96 | + var locationStrings []string |
| 97 | + for _, box := range result.Locations { |
| 98 | + locationStrings = append(locationStrings, fmt.Sprintf("{%.2f %.2f %.2f %.2f}", box.BBox.Llx, box.BBox.Lly, box.BBox.Urx, box.BBox.Ury)) |
| 99 | + } |
| 100 | + fmt.Printf("locations: %s\n\n", strings.Join(locationStrings, ", ")) |
| 101 | + } else { |
| 102 | + // If no matches found for the current page |
| 103 | + fmt.Printf("Page %d:\n", page) |
| 104 | + fmt.Println("pattern didn't match any text\n") |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if !foundAny { |
| 109 | + // If no matches found in any of the pages |
| 110 | + fmt.Println("pattern didn't match any text in the specified pages.") |
| 111 | + } |
| 112 | +} |
0 commit comments