Explore Flutter Encrypt & Decrypt PDF Files

Naveen Srivastava
FlutterDevs
Published in
2 min readJul 28, 2021

--

Flutter is a portable UI toolkit. In other words, it’s a comprehensive app Software Development toolkit (SDK) that comes complete with widgets and tools. Flutter is a free and open-source tool to develop mobile, desktop, web applications. Flutter is a cross-platform development tool. This means that with the same code, we can create both ios and android apps. This is the best way to save time and resources in our entire process. In this, hot reload is gaining traction among mobile developers. Allows us to quickly see the changes implemented in the code with hot reload.

In this article, we will explore the Explore Flutter Encrypt &Decrypt PDF Files using the Syncfusion_flutter_pdf_file_package. With the help of the package, this user can encrypt and decrypt PDF documents in a flutter. So let’s get started.

Table Of Contents :

Responsive Framework

Encrypt and Decrypt PDF

Code Implement

Code File

Conclusion

Encrypt and Decrypt PDF :

Encrypt PDF files allows to protect the user from unauthorized access to PDF documents As data theft has become a big problem nowadays, it is necessary to secure the files before sending them to prevent unauthorized access to their data to the recipient. Happens if a PDF document is encrypted then we will decrypt it first to access its data content.

Provides two types of passwords to protect PDF files.

  • Document open password: Document Open Password Encrypts the PDF document with the user password that is required to open the PDF document.
  • Permission Passwords are used to limit access permissions to tasks such as printing, editing, and copying PDF document content

Implementation :

Step 1: Add the dependencies

Add dependencies to pubspec — yaml file.

dependencies :

dependencies:
syncfusion_flutter_pdf: ^19.2.48-beta
open_file: ^3.0.1
path_provider: ^1.6.5

Step 2: Importing

import 'package:syncfusion_flutter_pdf/pdf.dart';
import ‘dart:io’;
import ‘package:open_file/open_file.dart’;
import ‘package:path_provider/path_provider.dart’;

Step 3: Enable AndriodX

org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true

Code Implement :

Before explaining about encrypt and decrypt the pdf file, we will create a new dart file inside which we will take the column widget and create three different buttons inside it, on the click of each button it will show a different example.

Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed:securePdf, child: Text('Encrypt PDF'),
),


FlatButton(onPressed: securePdf, child: Text('Encrypt PDF')),
FlatButton(
onPressed: restrictPermissions,
child: Text('Restrict Permissions')),
FlatButton(onPressed: decryptPDF, child: Text('Decrypt PDF')),
]),

After defining the button we will first click on the button Encrypt PDF which onPressed has a defined method named securePDF() which loads the PDF document and opens the file of the PDF document in which it is saved.

Future<void> securePdf() async {
PdfDocument document = PdfDocument(
inputBytes: await _readDocumentData('credit_card_statement.pdf'));
document.security.userPassword = 'password@123';
List<int> bytes = document.save();
document.dispose();
_launchPdf(bytes, 'secured.pdf');
}

Note: You can read the full blog here.

--

--