From f5de0b5044cf1494db46e57ccdf9ab13b91d1442 Mon Sep 17 00:00:00 2001 From: kellemNegasi Date: Sun, 22 Dec 2024 20:40:51 +0200 Subject: [PATCH] updated code to accept inputs from command line arguments --- go.mod | 4 +-- go.sum | 8 +++--- search-and-replace/replace_text.go | 40 +++++++++++++++++++++++++----- search-and-replace/search_text.go | 39 ++++++++++++++++++++++++----- 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 0b62c8d0..96aea4b9 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736 github.com/unidoc/pkcs7 v0.2.0 github.com/unidoc/unichart v0.3.0 - github.com/unidoc/unipdf/v3 v3.62.0 + github.com/unidoc/unipdf/v3 v3.65.0 golang.org/x/crypto v0.31.0 golang.org/x/image v0.18.0 golang.org/x/text v0.21.0 @@ -50,7 +50,7 @@ require ( github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a // indirect github.com/unidoc/unitype v0.4.0 // indirect go.opencensus.io v0.24.0 // indirect - golang.org/x/net v0.24.0 // indirect + golang.org/x/net v0.33.0 // indirect golang.org/x/oauth2 v0.7.0 // indirect golang.org/x/sys v0.28.0 // indirect golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect diff --git a/go.sum b/go.sum index d4f465c8..be365d03 100644 --- a/go.sum +++ b/go.sum @@ -128,8 +128,8 @@ github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a h1:RLtvUhe4DsUDl6 github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a/go.mod h1:j+qMWZVpZFTvDey3zxUkSgPJZEX33tDgU/QIA0IzCUw= github.com/unidoc/unichart v0.3.0 h1:VX1j5yzhjrR3f2flC03Yat6/WF3h7Z+DLEvJLoTGhoc= github.com/unidoc/unichart v0.3.0/go.mod h1:8JnLNKSOl8yQt1jXewNgYFHhFm5M6/ZiaydncFDpakA= -github.com/unidoc/unipdf/v3 v3.62.0 h1:CVsxq6k1SSIrprotlFvq6iBhA+5745dWaApB0LKtGcc= -github.com/unidoc/unipdf/v3 v3.62.0/go.mod h1:0OIzSHHno23Y8WzaK+852abK8d3AxUZ1GQkMqpyCzu8= +github.com/unidoc/unipdf/v3 v3.65.0 h1:ye3PP9JuUJnQd4BqRR4wJO/EN9EpHRGusMOruDjCS74= +github.com/unidoc/unipdf/v3 v3.65.0/go.mod h1:tTbloOTKtGGi6z5doJshesDpQYktePhR+7r+WGCkooU= github.com/unidoc/unitype v0.4.0 h1:/TMZ3wgwfWWX64mU5x2O9no9UmoBqYCB089LYYqHyQQ= github.com/unidoc/unitype v0.4.0/go.mod h1:HV5zuUeqMKA4QgYQq3KDlJY/P96XF90BQB+6czK6LVA= 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 golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= +golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.7.0 h1:qe6s0zUXlPX80/dITx3440hWZ7GwMwgDDyrSGTPJG/g= golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4= diff --git a/search-and-replace/replace_text.go b/search-and-replace/replace_text.go index d0145253..7f222c9a 100644 --- a/search-and-replace/replace_text.go +++ b/search-and-replace/replace_text.go @@ -1,8 +1,17 @@ +/* + * This example code shows how to do search and replace operation in PDF using unipdf + * + * Run as: go run replace_text.go + * + * example: go run replace_text.go "Australia" "America" "1,2" ./test-data/file1.pdf ./test-data/result2.pdf + */ package main import ( "fmt" "os" + "strconv" + "strings" "github.com/unidoc/unipdf/v3/common/license" "github.com/unidoc/unipdf/v3/extractor" @@ -19,12 +28,31 @@ func init() { } func main() { - pattern := "Australia" - pages := []int{1} - replacement := "America" - filePath := "./test-data/file1.pdf" + // Ensure enough arguments are provided + if len(os.Args) < 5 { + fmt.Println("Usage: go run replace_text.go ") + os.Exit(1) + } + + // Parse positional arguments + pattern := os.Args[1] + replacement := os.Args[2] + pagesArg := os.Args[3] + filePath := os.Args[4] + outputPath := os.Args[5] + + // Convert pages string to a slice of integers + pageStrings := strings.Split(pagesArg, ",") + pageList := []int{} + for _, pageStr := range pageStrings { + page, err := strconv.Atoi(pageStr) + if err != nil { + fmt.Printf("Invalid page number: %s\n", pageStr) + os.Exit(1) + } + pageList = append(pageList, page) + } - outputPath := "./test-data/result.pdf" reader, _, err := model.NewPdfReaderFromFile(filePath, nil) if err != nil { fmt.Printf("Failed to create PDF reader: %v", err) @@ -32,7 +60,7 @@ func main() { } editor := extractor.NewEditor(reader) - err = editor.Replace(pattern, replacement, pages) + err = editor.Replace(pattern, replacement, pageList) if err != nil { fmt.Printf("Failed to search pattern: %v\n", err) os.Exit(1) diff --git a/search-and-replace/search_text.go b/search-and-replace/search_text.go index 9850c046..3331c13f 100644 --- a/search-and-replace/search_text.go +++ b/search-and-replace/search_text.go @@ -1,8 +1,17 @@ +/* + * This example code shows how to do text searching on pdf using unipdf + * + * Run as: go run search_text.go + * + * Example: go run search_text.go "copyright law" "1,2" ./test-data/file1.pdf + */ + package main import ( "fmt" "os" + "strconv" "strings" "github.com/unidoc/unipdf/v3/common/license" @@ -20,10 +29,28 @@ func init() { } func main() { - // Input parameters - filePath := "./test-data/file1.pdf" // Path to the PDF file - pattern := "Australia" // Text pattern to search for - pages := []int{1} // Page numbers to search on + // Ensure enough arguments are provided + if len(os.Args) < 4 { + fmt.Println("Usage: go run main.go ") + os.Exit(1) + } + + // Parse positional arguments + pattern := os.Args[1] + pagesArg := os.Args[2] + filePath := os.Args[3] + + // Convert pages string to a slice of integers + pageStrings := strings.Split(pagesArg, ",") + pageList := []int{} + for _, pageStr := range pageStrings { + page, err := strconv.Atoi(pageStr) + if err != nil { + fmt.Printf("Invalid page number: %s\n", pageStr) + os.Exit(1) + } + pageList = append(pageList, page) + } // Create a new PDF reader reader, _, err := model.NewPdfReaderFromFile(filePath, nil) @@ -36,14 +63,14 @@ func main() { editor := extractor.NewEditor(reader) // Perform the search for the specified pattern on the given pages - matchesPerPage, err := editor.Search(pattern, pages) + matchesPerPage, err := editor.Search(pattern, pageList) if err != nil { fmt.Printf("Failed to search pattern: %v\n", err) os.Exit(1) } // Print formatted search results - printSearchResults(matchesPerPage, pages, pattern) + printSearchResults(matchesPerPage, pageList, pattern) } // printSearchResults formats and prints the search results.