-
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-557] Create example code for form fields with text colors (#256)
* added example for form fields with text colors * update unipdf version to 3.61 * fix go.sum * updated README.md and producer version in pdf_form_with_text_color.go code.
- Loading branch information
1 parent
227d174
commit 08cf488
Showing
8 changed files
with
337 additions
and
9 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
Binary file not shown.
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,121 @@ | ||
/* | ||
* Fill PDF form JSON input data and flatten it with a customized appearance to output PDF. | ||
* | ||
* Run as: go run pdf_fill_and_flatten_with_apearance.go. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/unidoc/unipdf/v3/annotator" | ||
"github.com/unidoc/unipdf/v3/common/license" | ||
"github.com/unidoc/unipdf/v3/fjson" | ||
"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() { | ||
inputPath := "./sample_form.pdf" | ||
jsonDataPath := "./sample_form.json" | ||
outputPath := "./sample_form_output.pdf" | ||
|
||
// Output path not specified: Export list of fields and data as JSON format. | ||
if len(outputPath) == 0 { | ||
fdata, err := fjson.LoadFromPDFFile(inputPath) | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
if fdata == nil { | ||
fmt.Printf("No data\n") | ||
return | ||
} | ||
fjson, err := fdata.JSON() | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf("%s\n", fjson) | ||
return | ||
} | ||
|
||
err := fillFieldsWithAppearance(inputPath, jsonDataPath, outputPath) | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Printf("Success, output written to %s\n", outputPath) | ||
} | ||
|
||
// fillFieldsWithAppearance loads field data from `jsonPath` and to fills the data to form fields in a PDF data provided via `inputPath` and outputs | ||
// as a flattened PDF in `outputPath`. Customized field appearance can be given either during filling or during flattening via the | ||
// `annotator.AppearanceStyle` object. | ||
func fillFieldsWithAppearance(inputPath, jsonPath, outputPath string) error { | ||
fdata, err := fjson.LoadFromJSONFile(jsonPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.Open(inputPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
pdfReader, err := model.NewPdfReader(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fieldAppearance := annotator.FieldAppearance{OnlyIfMissing: true, RegenerateTextFields: true} | ||
|
||
// specify a full set of appearance styles | ||
fieldAppearance.SetStyle(annotator.AppearanceStyle{ | ||
AutoFontSizeFraction: 0.70, | ||
FillColor: model.NewPdfColorDeviceRGB(1, 1, 1), | ||
BorderColor: model.NewPdfColorDeviceRGB(0, 0, 0), | ||
BorderSize: 2.0, | ||
AllowMK: false, | ||
TextColor: model.NewPdfColorDeviceRGB(0.5, 0.8, 0.8), | ||
}) | ||
|
||
// Populate the form data. | ||
// pdfReader.AcroForm.FillWithAppearance(fdata, fieldAppearance) // uncomment this line to fill with appearance | ||
pdfReader.AcroForm.Fill(fdata) | ||
|
||
// Flatten form. with field appearance | ||
err = pdfReader.FlattenFields(true, fieldAppearance) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Generate a PdfWriter instance from existing PdfReader. | ||
pdfWriter, err := pdfReader.ToWriter(nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write to file. | ||
err = pdfWriter.WriteToFile(outputPath) | ||
return err | ||
} | ||
|
||
func getFont(path string) (*model.PdfFont, error) { | ||
font, err := model.NewCompositePdfFontFromTTFFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return font, 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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Create a new form with customized text field colors. | ||
* The example shows how to create a form that has customized text field colors. | ||
* The form has submit and reset buttons. | ||
* Run as: go pdf_form_with_text_color.go | ||
*/ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"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 | ||
SampleInput string | ||
Rect []float64 | ||
}{ | ||
{Label: "Full Name", Name: "full_name", SampleInput: "Enter Full Name", Rect: []float64{123.97, 619.02, 343.99, 633.6}}, | ||
{Label: "Address 1", Name: "address_line_1", SampleInput: "Enter Address 1", Rect: []float64{123.97, 596.82, 343.99, 611.4}}, | ||
{Label: "Address 2", Name: "address_line_2", SampleInput: "Enter Address 2", Rect: []float64{123.97, 574.28, 343.99, 588.86}}, | ||
} | ||
|
||
c := creator.New() | ||
page := c.NewPage() | ||
|
||
_, pageHeight, err := page.Size() | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
form := model.NewPdfAcroForm() | ||
fields := core.MakeArray() | ||
|
||
for _, fdef := range textFieldsDef { | ||
opt := annotator.TextFieldOptions{ | ||
TextColor: "#0000FF", // Set text color to Blue. | ||
Value: fdef.SampleInput, | ||
} | ||
textf, err := annotator.NewTextField(page, fdef.Name, fdef.Rect, opt) | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
*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 { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
line := c.NewLine(fdef.Rect[0], y, fdef.Rect[2], y) | ||
err = c.Draw(line) | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
fields.Append(textf.ToPdfObject()) | ||
} | ||
|
||
// Add Submit button | ||
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 { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
*form.Fields = append(*form.Fields, btnSubmitField.PdfField) | ||
page.AddAnnotation(btnSubmitField.Annotations[0].PdfAnnotation) | ||
|
||
// Add Reset button | ||
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 { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Add widget to existing form. | ||
*form.Fields = append(*form.Fields, btnResetField.PdfField) | ||
page.AddAnnotation(btnResetField.Annotations[0].PdfAnnotation) | ||
|
||
c.SetForms(form) | ||
|
||
model.SetPdfProducer(`UniDoc v3.61.0 (Unlicensed) - http://unidoc.io`) | ||
defer model.SetPdfProducer("") | ||
|
||
outPath := filepath.Join(".", "form_field_with_colored_text_fields.pdf") | ||
err = c.WriteToFile(outPath) | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
} |
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,58 @@ | ||
[ | ||
{ | ||
"name": "formID", | ||
"value": "202286033286049" | ||
}, | ||
{ | ||
"name": "pdf_submission_new", | ||
"value": "1" | ||
}, | ||
{ | ||
"name": "simple_spc", | ||
"value": "202286033286049-202286033286049" | ||
}, | ||
{ | ||
"name": "adobeWarning", | ||
"value": "In order to submit this form, you should open it with Adobe Acrobat Reader." | ||
}, | ||
{ | ||
"name": "name3[first]", | ||
"value": "John" | ||
}, | ||
{ | ||
"name": "name3[last]", | ||
"value": "Doe" | ||
}, | ||
{ | ||
"name": "email4", | ||
"value": "alpha@omega.com" | ||
}, | ||
{ | ||
"name": "address5[addr_line1]", | ||
"value": "sample address line 1" | ||
}, | ||
{ | ||
"name": "address5[addr_line2]", | ||
"value": "sample address line 2" | ||
}, | ||
{ | ||
"name": "address5[city]", | ||
"value": "sample address" | ||
}, | ||
{ | ||
"name": "address5[state]", | ||
"value": "sample state" | ||
}, | ||
{ | ||
"name": "address5[postal]", | ||
"value": "3538148" | ||
}, | ||
{ | ||
"name": "fakeSubmitButton", | ||
"value": "Submit" | ||
}, | ||
{ | ||
"name": "submitButton", | ||
"value": "" | ||
} | ||
] |
Binary file not shown.
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