How to Extract Text From Flutter PDF File?
The Syncfusion® Flutter PDF library is used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can extract or find text from an existing PDF document.
Steps to extract text from an existing 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( 'Extract Text from PDF', style: TextStyle(color: Colors.white), ), style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith( (states) => Colors.blue)), onPressed: _extractTextFromPDF, ) ], ), ), ); }
- Add the following code to the _extractTextFromPDF function to extract text from a PDF document programmatically.
Future<void> _extractTextFromPDF() async{ //Load the PDF document final PdfDocument document = PdfDocument(inputBytes: await _readDocumentData('sample.pdf')); //Create PDF text extractor to extract text PdfTextExtractor extractor = PdfTextExtractor(document); //Extract text String text = extractor.extractText(); // Dispose the document document.dispose(); //Save the file and launch/download SaveFile.saveAndLaunchFile(text, 'output.txt'); }
- Use the following code to load an existing PDF document in 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 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 extracted text file on desktop, mobile, and web platforms.
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 extracted text file in Web platforms.
class SaveFile { static Future<void> saveAndLaunchFile( String text, String fileName) async { List<int> bytes = utf8.encode(text); 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 extracted text 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 extracted text file 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 flutter run command. This will extract text from the document. After the application launches, you will get the text as follows,
A complete working sample can be downloaded from ExtractTextFromPDF.zip.
Take a moment to peruse the documentation, where you can find other options like extract text with bounds and find text. Also, the features like headers and footers, bookmarks, tables, hyperlink, annotations and more with code examples.
Conclusion
I hope you enjoyed learning about how to extract text from Flutter PDF File.
You can refer to our Flutter PDF Library page to know about its other groundbreaking feature representations. You can also explore our Flutter PDF Documentation to understand how to manipulate data.
For current customers you can check out on our Flutter components from the License and Download page. If you are new to Syncfusion, you can try our 30-day free trial to check out our Flutter PDF and other Flutter components.
If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!