|
| 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 | +``` |
0 commit comments