Skip to content

Commit 002b73b

Browse files
committed
added example for form fields with text colors
1 parent 0573041 commit 002b73b

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

forms/filled_form_rich.pdf

58.5 KB
Binary file not shown.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Fill PDF form via json data where field names and values along side text colors for each field are provided.
3+
*
4+
* Run as: go run form-fill-custom-text-color.go.
5+
*/
6+
package main
7+
8+
import (
9+
"encoding/json"
10+
"fmt"
11+
"io/ioutil"
12+
"os"
13+
14+
"github.com/unidoc/unipdf/v3/annotator"
15+
"github.com/unidoc/unipdf/v3/common/license"
16+
"github.com/unidoc/unipdf/v3/creator"
17+
"github.com/unidoc/unipdf/v3/fjson"
18+
"github.com/unidoc/unipdf/v3/model"
19+
)
20+
21+
func init() {
22+
// Make sure to load your metered License API key prior to using the library.
23+
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
24+
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
25+
if err != nil {
26+
panic(err)
27+
}
28+
}
29+
30+
// FieldData represents the field data structure.
31+
type FieldData struct {
32+
Name string `json:"name"`
33+
TextColor string `json:"text_color"`
34+
}
35+
36+
func main() {
37+
inputPath := "sample_form.pdf"
38+
jsonDataPath := "sample_form_rich.json"
39+
outputPath := "filled_form_rich.pdf"
40+
41+
fdata, err := fjson.LoadFromJSONFile(jsonDataPath)
42+
if err != nil {
43+
fmt.Printf("failed to load json file. Error : %v\n", err)
44+
os.Exit(1)
45+
}
46+
47+
f, err := os.Open(inputPath)
48+
if err != nil {
49+
fmt.Printf("failed to open input file. Error : %v\n", err)
50+
os.Exit(1)
51+
}
52+
defer f.Close()
53+
54+
pdfReader, err := model.NewPdfReader(f)
55+
if err != nil {
56+
fmt.Printf("failed to read pdf file. Error : %v\n", err)
57+
os.Exit(1)
58+
}
59+
60+
fieldColor, err := loadFieldColors(jsonDataPath)
61+
if err != nil {
62+
fmt.Printf("failed to load field colors. Error : %v\n", err)
63+
os.Exit(1)
64+
}
65+
66+
fieldAppearance := annotator.FieldAppearance{OnlyIfMissing: true, RegenerateTextFields: true}
67+
68+
fieldAppearance.SetStyle(annotator.AppearanceStyle{
69+
AutoFontSizeFraction: 0.70,
70+
FillColor: model.NewPdfColorDeviceRGB(1, 1, 1),
71+
BorderColor: model.NewPdfColorDeviceRGB(0, 0, 0),
72+
BorderSize: 2.0,
73+
AllowMK: false,
74+
TextColor: model.NewPdfColorDeviceRGB(0.5, 0.8, 0.8), // Default text color.
75+
FieldColors: fieldColor, // This specifies the text color for each field.
76+
})
77+
78+
pdfReader.AcroForm.FillWithAppearance(fdata, fieldAppearance)
79+
pdfWriter, err := pdfReader.ToWriter(nil)
80+
if err != nil {
81+
fmt.Printf("failed to convert reader to writer pdf file. Error : %v\n", err)
82+
os.Exit(1)
83+
}
84+
85+
// Write to file.
86+
err = pdfWriter.WriteToFile(outputPath)
87+
if err != nil {
88+
fmt.Printf("failed to write to output file. Error : %v\n", err)
89+
os.Exit(1)
90+
}
91+
fmt.Printf("Success, output written to %s\n", outputPath)
92+
}
93+
94+
func loadFieldColors(jsonPath string) (map[string]model.PdfColor, error) {
95+
file, err := os.Open(jsonPath)
96+
if err != nil {
97+
return nil, err
98+
}
99+
defer file.Close()
100+
101+
byteValue, err := ioutil.ReadAll(file)
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
var fields []FieldData
107+
err = json.Unmarshal(byteValue, &fields)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
fieldColors := make(map[string]model.PdfColor)
113+
for _, field := range fields {
114+
if field.TextColor == "" {
115+
continue
116+
}
117+
tc := creator.ColorRGBFromHex(field.TextColor)
118+
r, g, b := tc.ToRGB()
119+
tcRGB := model.NewPdfColorDeviceRGB(r, g, b)
120+
fieldColors[field.Name] = tcRGB
121+
}
122+
123+
return fieldColors, nil
124+
}

forms/sample_form_rich.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[
2+
{
3+
"name": "formID",
4+
"value": "202286033286049"
5+
},
6+
{
7+
"name": "pdf_submission_new",
8+
"value": "1",
9+
"text_color": "#40E0D0"
10+
},
11+
{
12+
"name": "simple_spc",
13+
"value": "202286033286049-202286033286049",
14+
"text_color": "#6495ED"
15+
},
16+
{
17+
"name": "adobeWarning",
18+
"value": "In order to submit this form, you should open it with Adobe Acrobat Reader.",
19+
"text_color": "#008000"
20+
},
21+
{
22+
"name": "name3[first]",
23+
"value": "John",
24+
"text_color": "#0000FF",
25+
"fill_color": "#FFD700",
26+
"border_color": "#CD5C5C"
27+
},
28+
{
29+
"name": "name3[last]",
30+
"value": "Doe",
31+
"text_color": "#7dcea0"
32+
},
33+
{
34+
"name": "email4",
35+
"value": "alpha@omega.com",
36+
"text_color": "#CD5C5C"
37+
},
38+
{
39+
"name": "address5[addr_line1]",
40+
"value": "sample address line 1",
41+
"text_color": "#008000"
42+
},
43+
{
44+
"name": "address5[addr_line2]",
45+
"value": "sample address line 2",
46+
"text_color": "#40E0D0"
47+
},
48+
{
49+
"name": "address5[city]",
50+
"value": "sample address",
51+
"text_color": "#00FF00"
52+
},
53+
{
54+
"name": "address5[state]",
55+
"value": "sample state",
56+
"text_color": "#2596be"
57+
},
58+
{
59+
"name": "address5[postal]",
60+
"value": "3538148",
61+
"text_color": "#063970"
62+
},
63+
{
64+
"name": "fakeSubmitButton",
65+
"value": "Submit"
66+
},
67+
{
68+
"name": "submitButton",
69+
"value": ""
70+
}
71+
]

0 commit comments

Comments
 (0)