The Syncfusion Flutter PDF Library now allows users to encrypt and decrypt PDF documents in Flutter applications.
PDF encryption allows users to protect their PDF documents from unauthorized access. These days, data theft has become a real problem. So, we need to secure important files before sending them to recipients to prevent unauthorized access to their content. And then, if a PDF document we receive is encrypted, we have to decrypt it to access its content.
There are two types of passwords that are available to protect PDF files:
With the Syncfusion Flutter PDF Library, you can protect your PDF documents with either of these passwords, or both.
If a PDF is encrypted with both types of passwords, you can use either of the passwords to open it. But to change the restricted features, you need the permission password.
In this blog, I will show you how to encrypt and decrypt PDF files, divided into the following topics:
The Syncfusion Flutter PDF Library provides support for basic (RC4) to advanced (AES) encryption standards. The following are the encryption standards supported:
Following are the steps to encrypt a PDF document using Syncfusion Flutter PDF Library.
Follow the instructions provided in this Get Started documentation to create a basic project in Flutter.
Include the Syncfusion Flutter PDF package dependency in the pubspec.yaml file in your project.
dependencies: syncfusion_flutter_pdf: ^18.4.30-beta
Run the following commands to get the required packages.
$ flutter pub get |
Import the PDF package into the main.dart file, as shown in the following code example.
import 'package:syncfusion_flutter_pdf/pdf.dart';
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ FlatButton( child: Text( 'Encrypt PDF', style: TextStyle(color: Colors.white), ), onPressed: _extractText, color: Colors.blue, ) ], ), ), ); }
//Load the existing PDF document. PdfDocument document = PdfDocument( inputBytes: await _readDocumentData('credit_card_statement.pdf')); //Set the document security. document.security.userPassword = 'Password@123'; //Save and dispose the document. List<int> bytes = document.save(); document.dispose();
# To add assets to your application, add an assets section, like this: assets: - assets/credit_card_statement.pdf - assets/secured.pdf
Include the following code to read the PDF document from the folder where it is saved.
Future<List<int>> _readDocumentData(String name) async { final ByteData data = await rootBundle.load('assets/$name'); return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); }
open_file: ^3.0.1 path_provider: ^1.6.5
b. Import the following packages in your main.dart file.
import 'dart:io'; import 'package:open_file/open_file.dart'; import 'package:path_provider/path_provider.dart';
c. Include the following code to launch the output PDF document.
//Get the external storage directory. Directory directory = await getExternalStorageDirectory(); //Get the directory path. String path = directory.path; //Create an empty file to write the PDF data. File file = File('$path/secured.pdf'); //Write the PDF data. await file.writeAsBytes(bytes, flush: true); //Open the PDF document in mobile. OpenFile.open('$path/secured.pdf');
By executing the program, you will get output like the following.
You can also set both the user and owner passwords by using the following code.
//Create document security. PdfSecurity security = document.security; //Set the owner password for the document. security.ownerPassword = 'Owner@123'; //Set the user password for the document. security.userPassword = 'password@123';
You can customize the permissions to allow or restrict certain actions on PDFs, such as:
The following table lists the permission flags supported by Syncfusion Flutter PDF Library.
Permission flag | Description |
accessibilityCopyContent | Enables content screen reading for users with disabilities. |
assembleDocument | Allows users to manipulate the pages in the document. |
copyContent | Allows users to copy content from the document. |
editAnnotations | Allows users to get user input or feedback on a document. |
editContent | Allows users to edit the content in the document. |
fillFields | Allows users to fill out and sign a form. |
fullQualityPrint | Allows users to print a clear copy of the document. |
Allows users to print the document with low quality. |
Note: Only access to the included flags will be enabled and all other permissions will be disabled.
There are also permissions that are interrelated, so enabling one will affect another:
To restrict PDF permissions, you must set the permission password (owner password) for the PDF document. You can set the owner password by using the ownerPassword property available in the PdfSecurity class.
You can set various permissions for the PDF document using the Permissions property available in the PdfSecurity class.
Include the following code to restrict the PDF document permissions.
//Load the existing PDF document. PdfDocument document = PdfDocument( inputBytes: await _readDocumentData('credit_card_statement.pdf')); //Create document security. PdfSecurity security = document.security; //Set the owner password for the document. security.ownerPassword = 'Owner@123'; //Set various permissions. security.permissions.addAll(<PdfPermissionsFlags>[ PdfPermissionsFlags.fullQualityPrint, PdfPermissionsFlags.print, PdfPermissionsFlags.fillFields, PdfPermissionsFlags.copyContent ]); //Save and dispose the document. List<int> bytes = document.save(); document.dispose(); //Open the PDF file. _launchPdf(bytes);
By executing the program, you will get an output document like the following.
You can remove the security from the PDF document by using Syncfusion Flutter PDF Library, making it available for editing. It requires the password to decrypt the PDF document.
//Load the PDF document with user or permission password. PdfDocument document = PdfDocument( inputBytes: await _readDocumentData('secured.pdf'), password: 'owner@123'); //Get the document security. PdfSecurity security = document.security; //Set owner and user passwords to empty. security.userPassword = '';
//Load the PDF document with permission password (since the user password cannot be used to alter the security permissions). PdfDocument document = PdfDocument( inputBytes: await _readDocumentData('secured.pdf'), password: 'owner@123'); //Get the document security. PdfSecurity security = document.security; //Set owner and user passwords to empty. security.ownerPassword = '';
//Load the PDF document with permission password. PdfDocument document = PdfDocument( inputBytes: await _readDocumentData('secured.pdf'), password: 'owner@123'); //Get the document security. PdfSecurity security = document.security; //Set owner and user passwords to empty string. security.userPassword = ''; security.ownerPassword = ''; //Clear the security permissions. security.permissions.clear(); //Save and dispose the document. List<int> bytes = document.save(); document.dispose(); //Open the PDF file. _launchPdf(bytes, 'unsecured.pdf');
By executing this code, you will get an output document like the following screenshot.
You can check out the sample from the following GitHub repository on encrypting and decrypting PDF documents.
In this blog post, we have learned the steps to encrypt and decrypt PDF documents using the Syncfusion Flutter PDF Library in a Flutter application.
Take a moment to peruse our documentation, where you’ll find other options and features, all with accompanying code examples.
If you have any questions about these features, please let us know in the comments below. You can also contact us through our support forum, support portal, or feedback portal. We are happy to assist you!
If you liked this article, we think you would also like the following articles about PDF Library: