Skip to content

Commit 3a5b814

Browse files
Aspose.PDF for Go via C++: OpenWithPassword, Encrypt, Decrypt, SetPermissions, GetPermissions
1 parent c02fe04 commit 3a5b814

7 files changed

Lines changed: 384 additions & 0 deletions

File tree

english/go-cpp/_index.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ type Document struct {
127127
| [PageIsBlank](./core/pageisblank/) | Return page is blank in PDF-document. |
128128

129129

130+
## Security
131+
132+
| Function | Description |
133+
| -------- | ----------- |
134+
| [OpenWithPassword](./security/openwithpassword/) | Open a password-protected PDF-document. |
135+
| [Encrypt](./security/encrypt/) | Encrypt PDF-document. |
136+
| [Decrypt](./security/decrypt/) | Decrypt PDF-document. |
137+
| [SetPermissions](./security/setpermissions/) | Set permissions for PDF-document. |
138+
| [GetPermissions](./security/getpermissions/) | Get current permissions of PDF-document. |
139+
140+
130141
## Miscellaneous
131142

132143
| Function | Description |
@@ -179,3 +190,29 @@ const (
179190
PageSizeP11x17 int32 = 11 // P11x17 size.
180191
)
181192
```
193+
194+
## Enumeration of possible crypto algorithms.
195+
```go
196+
type CryptoAlgorithm int32
197+
const (
198+
RC4x40 CryptoAlgorithm = 0 // RC4 with key length 40.
199+
RC4x128 CryptoAlgorithm = 1 // RC4 with key length 128.
200+
AESx128 CryptoAlgorithm = 2 // AES with key length 128.
201+
AESx256 CryptoAlgorithm = 3 // AES with key length 256.
202+
)
203+
```
204+
205+
## Enumeration of possible permissions.
206+
```go
207+
type Permissions int32
208+
const (
209+
PrintDocument Permissions = 1 << 2 // 4
210+
ModifyContent Permissions = 1 << 3 // 8
211+
ExtractContent Permissions = 1 << 4 // 16
212+
ModifyTextAnnotations Permissions = 1 << 5 // 32
213+
FillForm Permissions = 1 << 8 // 256
214+
ExtractContentWithDisabilities Permissions = 1 << 9 // 512
215+
AssembleDocument Permissions = 1 << 10 // 1024
216+
PrintingQuality Permissions = 1 << 11 // 2048
217+
)
218+
```

english/go-cpp/security/_index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: "Security functions"
3+
second_title: Aspose.PDF for Go via C++
4+
description: "Security functions."
5+
type: docs
6+
url: /go-cpp/security/
7+
---
8+
9+
## Security
10+
11+
| Function | Description |
12+
| -------- | ----------- |
13+
| [OpenWithPassword](./openwithpassword/) | Open a password-protected PDF-document. |
14+
| [Encrypt](./encrypt/) | Encrypt PDF-document. |
15+
| [Decrypt](./decrypt/) | Decrypt PDF-document. |
16+
| [SetPermissions](./setpermissions/) | Set permissions for PDF-document. |
17+
| [GetPermissions](./getpermissions/) | Get current permissions of PDF-document. |
18+
19+
20+
## Detailed Description
21+
22+
Security functions.
23+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: "Decrypt"
3+
second_title: Aspose.PDF for Go via C++
4+
description: "Decrypt PDF-document."
5+
type: docs
6+
url: /go-cpp/security/decrypt/
7+
---
8+
9+
_Decrypt PDF-document._
10+
11+
```go
12+
func (document *Document) Decrypt() error
13+
```
14+
15+
**Parameters**:
16+
17+
**Return**:
18+
* **error** - contains an error or nil if absent
19+
20+
21+
**Example**:
22+
```go
23+
package main
24+
25+
import "github.com/aspose-pdf/aspose-pdf-go-cpp"
26+
import "log"
27+
28+
func main() {
29+
// OpenWithPassword(filename string, password string) opens a password-protected PDF-document
30+
pdf, err := asposepdf.OpenWithPassword("sample_with_password.pdf", "ownerpass")
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
// Close() releases allocated resources for PDF-document
35+
defer pdf.Close()
36+
// Decrypt() decrypts PDF-document
37+
err = pdf.Decrypt()
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
// SaveAs(filename string) saves previously opened PDF-document with new filename
42+
err = pdf.SaveAs("sample_without_password.pdf")
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
}
47+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: "Encrypt"
3+
second_title: Aspose.PDF for Go via C++
4+
description: "Encrypt PDF-document."
5+
type: docs
6+
url: /go-cpp/security/encrypt/
7+
---
8+
9+
_Encrypt PDF-document._
10+
11+
```go
12+
func (document *Document) Encrypt(userPassword string, ownerPassword string, permissions Permissions, cryptoAlgorithm CryptoAlgorithm, usePdf20 bool) error
13+
```
14+
15+
**Parameters**:
16+
* **userPassword** - user password
17+
* **ownerPassword** - owner password
18+
* **permissions** - defines allowed permissions for user (can combine flags using |):
19+
```go
20+
type Permissions int32
21+
const (
22+
PrintDocument Permissions = 1 << 2 // 4
23+
ModifyContent Permissions = 1 << 3 // 8
24+
ExtractContent Permissions = 1 << 4 // 16
25+
ModifyTextAnnotations Permissions = 1 << 5 // 32
26+
FillForm Permissions = 1 << 8 // 256
27+
ExtractContentWithDisabilities Permissions = 1 << 9 // 512
28+
AssembleDocument Permissions = 1 << 10 // 1024
29+
PrintingQuality Permissions = 1 << 11 // 2048
30+
)
31+
```
32+
* **cryptoAlgorithm** - encryption algorithm:
33+
```go
34+
type CryptoAlgorithm int32
35+
const (
36+
RC4x40 CryptoAlgorithm = 0 // RC4 with key length 40.
37+
RC4x128 CryptoAlgorithm = 1 // RC4 with key length 128.
38+
AESx128 CryptoAlgorithm = 2 // AES with key length 128.
39+
AESx256 CryptoAlgorithm = 3 // AES with key length 256.
40+
)
41+
```
42+
* **usePdf20** - if true, uses PDF 2.0 encryption (for AESx128/256); otherwise uses standard PDF 1.x encryption
43+
44+
**Return**:
45+
* **error** - contains an error or nil if absent
46+
47+
48+
**Example**:
49+
```go
50+
package main
51+
52+
import "github.com/aspose-pdf/aspose-pdf-go-cpp"
53+
import "log"
54+
55+
func main() {
56+
// New creates a new PDF-document
57+
pdf, err := asposepdf.New()
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
// Close() releases allocated resources for PDF-document
62+
defer pdf.Close()
63+
// Encrypt(userPassword, ownerPassword, permissions, cryptoAlgorithm, usePdf20) encrypts PDF-document
64+
err = pdf.Encrypt(
65+
"userpass",
66+
"ownerpass",
67+
asposepdf.PrintDocument|asposepdf.ModifyContent|asposepdf.FillForm,
68+
asposepdf.AESx128,
69+
true,
70+
)
71+
if err != nil {
72+
log.Fatal(err)
73+
}
74+
// SaveAs(filename string) saves previously opened PDF-document with new filename
75+
err = pdf.SaveAs("sample_with_password.pdf")
76+
if err != nil {
77+
log.Fatal(err)
78+
}
79+
}
80+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: "GetPermissions"
3+
second_title: Aspose.PDF for Go via C++
4+
description: "Get permissions for PDF-document."
5+
type: docs
6+
url: /go-cpp/security/getpermissions/
7+
---
8+
9+
_Get permissions for PDF-document._
10+
11+
```go
12+
func (document *Document) GetPermissions() (Permissions, error)
13+
```
14+
15+
**Parameters**:
16+
17+
**Return**:
18+
* **Permissions** - bitmask of permissions:
19+
```go
20+
type Permissions int32
21+
const (
22+
PrintDocument Permissions = 1 << 2 // 4
23+
ModifyContent Permissions = 1 << 3 // 8
24+
ExtractContent Permissions = 1 << 4 // 16
25+
ModifyTextAnnotations Permissions = 1 << 5 // 32
26+
FillForm Permissions = 1 << 8 // 256
27+
ExtractContentWithDisabilities Permissions = 1 << 9 // 512
28+
AssembleDocument Permissions = 1 << 10 // 1024
29+
PrintingQuality Permissions = 1 << 11 // 2048
30+
)
31+
```
32+
* **error** - contains an error or nil if absent
33+
34+
35+
**Example**:
36+
```go
37+
package main
38+
39+
import "github.com/aspose-pdf/aspose-pdf-go-cpp"
40+
import (
41+
"fmt"
42+
"log"
43+
"strings"
44+
)
45+
46+
var permissionNames = map[asposepdf.Permissions]string{
47+
asposepdf.PrintDocument: "Allow printing",
48+
asposepdf.ModifyContent: "Allow modifying content (except forms/annotations)",
49+
asposepdf.ExtractContent: "Allow copying/extracting text and graphics",
50+
asposepdf.ModifyTextAnnotations: "Allow adding/modifying text annotations",
51+
asposepdf.FillForm: "Allow filling interactive forms",
52+
asposepdf.ExtractContentWithDisabilities: "Allow content extraction for accessibility",
53+
asposepdf.AssembleDocument: "Allow inserting/rotating/deleting pages or changing structure",
54+
asposepdf.PrintingQuality: "Allow high-quality / faithful printing",
55+
}
56+
57+
func PermissionsToString(p asposepdf.Permissions) string {
58+
var result []string
59+
for flag, name := range permissionNames {
60+
if p&flag != 0 {
61+
result = append(result, name)
62+
}
63+
}
64+
if len(result) == 0 {
65+
return "None"
66+
}
67+
return strings.Join(result, ", ")
68+
}
69+
70+
func main() {
71+
// OpenWithPassword(filename string, password string) opens a password-protected PDF-document
72+
pdf, err := asposepdf.OpenWithPassword("sample_with_permissions.pdf", "ownerpass")
73+
if err != nil {
74+
log.Fatal(err)
75+
}
76+
// Close() releases allocated resources for PDF-document
77+
defer pdf.Close()
78+
// GetPermissions() gets current permissions of PDF-document
79+
permissions, err := pdf.GetPermissions()
80+
if err != nil {
81+
log.Fatal(err)
82+
}
83+
// Print permissions
84+
fmt.Println("Permissions:", PermissionsToString(permissions))
85+
}
86+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: "OpenWithPassword"
3+
second_title: Aspose.PDF for Go via C++
4+
description: "Open a password-protected PDF-document."
5+
type: docs
6+
url: /go-cpp/security/openwithpassword/
7+
---
8+
9+
_Open a password-protected PDF-document._
10+
11+
```go
12+
func OpenWithPassword(filename string, password string) (*Document, error)
13+
```
14+
15+
**Parameters**:
16+
* **\*Document** - pointer to document
17+
* **filename** - full file name of the PDF-document
18+
* **password** - user/owner password of the password-protected PDF-document
19+
20+
**Return**:
21+
* **error** - contains an error or nil if absent
22+
23+
24+
**Example**:
25+
```go
26+
package main
27+
28+
import "github.com/aspose-pdf/aspose-pdf-go-cpp"
29+
import "log"
30+
31+
func main() {
32+
// OpenWithPassword(filename string, password string) opens a password-protected PDF-document
33+
pdf, err := asposepdf.OpenWithPassword("sample_with_password.pdf", "ownerpass")
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
// Close() releases allocated resources for PDF-document
38+
defer pdf.Close()
39+
// working...
40+
}
41+
```

0 commit comments

Comments
 (0)