PDF Viewer - Protect PDF document
GroupDocs.Viewer enables you to protect PDF document by setting permissions, password for opening and password for changing permissions.
The following steps are to be followed in order to set PDF document permissions.
- Create a new instance of the Viewer class and pass the source document path as a constructor parameter.
- Initialize the instance of Security class;
- Set setDocumentOpenPassword(…) property if password is required to open PDF document;
- Set setPermissionsPassword(…) property if it is required to change restrictions applied to PDF document;
- Set setPermissions(…) property to specify exact permissions that should be applied to document;
- Instantiate the PdfViewOptions object and specify saving path format for rendered document.
- Initialize setSecurity(…) property of PdfViewOptions with object created at previous steps;
- Pass PdfViewOptions object to view(…) method of Viewer class.
Following example demonstrates how to protect output PDF document.
Viewer viewer = new Viewer("sample.docx");
Security security = new Security();
security.setDocumentOpenPassword("o123");
security.setPermissionsPassword("p123");
security.setPermissions(Permissions.ALLOW_ALL ^ Permissions.DENY_PRINTING);
PdfViewOptions viewOptions = new PdfViewOptions(filePath);
viewOptions.setSecurity(security);
viewer.view(viewOptions);
viewer.close();
More resources
Advanced Usage Topics
To learn more about document viewing features, please refer to the advanced usage section.
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Viewer for Java examples, plugins, and showcase
- Document Viewer for .NET App WebForms UI Modern Example
- Document Viewer for Java App Dropwizard UI Modern Example
- Document Viewer for Java Spring UI Example
- GroupDocs.Viewer for .NET samples, plugins and showcase
- Document Viewer for .NET MVC UI Example
Free Online App
Along with full-featured Java library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.