-
Notifications
You must be signed in to change notification settings - Fork 101
/
pdf_protect.go
121 lines (98 loc) · 2.95 KB
/
pdf_protect.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Protects PDF files by setting a password on it. This example both sets user
* and opening password and hard-codes the protection bits here, but easily adjusted
* in the code here although not on the command line.
*
* The user-pass is a password required to view the file with the access specified by certain permission flags (specified
* in the code example below), whereas the owner pass is needed to have full access to the file.
* See pdf_check_permissions.go for an example about checking the permissions for a given PDF file.
*
* If anyone is supposed to be able to read the PDF under the given access restrictions, then the user password should
* be left empty ("").
*
* Run as: go run pdf_protect.go input.pdf <user-pass> <owner-pass> output.pdf
* Sets a user and owner password for the PDF.
*/
package main
import (
"fmt"
"os"
"github.com/unidoc/unipdf/v3/core/security"
pdf "github.com/unidoc/unipdf/v3/model"
)
func main() {
if len(os.Args) < 5 {
fmt.Println("Usage: go run pdf_protect.go input.pdf <user-pass> <owner-pass> output.pdf")
fmt.Println("Sets a user and owner password for the PDF.")
os.Exit(1)
}
inputPath := os.Args[1]
userPassword := os.Args[2]
ownerPassword := os.Args[3]
outputPath := os.Args[4]
err := protectPdf(inputPath, outputPath, userPassword, ownerPassword)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fmt.Printf("Complete, see output file: %s\n", outputPath)
}
func protectPdf(inputPath string, outputPath string, userPassword, ownerPassword string) error {
pdfWriter := pdf.NewPdfWriter()
permissions := security.PermPrinting | // Allow printing with low quality
security.PermFullPrintQuality |
security.PermModify | // Allow modifications.
security.PermAnnotate | // Allow annotations.
security.PermFillForms |
security.PermRotateInsert | // Allow modifying page order, rotating pages etc.
security.PermExtractGraphics | // Allow extracting graphics.
security.PermDisabilityExtract // Allow extracting graphics (accessibility)
encryptOptions := &pdf.EncryptOptions{
Permissions: permissions,
}
err := pdfWriter.Encrypt([]byte(userPassword), []byte(ownerPassword), encryptOptions)
if err != nil {
return err
}
f, err := os.Open(inputPath)
if err != nil {
return err
}
defer f.Close()
pdfReader, err := pdf.NewPdfReader(f)
if err != nil {
return err
}
isEncrypted, err := pdfReader.IsEncrypted()
if err != nil {
return err
}
if isEncrypted {
return fmt.Errorf("The PDF is already locked (need to unlock first)")
}
numPages, err := pdfReader.GetNumPages()
if err != nil {
return err
}
for i := 0; i < numPages; i++ {
pageNum := i + 1
page, err := pdfReader.GetPage(pageNum)
if err != nil {
return err
}
err = pdfWriter.AddPage(page)
if err != nil {
return err
}
}
fWrite, err := os.Create(outputPath)
if err != nil {
return err
}
defer fWrite.Close()
err = pdfWriter.Write(fWrite)
if err != nil {
return err
}
return nil
}