-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_pdf_merge.go
executable file
·57 lines (48 loc) · 1.38 KB
/
model_pdf_merge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"io"
"github.com/unidoc/unipdf/v3/annotator"
"github.com/unidoc/unipdf/v3/fdf"
"github.com/unidoc/unipdf/v3/model"
)
func fdfMerge(pdfInput io.ReadSeeker, fdfInput io.ReadSeeker, pdfOutput io.WriteSeeker) error {
fdfData, err := fdf.Load(fdfInput)
if err != nil {
return fmt.Errorf("Loading FDF: %s", err)
}
pdfReader, err := model.NewPdfReader(pdfInput)
if err != nil {
return fmt.Errorf("Creating PdfReader: %s", err)
}
// Populate the form data.
err = pdfReader.AcroForm.Fill(fdfData)
if err != nil {
return fmt.Errorf("Filling AcroForm: %s", err)
}
// Flatten form.
fieldAppearance := annotator.FieldAppearance{OnlyIfMissing: false, RegenerateTextFields: true}
style := fieldAppearance.Style()
style.AutoFontSizeFraction = 0.5
fieldAppearance.SetStyle(style)
err = pdfReader.FlattenFields(true, fieldAppearance)
if err != nil {
return fmt.Errorf("Flattening Fields: %s", err)
}
// Write out.
pdfWriter := model.NewPdfWriter()
pdfVersion := pdfReader.PdfVersion()
pdfWriter.SetVersion(pdfVersion.Major, pdfVersion.Minor)
pdfWriter.SetForms(nil)
for i, p := range pdfReader.PageList {
err := pdfWriter.AddPage(p)
if err != nil {
return fmt.Errorf("Adding Page (%d of %d): %s", (i + 1), len(pdfReader.PageList), err)
}
}
err = pdfWriter.Write(pdfOutput)
if err != nil {
return fmt.Errorf("Writing PDF: %s", err)
}
return nil
}