|
| 1 | +/* |
| 2 | + * This example showcases how to create an adbe.pkcs7.detached compatible digital signature for a PDF file |
| 3 | + * using ECDSA (elliptic curve DSA) key. ECDSA keys can be used for signing PDF files starting from version 2.0. |
| 4 | + * |
| 5 | + * $ ./pdf_sign_pkcs7_ecdsa <FILE.PFX> <PASSWORD> <INPUT_PDF_PATH> <OUTPUT_PDF_PATH> |
| 6 | + */ |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "crypto/ecdsa" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "os" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/unidoc/unipdf/v4/annotator" |
| 17 | + "github.com/unidoc/unipdf/v4/common/license" |
| 18 | + "github.com/unidoc/unipdf/v4/core" |
| 19 | + "github.com/unidoc/unipdf/v4/model" |
| 20 | + "github.com/unidoc/unipdf/v4/model/sighandler" |
| 21 | + |
| 22 | + "software.sslmate.com/src/go-pkcs12" |
| 23 | +) |
| 24 | + |
| 25 | +func init() { |
| 26 | + // Make sure to load your metered License API key prior to using the library. |
| 27 | + // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io |
| 28 | + err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) |
| 29 | + if err != nil { |
| 30 | + panic(err) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const usagef = "Usage: %s PFX_FILE PASSWORD INPUT_PDF_PATH OUTPUT_PDF_PATH\n" |
| 35 | + |
| 36 | +func main() { |
| 37 | + args := os.Args |
| 38 | + if len(args) < 5 { |
| 39 | + fmt.Printf(usagef, os.Args[0]) |
| 40 | + return |
| 41 | + } |
| 42 | + pfxPath := args[1] |
| 43 | + password := args[2] |
| 44 | + inputPath := args[3] |
| 45 | + outputPath := args[4] |
| 46 | + |
| 47 | + // Get private key and X509 certificate from the ECDSA PFX file. |
| 48 | + pfxData, err := os.ReadFile(pfxPath) |
| 49 | + if err != nil { |
| 50 | + log.Fatal("Fail: %v\n", err) |
| 51 | + } |
| 52 | + |
| 53 | + priv, cert, err := pkcs12.Decode(pfxData, password) |
| 54 | + if err != nil { |
| 55 | + log.Fatal("Fail: %v\n", err) |
| 56 | + } |
| 57 | + |
| 58 | + // Create reader. |
| 59 | + file, err := os.Open(inputPath) |
| 60 | + if err != nil { |
| 61 | + log.Fatal("Fail: %v\n", err) |
| 62 | + } |
| 63 | + defer file.Close() |
| 64 | + |
| 65 | + reader, err := model.NewPdfReader(file) |
| 66 | + if err != nil { |
| 67 | + log.Fatal("Fail: %v\n", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Create appender. |
| 71 | + appender, err := model.NewPdfAppender(reader) |
| 72 | + if err != nil { |
| 73 | + log.Fatal("Fail: %v\n", err) |
| 74 | + } |
| 75 | + |
| 76 | + // Create signature handler with ECDSA key. |
| 77 | + handler, err := sighandler.NewAdobePKCS7DetachedEcdsa(priv.(*ecdsa.PrivateKey), cert) |
| 78 | + if err != nil { |
| 79 | + log.Fatal("Fail: %v\n", err) |
| 80 | + } |
| 81 | + |
| 82 | + // Create signature. |
| 83 | + signature := model.NewPdfSignature(handler) |
| 84 | + signature.SetName("adbe.pkcs7.detached ECDSA PDF") |
| 85 | + signature.SetReason("Test ECDSA") |
| 86 | + signature.SetDate(time.Now(), "") |
| 87 | + |
| 88 | + if err := signature.Initialize(); err != nil { |
| 89 | + log.Fatal("Fail: %v\n", err) |
| 90 | + } |
| 91 | + |
| 92 | + // Create signature field and appearance. |
| 93 | + opts := annotator.NewSignatureFieldOpts() |
| 94 | + opts.FontSize = 10 |
| 95 | + opts.Rect = []float64{10, 25, 75, 60} |
| 96 | + |
| 97 | + field, err := annotator.NewSignatureField( |
| 98 | + signature, |
| 99 | + []*annotator.SignatureLine{ |
| 100 | + annotator.NewSignatureLine("Name", "John Doe"), |
| 101 | + annotator.NewSignatureLine("Date", "2025.07.28"), |
| 102 | + annotator.NewSignatureLine("Reason", "Test"), |
| 103 | + }, |
| 104 | + opts, |
| 105 | + ) |
| 106 | + field.T = core.MakeString("Self signed PDF") |
| 107 | + |
| 108 | + if err = appender.Sign(1, field); err != nil { |
| 109 | + log.Fatal("Fail: %v\n", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Write output PDF file. |
| 113 | + err = appender.WriteToFile(outputPath) |
| 114 | + if err != nil { |
| 115 | + log.Fatal("Fail: %v\n", err) |
| 116 | + } |
| 117 | + |
| 118 | + log.Printf("PDF file successfully signed. Output path: %s\n", outputPath) |
| 119 | +} |
0 commit comments