-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[US-234] Update form example to add a submit button (#239)
* update form example to add a submit button * add new pdf form with submit and reset button example * update readme file * update dependecies * revert some dependency version updates
- Loading branch information
Showing
4 changed files
with
223 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Create a new form with a submit and reset button. | ||
* The example shows how to add a submit button and a reset button to a form. | ||
* The submit button could be used to send the form data to a backend server, | ||
* and the reset button would reset the form fields value. | ||
* | ||
* Run as: go run pdf_form_action.go | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/unidoc/unipdf/v3/annotator" | ||
"github.com/unidoc/unipdf/v3/common/license" | ||
"github.com/unidoc/unipdf/v3/contentstream/draw" | ||
"github.com/unidoc/unipdf/v3/core" | ||
"github.com/unidoc/unipdf/v3/creator" | ||
"github.com/unidoc/unipdf/v3/model" | ||
) | ||
|
||
func init() { | ||
// Make sure to load your metered License API key prior to using the library. | ||
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io | ||
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func main() { | ||
textFieldsDef := []struct { | ||
Label string | ||
Name string | ||
Rect []float64 | ||
}{ | ||
{Label: "Full Name", Name: "full_name", Rect: []float64{123.97, 619.02, 343.99, 633.6}}, | ||
{Label: "Address 1", Name: "address_line_1", Rect: []float64{123.97, 596.82, 343.99, 611.4}}, | ||
{Label: "Address 2", Name: "address_line_2", Rect: []float64{123.97, 574.28, 343.99, 588.86}}, | ||
} | ||
|
||
c := creator.New() | ||
page := c.NewPage() | ||
_, pageHeight, err := page.Size() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
form := model.NewPdfAcroForm() | ||
fields := core.MakeArray() | ||
|
||
// Create text fields and it's label | ||
for _, fdef := range textFieldsDef { | ||
opt := annotator.TextFieldOptions{} | ||
textf, err := annotator.NewTextField(page, fdef.Name, fdef.Rect, opt) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
*form.Fields = append(*form.Fields, textf.PdfField) | ||
page.AddAnnotation(textf.Annotations[0].PdfAnnotation) | ||
|
||
y := pageHeight - fdef.Rect[1] | ||
|
||
p := c.NewParagraph(fdef.Label) | ||
p.SetPos(fdef.Rect[0]-80, y-10) | ||
err = c.Draw(p) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
line := c.NewLine(fdef.Rect[0], y, fdef.Rect[2], y) | ||
err = c.Draw(line) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fields.Append(textf.ToPdfObject()) | ||
} | ||
|
||
err = addSubmitButton(page, form) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = addResetButton(page, form, fields) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
c.SetForms(form) | ||
|
||
err = c.WriteToFile("form_with_action_button.pdf") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// Add Submit button that will submit all fields value. | ||
func addSubmitButton(page *model.PdfPage, form *model.PdfAcroForm) error { | ||
optSubmit := annotator.FormSubmitActionOptions{ | ||
Url: "https://unidoc.io", | ||
Rectangle: draw.Rectangle{ | ||
X: 400.0, | ||
Y: 400.0, | ||
Width: 50.0, | ||
Height: 20.0, | ||
FillColor: model.NewPdfColorDeviceRGB(0.0, 1.0, 0.0), | ||
}, | ||
Label: "Submit", | ||
LabelColor: model.NewPdfColorDeviceRGB(1.0, 0.0, 0.0), | ||
} | ||
|
||
btnSubmitField, err := annotator.NewFormSubmitButtonField(page, optSubmit) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*form.Fields = append(*form.Fields, btnSubmitField.PdfField) | ||
page.AddAnnotation(btnSubmitField.Annotations[0].PdfAnnotation) | ||
|
||
return nil | ||
} | ||
|
||
// Add Reset button that would reset the specified fields to it's default value. | ||
func addResetButton(page *model.PdfPage, form *model.PdfAcroForm, fields *core.PdfObjectArray) error { | ||
optReset := annotator.FormResetActionOptions{ | ||
Rectangle: draw.Rectangle{ | ||
X: 100.0, | ||
Y: 400.0, | ||
Width: 50.0, | ||
Height: 20.0, | ||
FillColor: model.NewPdfColorDeviceGray(0.5), | ||
}, | ||
Label: "Reset", | ||
LabelColor: model.NewPdfColorDeviceGray(1.0), | ||
Fields: fields, | ||
} | ||
|
||
btnResetField, err := annotator.NewFormResetButtonField(page, optReset) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Add widget to existing form. | ||
*form.Fields = append(*form.Fields, btnResetField.PdfField) | ||
page.AddAnnotation(btnResetField.Annotations[0].PdfAnnotation) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,63 @@ | ||
module github.com/unidoc/unidoc-examples | ||
|
||
go 1.17 | ||
|
||
require ( | ||
cloud.google.com/go/kms v1.4.0 | ||
github.com/ThalesIgnite/crypto11 v1.2.4 | ||
github.com/adrg/strutil v0.2.3 // indirect | ||
github.com/adrg/sysfont v0.1.2 // indirect | ||
github.com/adrg/xdg v0.3.3 // indirect | ||
github.com/aws/aws-sdk-go v1.44.130 | ||
github.com/bmatcuk/doublestar v1.3.4 | ||
github.com/boombuler/barcode v1.0.1 | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/miekg/pkcs11 v1.0.3 // indirect | ||
github.com/sirupsen/logrus v1.8.1 // indirect | ||
github.com/trimmer-io/go-xmp v1.0.0 | ||
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.50.0 | ||
github.com/unidoc/unipdf/v3 v3.54.0 | ||
github.com/wcharczuk/go-chart/v2 v2.1.0 | ||
go.opencensus.io v0.24.0 // indirect | ||
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa | ||
golang.org/x/image v0.10.0 | ||
golang.org/x/text v0.11.0 | ||
golang.org/x/crypto v0.17.0 | ||
golang.org/x/image v0.14.0 | ||
golang.org/x/text v0.14.0 | ||
google.golang.org/api v0.102.0 | ||
google.golang.org/genproto v0.0.0-20221024183307-1bc688fe9f3e | ||
google.golang.org/protobuf v1.28.1 | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/gographics/imagick.v2 v2.6.0 | ||
) | ||
|
||
go 1.15 | ||
require ( | ||
cloud.google.com/go v0.104.0 // indirect | ||
cloud.google.com/go/compute v1.12.1 // indirect | ||
cloud.google.com/go/compute/metadata v0.2.1 // indirect | ||
cloud.google.com/go/iam v0.3.0 // indirect | ||
github.com/adrg/strutil v0.2.3 // indirect | ||
github.com/adrg/sysfont v0.1.2 // indirect | ||
github.com/adrg/xdg v0.3.3 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect | ||
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/go-cmp v0.5.9 // indirect | ||
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect | ||
github.com/googleapis/gax-go/v2 v2.6.0 // indirect | ||
github.com/gorilla/i18n v0.0.0-20150820051429-8b358169da46 // indirect | ||
github.com/jmespath/go-jmespath v0.4.0 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/miekg/pkcs11 v1.0.3 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/sirupsen/logrus v1.8.1 // indirect | ||
github.com/stretchr/testify v1.8.4 // indirect | ||
github.com/thales-e-security/pool v0.0.2 // indirect | ||
github.com/unidoc/freetype v0.2.3 // indirect | ||
github.com/unidoc/garabic v0.0.0-20220702200334-8c7cb25baa11 // indirect | ||
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a // indirect | ||
github.com/unidoc/unitype v0.2.1 // indirect | ||
go.opencensus.io v0.24.0 // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect | ||
golang.org/x/sys v0.15.0 // indirect | ||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/grpc v1.50.1 // indirect | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters