Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

- name: Setup ImageMagick's MagickWand
run:
sudo apt-get install libmagickwand-dev imagemagick
sudo apt-get update && sudo apt-get install libmagickwand-dev imagemagick

- name: Build all
run: |
Expand Down
11 changes: 8 additions & 3 deletions accessibility/pdf_add_image_alt_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {

// Construct base K dictionary.
docK := model.NewKDictionary()
docK.S = core.MakeName(model.StructureTypeDocument)
docK.S = core.MakeName(string(model.StructureTypeDocument))
// Manually set optional ID for the K object
docK.ID = core.MakeString(kIds[0])
// Or generate the ID automatically using the following:
Expand All @@ -62,7 +62,7 @@ func main() {
pageMarkedContentSection := model.NewKDictionary()

// Set the structure type to Section.
pageMarkedContentSection.S = core.MakeName(model.StructureTypeSection)
pageMarkedContentSection.S = core.MakeName(string(model.StructureTypeSection))
pageMarkedContentSection.ID = core.MakeString(kIds[1])

// Add as a child
Expand Down Expand Up @@ -105,7 +105,12 @@ func addImage(c *creator.Creator, imageFile string, x, y float64, mcid int64, al
}

// Add the image to the marked content section.
altKdictEntry := img.SetMarkedContentID(mcid)
img.SetMarkedContentID(mcid)

altKdictEntry, err := img.GenerateKDict()
if err != nil {
fmt.Errorf("Error: %v", err)
}

// Set the alternate text.
altKdictEntry.Alt = core.MakeString(altText)
Expand Down
4 changes: 2 additions & 2 deletions accessibility/pdf_copy_page_with_accessibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {

// Construct base K dictionary.
docK := model.NewKDictionary()
docK.S = core.MakeName(model.StructureTypeDocument)
docK.S = core.MakeName(string(model.StructureTypeDocument))

newStr.AddKDict(docK)

Expand Down Expand Up @@ -115,7 +115,7 @@ func main() {

// Add section K object to store all original K objects from template page.
sectK := model.NewKDictionary()
sectK.S = core.MakeName(model.StructureTypeSection)
sectK.S = core.MakeName(string(model.StructureTypeSection))
sectK.T = core.MakeString(fmt.Sprintf("Page %d", n))
sectK.GenerateRandomID()

Expand Down
17 changes: 13 additions & 4 deletions accessibility/pdf_set_language_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {

// Construct base K dictionary.
docK := model.NewKDictionary()
docK.S = core.MakeName(model.StructureTypeDocument)
docK.S = core.MakeName(string(model.StructureTypeDocument))

str.AddKDict(docK)

Expand All @@ -57,7 +57,12 @@ func main() {
p.SetPos(100, 100)

// Set marked content identifier for the paragraph.
pMarkedContent := p.SetMarkedContentID(0)
p.SetMarkedContentID(0)

pMarkedContent, err := p.GenerateKDict()
if err != nil {
fmt.Errorf("Error: %v", err)
}

// Set the language identifier for the paragraph to British English.
pMarkedContent.Lang = core.MakeString("en-GB")
Expand All @@ -76,7 +81,11 @@ func main() {
// It's "Hello World" in Indonesian.
sp.SetText("Halo Dunia")
sp.SetPos(100, 200)
pMarkedContent = sp.SetMarkedContentID(1)
sp.SetMarkedContentID(1)
pMarkedContent, err = p.GenerateKDict()
if err != nil {
fmt.Errorf("Error: %v", err)
}

// Set the language identifier for the styled paragraph to Indonesian.
pMarkedContent.Lang = core.MakeString("id-ID")
Expand All @@ -88,7 +97,7 @@ func main() {

c.SetStructTreeRoot(str)

err := c.WriteToFile("pdf_set_language_identifier.pdf")
err = c.WriteToFile("pdf_set_language_identifier.pdf")
if err != nil {
fmt.Errorf("Error: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736
github.com/unidoc/pkcs7 v0.2.0
github.com/unidoc/unichart v0.4.0
github.com/unidoc/unipdf/v4 v4.1.0
github.com/unidoc/unipdf/v4 v4.2.0
golang.org/x/crypto v0.33.0
golang.org/x/image v0.24.0
golang.org/x/text v0.22.0
Expand Down Expand Up @@ -70,4 +70,5 @@ require (
google.golang.org/grpc v1.64.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
software.sslmate.com/src/go-pkcs12 v0.6.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ github.com/unidoc/unichart v0.4.0 h1:uXk9ZjbqzKb8Lt2Qv2oM9D2ftNRXvezPevgxQhsTQys
github.com/unidoc/unichart v0.4.0/go.mod h1:9QsE8RbS0fE7ndHNroeCEFkRPqqk47Qsoj6QSAtcwN0=
github.com/unidoc/unipdf/v4 v4.1.0 h1:qeEUEVm0rVPocIGZdKoSsRCUTrA3RD+VlBQX7NnP/kY=
github.com/unidoc/unipdf/v4 v4.1.0/go.mod h1:SbSYFUoutyBR+hLlsHyNiCzzcSVVuG10S5Xu8RIJ6EY=
github.com/unidoc/unipdf/v4 v4.2.0 h1:nMBQMOp8WDmm0L1fAdPKZ57pVNloxG+JHClXucwufLo=
github.com/unidoc/unipdf/v4 v4.2.0/go.mod h1:csApn6iwjybsHb43/6BSXAubkSumZ7et0zlcIpJ4iXI=
github.com/unidoc/unitype v0.5.1 h1:UwTX15K6bktwKocWVvLoijIeu4JAVEAIeFqMOjvxqQs=
github.com/unidoc/unitype v0.5.1/go.mod h1:3dxbRL+f1otNqFQIRHho8fxdg3CcUKrqS8w1SXTsqcI=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
Expand Down Expand Up @@ -238,3 +240,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
software.sslmate.com/src/go-pkcs12 v0.6.0 h1:f3sQittAeF+pao32Vb+mkli+ZyT+VwKaD014qFGq6oU=
software.sslmate.com/src/go-pkcs12 v0.6.0/go.mod h1:Qiz0EyvDRJjjxGyUQa2cCNZn/wMyzrRJ/qcDXOQazLI=
119 changes: 119 additions & 0 deletions signatures/pdf_sign_pkcs7_ecdsa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* This example showcases how to create an adbe.pkcs7.detached compatible digital signature for a PDF file
* using ECDSA (elliptic curve DSA) key. ECDSA keys can be used for signing PDF files starting from version 2.0.
*
* $ ./pdf_sign_pkcs7_ecdsa <FILE.PFX> <PASSWORD> <INPUT_PDF_PATH> <OUTPUT_PDF_PATH>
*/
package main

import (
"crypto/ecdsa"
"fmt"
"log"
"os"
"time"

"github.com/unidoc/unipdf/v4/annotator"
"github.com/unidoc/unipdf/v4/common/license"
"github.com/unidoc/unipdf/v4/core"
"github.com/unidoc/unipdf/v4/model"
"github.com/unidoc/unipdf/v4/model/sighandler"

"software.sslmate.com/src/go-pkcs12"
)

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)
}
}

const usagef = "Usage: %s PFX_FILE PASSWORD INPUT_PDF_PATH OUTPUT_PDF_PATH\n"

func main() {
args := os.Args
if len(args) < 5 {
fmt.Printf(usagef, os.Args[0])
return
}
pfxPath := args[1]
password := args[2]
inputPath := args[3]
outputPath := args[4]

// Get private key and X509 certificate from the ECDSA PFX file.
pfxData, err := os.ReadFile(pfxPath)
if err != nil {
log.Fatal("Fail: %v\n", err)
}

priv, cert, err := pkcs12.Decode(pfxData, password)
if err != nil {
log.Fatal("Fail: %v\n", err)
}

// Create reader.
file, err := os.Open(inputPath)
if err != nil {
log.Fatal("Fail: %v\n", err)
}
defer file.Close()

reader, err := model.NewPdfReader(file)
if err != nil {
log.Fatal("Fail: %v\n", err)
}

// Create appender.
appender, err := model.NewPdfAppender(reader)
if err != nil {
log.Fatal("Fail: %v\n", err)
}

// Create signature handler with ECDSA key.
handler, err := sighandler.NewAdobePKCS7DetachedEcdsa(priv.(*ecdsa.PrivateKey), cert)
if err != nil {
log.Fatal("Fail: %v\n", err)
}

// Create signature.
signature := model.NewPdfSignature(handler)
signature.SetName("adbe.pkcs7.detached ECDSA PDF")
signature.SetReason("Test ECDSA")
signature.SetDate(time.Now(), "")

if err := signature.Initialize(); err != nil {
log.Fatal("Fail: %v\n", err)
}

// Create signature field and appearance.
opts := annotator.NewSignatureFieldOpts()
opts.FontSize = 10
opts.Rect = []float64{10, 25, 75, 60}

field, err := annotator.NewSignatureField(
signature,
[]*annotator.SignatureLine{
annotator.NewSignatureLine("Name", "John Doe"),
annotator.NewSignatureLine("Date", "2025.07.28"),
annotator.NewSignatureLine("Reason", "Test"),
},
opts,
)
field.T = core.MakeString("Self signed PDF")

if err = appender.Sign(1, field); err != nil {
log.Fatal("Fail: %v\n", err)
}

// Write output PDF file.
err = appender.WriteToFile(outputPath)
if err != nil {
log.Fatal("Fail: %v\n", err)
}

log.Printf("PDF file successfully signed. Output path: %s\n", outputPath)
}