How to create shape annotations in a PDF document using Syncfusion Flutter PDF Library?
The Syncfusion® Flutter PDF library allows you to create, read and edit PDF documents programmatically without Adobe Dependencies. Using this library, you can add shape annotation in PDF document using Flutter.
Steps to add shape annotation in a PDF document programmatically:
- Create a new Flutter application project.
- Open Visual Studio Code (After installing the Dart and Flutter extensions as stated in this setup editor page).
- Click View -> Command Palette.
- Type Flutter and choose Flutter: New Project.
- Enter the project name and press the Enter button.
- 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 get a comment or 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( 'Add shape annotation to PDF document', style: TextStyle(color: Colors.white), ), style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith( (states) => Colors.blue)), onPressed: _createShapeAnnotations, ) ], ), ), ); }
- Add the following code to the _createShapeAnnotations function to create shape annotation in an existing PDF document programmatically.
Future<void> _createShapeAnnotations() async{ //Load an existing PDF document. final PdfDocument document = PdfDocument(inputBytes: await _readDocumentData('sample.pdf')); //Get the page. final PdfPage page = document.pages[0]; //Create a line annotation. final PdfLineAnnotation lineAnnotation = PdfLineAnnotation( [60, 710, 187, 710], 'Introduction', color: PdfColor(0, 0, 255), author: 'John Milton', border: PdfAnnotationBorder(2), lineCaption: false, setAppearance: true, lineIntent: PdfLineIntent.lineDimension); //Add the line annotation to the page. page.annotations.add(lineAnnotation); //Create an ellipse annotation. final PdfEllipseAnnotation ellipseAnnotation = PdfEllipseAnnotation( Rect.fromLTRB(475, 771, 549, 815), 'Page Number', author: 'John Milton', border: PdfAnnotationBorder(2), color: PdfColor(255, 0, 0), setAppearance: true); //Add the ellipse annotation to the page. page.annotations.add(ellipseAnnotation); //Create a rectangle annotation. final PdfRectangleAnnotation rectangleAnnotation = PdfRectangleAnnotation( Rect.fromLTRB(57, 250, 565, 349), 'Usage', color: PdfColor(255, 170, 0), border: PdfAnnotationBorder(2), author: 'John Milton', setAppearance: true); //Add the rectangle annotation to the page. page.annotations.add(rectangleAnnotation); //Create a polygon annotation. final PdfPolygonAnnotation polygonAnnotation = PdfPolygonAnnotation( [129, 356, 486, 356, 532, 333, 486, 310, 129, 310, 83, 333, 129, 356], 'Chapter 1 Conceptual Overview', color: PdfColor(255, 0, 0), border: PdfAnnotationBorder(2), author: 'John Milton', setAppearance: true); //Add the polygon annotation to the page. page.annotations.add(polygonAnnotation); //Save the document. List<int> bytes = await document.save(); // Dispose the document. document.dispose(); //Save the file and launch/download. SaveFile.saveAndLaunchFile(bytes, 'output.pdf'); }
- Use the following code to load an existing PDF document in the Flutter project.
- Add the following code in your pubspec.yaml file.
flutter: # To add assets to your application, add an assets section, like this: assets: - assets/pdf/
- Import the following package in your main.dart file.
import 'package:flutter/services.dart';
- Add the following code in the lib/main.dart file to read the existing PDF document.
Future<List<int>> _readDocumentData(String name) async { final ByteData data = await rootBundle.load('assets/pdf/$name'); return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes); }
- Follow the below steps to launch the generated PDF file on desktop, mobile, and web.
Web:
- 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';
- 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:
- Add the following dependencies in your pubspec.yaml file.
open_file: ^3.2.1 path_provider: ^2.0.11 #Open source library to launch the PDF file in mobile devices
- 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 'dart:io'; import 'package:open_file/open_file.dart'; import 'package:path_provider/path_provider.dart';
- 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 shape annotation in the document. After the application launches, you will get the PDF document as follows.
A complete working sample can be downloaded from ShapeAnnotationsSample.zip.
Take a moment to peruse the documentation, where you can find other options like document link annotation, text web link annotation, and more. Also, the features like headers and footers, bookmarks, tables, hyperlink, pages in the PDF documents, and more with code examples.
Conclusion
I hope you enjoyed learning about how to create shape annotations in a PDF document using Syncfusion® Flutter PDF Library.
You can refer to our Flutter PDF Library feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter PDF Library example to understand how to create and manipulate data.
For current customers, you can check out our components from 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, Direct-Trac, or feedback portal. We are always happy to assist you!