How to encrypt a PDF and load it into Flutter PDF Viewer?
This article explains how to encrypt a PDF document using the Flutter PDF library and load the encrypted PDF in the Flutter PDF viewer.
The Syncfusion® Flutter PDF Viewer widget allows you to load a PDF document from various sources, such as Asset, Network, File, and Memory. The SfPdfViewer.memory is used to load a PDF document obtained from the Uint8List, which is used here to load the file that is encrypted.
Step 1: Add dependencies
First, create a new Flutter app and add the following dart packages as dependencies in your pubspec.yaml file:
YAML:
dependencies:
syncfusion_flutter_pdfviewer: ^XX.X.XX # Replace XX.X.XX with the latest version
syncfusion_flutter_pdf: ^XX.X.XX # Replace XX.X.XX with the latest version
Step 2: Import packages
Import the below packages in your Dart file:
DART:
import 'package:syncfusion_flutter_pdf/pdf.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
Step 3: Encrypt PDF
Encrypt the PDF using the Syncfusion® Flutter PDF library: Here, we are creating a new PDF and encrypting the document using the Flutter PDF library.
DART:
Future<void> encryptPdf() async {
// Create a PDF document.
final PdfDocument document = PdfDocument();
// Add a page to the document.
final PdfPage page = document.pages.add();
// Add content to the page.
page.graphics.drawString(
'This pdf is encrypted using Syncfusion Flutter PDF library and\nviewed using Syncfusion Flutter PDF Viewer.',
PdfStandardFont(PdfFontFamily.helvetica, 12),
brush: PdfSolidBrush(PdfColor(0, 0, 0)));
// Set encryption options.
final PdfSecurity security = document.security;
//Specifies encryption algorithm and key size.
security.algorithm = PdfEncryptionAlgorithm.aesx128Bit;
//Set user password.
security.userPassword = 'syncfusion';
// Save the encrypted PDF to a file.
final List<int> bytes = await document.save();
document.dispose();
// Rebuild the widget with the updated bytes.
setState(() {
_documentBytes = Uint8List.fromList(bytes);
});
}
After adding the above code to your project, call this encryptPdf() method on the initState() of the StatefulWidget (say, PdfViewerPage).
Note: You can also encrypt an existing document. Please refer here.
Step 4: View the encrypted PDF
Load and view the encrypted PDF using the Syncfusion® Flutter PDF Viewer. In the StatefulWidget’s (say, PdfViewerPageState) build method, you can create the SfPdfViewer widget and pass the document bytes that we encrypted.
DART:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Syncfusion Flutter PDF Viewer'),
),
body: _documentBytes != null
? SfPdfViewer.memory(
_documentBytes!,
)
: Container());
}
Step 5: Run the app
Finally, run your Flutter app, and you will be greeted with the password dialog. Enter the password that you used to encrypt the PDF, and you should be able to view the encrypted PDF using our Syncfusion® Flutter PDF Viewer.
Example
A complete working example can be downloaded from here
Conclusion
We hope you enjoyed learning how to encrypt the PDF document and load it in the Flutter PDF viewer.
You can refer to our Flutter PDF Viewer feature tour page to learn about its other groundbreaking feature representations. You can also explore our documentation to quickly get started and to understand the other feature usages.
For current customers, you can check out our components on the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk, or feedback portal. We are always happy to assist you!