How to create layers in a PDF document using Syncfusion Flutter PDF Library?
The Flutter PDF is a Flutter PDF library used to create, read, and edit PDF documents. Using this library, you can add, update, and remove layers from the PDF document.
Layers also known as Option content, refer to the sections of content in a PDF document that can be selectively viewed or hidden by document authors or consumers.
Steps to create layers in PDF document programmatically
- Create a new Flutter application project.
1.1.Open Visual Studio Code (After installing the Dart and Flutter extensions as stated in the setup editor page.)
1.2.Click View -> Command Palette…
1.3.Type Flutter and choose Flutter: New Project.
1.4.Enter the project name and press the Enter button.
1.5.Now, choose the location of the project.
- Add the following code in your pubspec.yaml file to install the syncfusion® flutter pdf package in your application. It will be automatically downloaded from the pub once you trigger the flutter pub to get a comment or the Get packages option from the Visual Studio Code.
dependencies: syncfusion_flutter_pdf: ^20.3.49
- Import the following package in your main.dart file.
import 'package:syncfusion_flutter_pdf/pdf.dart';
- Add the following code in the lib/main.dart file to create a simple button.
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ TextButton( child: Text( 'Generate PDF', style: TextStyle(color: Colors.white), ), style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith( (states) => Colors.blue)), onPressed: createLayer, ) ], ), ), ); }
- Add the following code to the createLayer function to add layers to the PDF document programmatically.
Future<void> createLayer() async { //Create a new PDF document. PdfDocument document = PdfDocument(); //Add a new page to the PDF document. PdfPage page = document.pages.add(); //Add a layer to the PDF page. page.layers.add(name: 'Layer 1').graphics.drawString( 'Layer 1', PdfStandardFont(PdfFontFamily.helvetica, 20), bounds: Rect.fromLTWH(100, 60, 100, 50), brush: PdfBrushes.red); //Add another layer on the page. page.layers.add(name: 'Layer 2', visible: false).graphics.drawString( 'Layer 2', PdfStandardFont(PdfFontFamily.helvetica, 20), bounds: Rect.fromLTWH(100, 180, 100, 50), brush: PdfBrushes.green); //Add another layer. page.layers.add(name: 'Layer 3').graphics.drawString( 'Layer 3', PdfStandardFont(PdfFontFamily.helvetica, 20), bounds: Rect.fromLTWH(200, 120, 100, 50), brush: PdfBrushes.blue); //Save and dispose the document. final List<int> bytes = await document.save(); document.dispose(); //Save the file and launch/download. SaveFile.saveAndLaunchFile(bytes, 'output.pdf'); }
- Use the following code to save and launch the generated PDF file.
Web:
6.1.Create a new dart file named save_file_web.dart under the lib folder and import the following packages in save_file_web.dart file.
import 'dart:convert'; import 'dart:html';
6.2.Include the following code snippet in save_file_web.dart file to open the PDF document in Web.
class SaveFile { static Future<void> saveAndLaunchFile( List<int> bytes, String fileName) async { AnchorElement( href: 'data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}') ..setAttribute('download', fileName) ..click(); } }
Desktop and Mobile:
6.3.Add the following dependencies in your pubspec.yaml file.
open_file: ^3.2.1 #Open source library to launch the PDF file in mobile devices path_provider: ^2.0.11
6.4.Create a new dart file named save_file_mobile_and_desktop.dart under the lib folder and import the following packages in save_file_mobile_and_desktop.dart file.
import 'package:open_file/open_file.dart'; import 'package:path_provider/path_provider.dart'; import 'dart:io';
6.5.Include the following code snippet in save_file_mobile_and_desktop.dart to open the PDF document in Desktop and Mobile.
class SaveFile { static Future<void> saveAndLaunchFile( List<int> bytes, String fileName) async { //Get external storage directory Directory directory = await getApplicationSupportDirectory(); //Get directory path String path = directory.path; //Create an empty file to write PDF data File file = File('$path/$fileName'); //Write PDF data await file.writeAsBytes(bytes, flush: true); //Open the PDF document in mobile OpenFile.open('$path/$fileName'); } }
- Run the sample using the flutter run command. This will create layers in the PDF document. After the application launches, you will get the PDF document as follows.
A complete working sample can be downloaded from layers.zip
Take a moment to peruse the documentation. You can find other options like adding layers, Toggling the visibility of layers, removing layers from an existing PDF document, nested layer, and flattening the layers in an existing document.