Skip to content

Commit f5de0b5

Browse files
committed
updated code to accept inputs from command line arguments
1 parent 3873782 commit f5de0b5

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736
1414
github.com/unidoc/pkcs7 v0.2.0
1515
github.com/unidoc/unichart v0.3.0
16-
github.com/unidoc/unipdf/v3 v3.62.0
16+
github.com/unidoc/unipdf/v3 v3.65.0
1717
golang.org/x/crypto v0.31.0
1818
golang.org/x/image v0.18.0
1919
golang.org/x/text v0.21.0
@@ -50,7 +50,7 @@ require (
5050
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a // indirect
5151
github.com/unidoc/unitype v0.4.0 // indirect
5252
go.opencensus.io v0.24.0 // indirect
53-
golang.org/x/net v0.24.0 // indirect
53+
golang.org/x/net v0.33.0 // indirect
5454
golang.org/x/oauth2 v0.7.0 // indirect
5555
golang.org/x/sys v0.28.0 // indirect
5656
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a h1:RLtvUhe4DsUDl6
128128
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a/go.mod h1:j+qMWZVpZFTvDey3zxUkSgPJZEX33tDgU/QIA0IzCUw=
129129
github.com/unidoc/unichart v0.3.0 h1:VX1j5yzhjrR3f2flC03Yat6/WF3h7Z+DLEvJLoTGhoc=
130130
github.com/unidoc/unichart v0.3.0/go.mod h1:8JnLNKSOl8yQt1jXewNgYFHhFm5M6/ZiaydncFDpakA=
131-
github.com/unidoc/unipdf/v3 v3.62.0 h1:CVsxq6k1SSIrprotlFvq6iBhA+5745dWaApB0LKtGcc=
132-
github.com/unidoc/unipdf/v3 v3.62.0/go.mod h1:0OIzSHHno23Y8WzaK+852abK8d3AxUZ1GQkMqpyCzu8=
131+
github.com/unidoc/unipdf/v3 v3.65.0 h1:ye3PP9JuUJnQd4BqRR4wJO/EN9EpHRGusMOruDjCS74=
132+
github.com/unidoc/unipdf/v3 v3.65.0/go.mod h1:tTbloOTKtGGi6z5doJshesDpQYktePhR+7r+WGCkooU=
133133
github.com/unidoc/unitype v0.4.0 h1:/TMZ3wgwfWWX64mU5x2O9no9UmoBqYCB089LYYqHyQQ=
134134
github.com/unidoc/unitype v0.4.0/go.mod h1:HV5zuUeqMKA4QgYQq3KDlJY/P96XF90BQB+6czK6LVA=
135135
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
@@ -153,8 +153,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
153153
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
154154
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
155155
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
156-
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
157-
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
156+
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
157+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
158158
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
159159
golang.org/x/oauth2 v0.7.0 h1:qe6s0zUXlPX80/dITx3440hWZ7GwMwgDDyrSGTPJG/g=
160160
golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4=

search-and-replace/replace_text.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
/*
2+
* This example code shows how to do search and replace operation in PDF using unipdf
3+
*
4+
* Run as: go run replace_text.go <pattern> <replacement> <pages> <input> <output>
5+
*
6+
* example: go run replace_text.go "Australia" "America" "1,2" ./test-data/file1.pdf ./test-data/result2.pdf
7+
*/
18
package main
29

310
import (
411
"fmt"
512
"os"
13+
"strconv"
14+
"strings"
615

716
"github.com/unidoc/unipdf/v3/common/license"
817
"github.com/unidoc/unipdf/v3/extractor"
@@ -19,20 +28,39 @@ func init() {
1928
}
2029

2130
func main() {
22-
pattern := "Australia"
23-
pages := []int{1}
24-
replacement := "America"
25-
filePath := "./test-data/file1.pdf"
31+
// Ensure enough arguments are provided
32+
if len(os.Args) < 5 {
33+
fmt.Println("Usage: go run replace_text.go <pattern> <replacement> <pages> <input> <output>")
34+
os.Exit(1)
35+
}
36+
37+
// Parse positional arguments
38+
pattern := os.Args[1]
39+
replacement := os.Args[2]
40+
pagesArg := os.Args[3]
41+
filePath := os.Args[4]
42+
outputPath := os.Args[5]
43+
44+
// Convert pages string to a slice of integers
45+
pageStrings := strings.Split(pagesArg, ",")
46+
pageList := []int{}
47+
for _, pageStr := range pageStrings {
48+
page, err := strconv.Atoi(pageStr)
49+
if err != nil {
50+
fmt.Printf("Invalid page number: %s\n", pageStr)
51+
os.Exit(1)
52+
}
53+
pageList = append(pageList, page)
54+
}
2655

27-
outputPath := "./test-data/result.pdf"
2856
reader, _, err := model.NewPdfReaderFromFile(filePath, nil)
2957
if err != nil {
3058
fmt.Printf("Failed to create PDF reader: %v", err)
3159
os.Exit(1)
3260
}
3361
editor := extractor.NewEditor(reader)
3462

35-
err = editor.Replace(pattern, replacement, pages)
63+
err = editor.Replace(pattern, replacement, pageList)
3664
if err != nil {
3765
fmt.Printf("Failed to search pattern: %v\n", err)
3866
os.Exit(1)

search-and-replace/search_text.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
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+
19
package main
210

311
import (
412
"fmt"
513
"os"
14+
"strconv"
615
"strings"
716

817
"github.com/unidoc/unipdf/v3/common/license"
@@ -20,10 +29,28 @@ func init() {
2029
}
2130

2231
func main() {
23-
// Input parameters
24-
filePath := "./test-data/file1.pdf" // Path to the PDF file
25-
pattern := "Australia" // Text pattern to search for
26-
pages := []int{1} // Page numbers to search on
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+
}
2754

2855
// Create a new PDF reader
2956
reader, _, err := model.NewPdfReaderFromFile(filePath, nil)
@@ -36,14 +63,14 @@ func main() {
3663
editor := extractor.NewEditor(reader)
3764

3865
// Perform the search for the specified pattern on the given pages
39-
matchesPerPage, err := editor.Search(pattern, pages)
66+
matchesPerPage, err := editor.Search(pattern, pageList)
4067
if err != nil {
4168
fmt.Printf("Failed to search pattern: %v\n", err)
4269
os.Exit(1)
4370
}
4471

4572
// Print formatted search results
46-
printSearchResults(matchesPerPage, pages, pattern)
73+
printSearchResults(matchesPerPage, pageList, pattern)
4774
}
4875

4976
// printSearchResults formats and prints the search results.

0 commit comments

Comments
 (0)