5 Ways to Extract Text from PDF Documents in Flutter
Tags: flutter, pdf, dart, productivity
Published: 2020-12-13 03:56:23

---
title: 5 Ways to Extract Text from PDF Documents in Flutter
published: true
date: 2020-12-10 11:24:18 UTC
tags: flutter, pdf, dart, productivity
canonical_url: https://www.syncfusion.com/blogs/post/5-ways-to-extract-text-from-pdf-documents-in-flutter.aspx
cover_image: https://www.syncfusion.com/blogs/wp-content/uploads/2020/12/5-Ways-to-Extract-Text-from-PDF-Documents-in-Flutter-672x372.png
---

[Syncfusion Flutter PDF library](https://pub.dev/packages/syncfusion_flutter_pdf/) is a file-format library that allows you to add robust PDF functionalities to your Flutter applications. With it, you can create PDF reports programmatically with formatted text, images, tables, links, lists, headers and footers, bookmarks, and more. This library also offers functionalities to read and edit PDF documents without Adobe dependencies.

PDF documents are mostly used for exchanging business data in the form of invoices, purchase orders, shipping notes, reports, presentations, price and product lists, HR forms, and more.

At some point, a user might need to read and validate the data present in a PDF document. This may require some additional cost and time to do this manually. To avoid this problem, we can use text extraction techniques. These techniques will extract all the text data or specific text data from a PDF document to validate further in an automated way.

By using our [Flutter PDF library](https://www.syncfusion.com/flutter-widgets/pdf-library), you can easily extract text from a PDF document in your Flutter application. In this blog, we are going to cover how to do the following:

- [Extract all the text from a PDF document](#extract-all-the-text-from-a-pdf-document).
- [Extract text from predefined bounds](#extract-text-from-predefined-bounds).
- [Extract text from a specific page](#extarct-text-from-a-specific-page).
- [Extract text from a range of pages](#extarct-text-from-a-range-of-pages).
- [Extract text with font and style information](#extract-text-with-font-and-style-information).

And we’ll provide code examples along the way!

## Extract all the text from a PDF document

With the Syncfusion Flutter PDF library, you can extract all the text from a PDF document. Here’s the procedure to do so:

### Step 1: Create a Flutter application

Follow the instructions provided in this [Getting Started](https://flutter.dev/docs/get-started/test-drive?tab=vscode#create-app) documentation to create a basic Flutter application.

### Step #2: Add the Syncfusion Flutter PDF dependency

Include the [Syncfusion Flutter PDF](https://pub.dev/packages/syncfusion_flutter_pdf/) package dependency in the **pubspec.yaml** file in your project. Refer to the following code.

```cmd
dependencies:
  syncfusion_flutter_pdf: ^18.3.50-beta
```

### Step #3: Get the package

Run the following command to get the required package.

| `$ flutter pub get` |

### Step #4: Import the package

Import the PDF package into your **main.dart** file using the following code example.

```dart
import 'package:syncfusion_flutter_pdf/pdf.dart';
```

### Step #5: Extract all the text from the PDF document

1. Add a button widget as a child to your container widget as shown in the following code example.
```dart
@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text(widget.title),
    ),
    body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          FlatButton(
            child: Text(
              'Generate PDF',
              style: TextStyle(color: Colors.white),
            ),
            onPressed: _extractText,
            color: Colors.blue,
          )
        ],
      ),
    ),
  );
}
```
2. Include the following code in the button click event to extract all the text from the entire PDF file.
```dart
//Load an existing PDF document.
PdfDocument document =
    PdfDocument(inputBytes: await _readDocumentData('pdf_succinctly.pdf'));
 
//Create a new instance of the PdfTextExtractor.
PdfTextExtractor extractor = PdfTextExtractor(document);
 
//Extract all the text from the document.
String text = extractor.extractText();
 
//Display the text.
_showResult(text);
```
3. Include the following code to read the PDF document from the folder where it is saved. Here, we have named our folder **[assets](https://github.com/SyncfusionExamples/Extract-text-from-PDF-Flutter/tree/main/assets)**.
```dart
Future<List<int>> _readDocumentData(String name) async {
final ByteData data = await rootBundle.load('assets/$name');
return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
}
```
4. Include the following code to display the extracted text.
```dart
void _showResult(String text) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Extracted text'),
content: Scrollbar(
child: SingleChildScrollView(
child: Text(text),
physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
),
),
actions: [
FlatButton(
child: Text('Close'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
```

By executing the previous code example, the text extracted from the PDF document will be displayed like in the following screenshot.

![Text Extracted from PDF Document](https://www.syncfusion.com/blogs/wp-content/uploads/2020/11/Text-Extracted-from-PDF-Document.png)<figcaption>Text Extracted from PDF Document</figcaption>

## Extract text from predefined bounds

We can extract text from predefined bounds in an existing PDF document. To do this, we need to specify the bounds where the data we want is present in the PDF.

The following code example illustrates the procedure to extract text from specified bounds. Here, we are going to extract the invoice number in the PDF document.

```dart
//Load an existing PDF document.
PdfDocument document =
    PdfDocument(inputBytes: await _readDocumentData('invoice.pdf'));
 
//Create a new instance of the PdfTextExtractor.
PdfTextExtractor extractor = PdfTextExtractor(document);
 
//Extract all the text from a particular page.
List<TextLine> result = extractor.extractTextWithLine(startPageIndex: 0);
 
//Predefined bound.
Rect textBounds = Rect.fromLTWH(474, 161, 50, 9);
 
String invoiceNumber = '';
 
for (int i = 0; i < result.length; i++) {
  List<TextWord> wordCollection = result[i].wordCollection;
  for (int j = 0; j < wordCollection.length; j++) {
    if (textBounds.overlaps(wordCollection[j].bounds)) {
      invoiceNumber = wordCollection[j].text;
      break;
    }
  }
  if(invoiceNumber != ''){
    break;
  }
}
 
//Display the text.
_showResult(invoiceNumber);
```

Executing the above code example will display the output text shown in the following screenshot.

![Text Extracted from Predefined Bounds](https://www.syncfusion.com/blogs/wp-content/uploads/2020/11/Text-Extracted-from-Predefined-Bounds.png)<figcaption>Text Extracted from Predefined Bounds</figcaption>

## Extract text from a specific page

We can extract text from a particular page from a PDF document by passing the specific page index to the **extractText** method.

The following code example illustrates how to do this.

```dart
//Load an existing PDF document.
PdfDocument document =
    PdfDocument(inputBytes: await _readDocumentData('pdf_succinctly.pdf'));
 
//Create a new instance of the PdfTextExtractor.
PdfTextExtractor extractor = PdfTextExtractor(document);
 
//Extract all the text from the first page of the PDF document.
String text = extractor.extractText(startPageIndex: 0);
 
//Display the text.
_showResult(text);
```

Executing the above code example will display the text from the first page like in the following screenshot.

![Text Extracted from a Specific Page](https://www.syncfusion.com/blogs/wp-content/uploads/2020/11/Text-Extracted-from-a-Specific-Page.png)<figcaption>Text Extracted from a Specific Page</figcaption>

## Extract text from a range of pages

We can also extract text from a range of pages in a PDF document by providing the start and end page indices to the **extractText** method. The following example illustrates how to extract text from a range of pages.

```dart
//Load the existing PDF document.
PdfDocument document =
    PdfDocument(inputBytes: await _readDocumentData('pdf_succinctly.pdf'));
 
//Create the new instance of the PdfTextExtractor.
PdfTextExtractor extractor = PdfTextExtractor(document);
 
//Extract all the text from the first page to third page of the PDF document.
String text = extractor.extractText(startPageIndex: 0, endPageIndex: 2);
 
//Display the text.
_showResult(text);
```

## Extract text with font and style information

You can also extract text with its bounds, font name, font style, and font size. The following code example illustrates how to extract text with its details.

```dart
//Load an existing PDF document.
PdfDocument document =
PdfDocument(inputBytes: await _readDocumentData('invoice.pdf'));

//Create a new instance of the PdfTextExtractor.
PdfTextExtractor extractor = PdfTextExtractor(document);

//Extract all the text from specific page.
List<TextLine> result = extractor.extractTextWithLine(startPageIndex: 0);

//Draw rectangle.
for (int i = 0; i < result.length; i++) {
List<TextWord> wordCollection = result[i].wordCollection;
for (int j = 0; j < wordCollection.length; j++) {
if ('2058557939' == wordCollection[j].text) {
//Get the font name.
String fontName = wordCollection[j].fontName;
//Get the font size.
double fontSize = wordCollection[j].fontSize;
//Get the font style.
List<PdfFontStyle> fontStyle = wordCollection[j].fontStyle;
//Get the text.
String text = wordCollection[j].text;
String fontStyleText = '';
for (int i = 0; i < fontStyle.length; i++) {
fontStyleText += fontStyle[i].toString() + ' ';
}
fontStyleText = fontStyleText.replaceAll('PdfFontStyle.', '');
_showResult(
'Text : $text \r\n Font Name: $fontName \r\n Font Size: $fontSize \r\n Font Style: $fontStyleText');
break;
}
}
}
//Dispose the document.
document.dispose();
```

Executing the above code example will provide the output shown in the following screenshot.

![Extarcted Text Information](https://www.syncfusion.com/blogs/wp-content/uploads/2020/11/Extarcted-Text-Information.png)<figcaption>Extarcted Text Information</figcaption>

## GitHub sample:

You can check out samples for all these extraction types in this [GitHub repository](https://github.com/SyncfusionExamples/Extract-text-from-PDF-Flutter/).

## Conclusion

In this blog post, we have covered five different ways to extract text from a PDF document in Flutter applications using the Syncfusion Flutter PDF library. Take a moment to peruse our [documentation](https://help.syncfusion.com/flutter/pdf/working-with-text-extraction), where you’ll find other options and features, all with accompanying code examples.

If you have any questions about these features, please let us know in the comments section below. You can also contact us through our [support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://www.syncfusion.com/support/directtrac/?_ga=2.203813140.507257297.1548648173-2146943073.1535695045), or [feedback portal](https://www.syncfusion.com/feedback/pdf). We are happy to assist you!

If you like this article, we think you will also like the following articles about our PDF Library:

- [Create and Validate PDF Digital Signatures in C#](https://www.syncfusion.com/blogs/post/create-validate-pdf-digital-signatures-csharp.aspx)
- [9 Types of Useful Data You Can Extract from a PDF Using C#](https://dev.to/kartik110895/9-types-of-useful-data-you-can-extract-from-a-pdf-using-c-5d69-temp-slug-7256836)
- [6 Easy Ways to Split a PDF Document Using C#](https://dev.to/karthickramasamy08/6-easy-ways-to-split-a-pdf-document-using-c-1hem-temp-slug-8330313)
- [Simple Steps to Encrypt and Decrypt PDF Files Using C#](https://dev.to/syncfusion/simple-steps-to-encrypt-and-decrypt-pdf-files-using-c-4l8i)
- [7 Ways to Compress PDF Files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx)