Skip to content

Commit 7dd0bf9

Browse files
committed
added examples for search and replace usages
1 parent cd8a28a commit 7dd0bf9

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

search-and-replace/replace_text.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/unidoc/unipdf/v3/common/license"
8+
"github.com/unidoc/unipdf/v3/extractor"
9+
"github.com/unidoc/unipdf/v3/model"
10+
)
11+
12+
func init() {
13+
// Make sure to load your metered License API key prior to using the library.
14+
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
15+
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
16+
if err != nil {
17+
panic(err)
18+
}
19+
}
20+
21+
func main() {
22+
pattern := "Australia"
23+
pages := []int{1}
24+
replacement := "America"
25+
filePath := "./test-data/file1.pdf"
26+
27+
outputPath := "./test-data/result.pdf"
28+
reader, _, err := model.NewPdfReaderFromFile(filePath, nil)
29+
if err != nil {
30+
fmt.Printf("Failed to create PDF reader: %v", err)
31+
os.Exit(1)
32+
}
33+
editor := extractor.NewEditor(reader)
34+
35+
err = editor.Replace(pattern, replacement, pages)
36+
if err != nil {
37+
fmt.Printf("Failed to search pattern: %v\n", err)
38+
os.Exit(1)
39+
}
40+
41+
err = editor.WriteToFile(outputPath)
42+
if err != nil {
43+
fmt.Printf("Failed to write to file: %v", err)
44+
os.Exit(1)
45+
}
46+
47+
fmt.Println("Finished replacing %s by %s and saved the output file at %s", pattern, replacement, filePath)
48+
}

search-and-replace/search_text.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/unidoc/unipdf/v3/extractor"
9+
"github.com/unidoc/unipdf/v3/model"
10+
)
11+
12+
func main() {
13+
// Input parameters
14+
filePath := "./test-data/file1.pdf" // Path to the PDF file
15+
pattern := "Australia" // Text pattern to search for
16+
pages := []int{1} // Page numbers to search on
17+
18+
// Create a new PDF reader
19+
reader, _, err := model.NewPdfReaderFromFile(filePath, nil)
20+
if err != nil {
21+
fmt.Printf("Failed to create PDF reader: %v\n", err)
22+
os.Exit(1)
23+
}
24+
25+
// Create an Editor object for searching
26+
editor := extractor.NewEditor(reader)
27+
28+
// Perform the search for the specified pattern on the given pages
29+
matchesPerPage, err := editor.Search(pattern, pages)
30+
if err != nil {
31+
fmt.Printf("Failed to search pattern: %v\n", err)
32+
os.Exit(1)
33+
}
34+
35+
// Print formatted search results
36+
printSearchResults(matchesPerPage, pages, pattern)
37+
}
38+
39+
// printSearchResults formats and prints the search results.
40+
// It displays indexes as [beg:end] and locations as {Llx Lly Urx Ury}.
41+
// If no matches are found for a page, it prints a not found message.
42+
func printSearchResults(matchesPerPage map[int]extractor.Match, pages []int, pattern string) {
43+
foundAny := false // Flag to check if any match is found across all pages
44+
45+
for _, page := range pages {
46+
result, exists := matchesPerPage[page]
47+
if exists && len(result.Indexes) > 0 {
48+
foundAny = true
49+
fmt.Printf("Page %d:\n", page)
50+
51+
// Prepare index strings
52+
var indexStrings []string
53+
for _, idx := range result.Indexes {
54+
indexStrings = append(indexStrings, fmt.Sprintf("[%d:%d]", idx[0], idx[1]))
55+
}
56+
fmt.Printf("indexes: %s\n", strings.Join(indexStrings, ", "))
57+
58+
// Prepare location strings
59+
var locationStrings []string
60+
for _, box := range result.Locations {
61+
locationStrings = append(locationStrings, fmt.Sprintf("{%.2f %.2f %.2f %.2f}", box.BBox.Llx, box.BBox.Lly, box.BBox.Urx, box.BBox.Ury))
62+
}
63+
fmt.Printf("locations: %s\n\n", strings.Join(locationStrings, ", "))
64+
} else {
65+
// If no matches found for the current page
66+
fmt.Printf("Page %d:\n", page)
67+
fmt.Println("pattern didn't match any text\n")
68+
}
69+
}
70+
71+
if !foundAny {
72+
// If no matches found in any of the pages
73+
fmt.Println("pattern didn't match any text in the specified pages.")
74+
}
75+
}
198 KB
Binary file not shown.

0 commit comments

Comments
 (0)