|
| 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 | +} |
0 commit comments