How to Implement Foxit PDF SDK for Android using Flutter Plugin
Flutter is Google’s own framework for cross-platform mobile development. It was built using Dart, a portable and easily scalable Google programming language. Most efficient in the development of Android and iOS apps, Dart also has the same abstraction level as Javascript, which makes developing its apps to the Web effortless.
This section will help you get started with Foxit PDF SDK for Android and the Flutter plugin on Windows.
System Requirements
- Foxit PDF SDK 6.2.1 or higher
- Flutter 1.0.0 or higher
Note: The version of Foxit PDF SDK for Android should match the version of the ‘flutter-foxitpdf‘ that you choose and download.
Installation
Install Flutter 1.0.0
Please refer to Flutter official Getting Started page to install, set up and editor and create a Flutter Project.
Integrate Foxit PDF SDK for Android
This section will help you to quickly build a full-featured PDF Reader in Android platform using Flutter. For these steps, make sure you have created a Flutter project called ‘testflutter’ and it is functional in your chosen IDE.
Download Foxit PDF SDK for Android package, extract it, and follow the steps below:
- Copy the following files (libraries and licenses) in the “libs” folder of the extracted package to testflutter\android\libs directory:
- FoxitRDK.aar
- FoxitRDKUIExtensions.aar
- RMSSDK-4.2-release.aar
- rms-sdk–ui.aar
- rdk_key.txt
- rdk_sn.txt
- Add the dependencies to your Flutter project in ‘testflutter/pubspec.yaml’:
dependencies: flutter: sdk: flutter flutter_foxitpdf: git: url: git://github.com/foxitsoftware/flutter-foxitpdf.git
- Adjust the ‘testflutter/android/build.gradle’ file:
allprojects { repositories { google() jcenter() flatDir { dirs project(':flutter_foxitpdf').file("$rootDir/libs") } } }
- Adjust the testflutter/android/app/src/main/AndroidManifest.xml file.
- Replace ‘lib/main.dart’, see Usage section for more information on this step.
- Replace YOUR_RDK_SN and YOUR_RDK_KEY with your own license (rdk_key.txt, rdk_sn.txt):
class _MyAppState extends State { String _platformVersion = 'Unknown'; int _error = -1; String _sn = 'YOUR_RDK_SN'; String _key = 'YOUR_RDK_KEY';
Plugin Usage
- In the “lib/main.dart” file, you can import the plugin using the following code:
import 'package:flutter_foxitpdf/flutter_foxitpdf.dart';
- Then, call the function below to open a PDF document:
openDocument(_path, null);
- Update the “lib/main.dart” file as below, which displays Foxit PDF SDK for Android PDFReader and opens a PDF document:
import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'dart:async'; import 'package:flutter/services.dart'; import 'package:flutter_foxitpdf/flutter_foxitpdf.dart'; void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { String _platformVersion = 'Unknown'; int _error = -1; String _sn = 'YOUR_RDK_SN'; String _key = 'YOUR_RDK_KEY'; @override void initState() { super.initState(); initPlatformState(); init(_sn, _key); openDocument(_path, null); } // Platform messages are asynchronous, so we initialize in an async method. Future initPlatformState() async { String platformVersion; // Platform messages may fail, so we use a try/catch PlatformException. try { platformVersion = await FlutterFoxitpdf.platformVersion; } on PlatformException { platformVersion = 'Failed to get platform version.'; } // If the widget was removed from the tree while the asynchronous platform // message was in flight, we want to discard the reply rather than calling // setState to update our non-existent appearance. if (!mounted) return; setState(() { _platformVersion = platformVersion; }); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), ), body: Center( child: Text('Running on: $_platformVersion\n'), ), ), ); } Future init(String sn, String key) async { int error; try { error = await FlutterFoxitpdf.initialize(sn, key); } on PlatformException { error = -1; } setState(() { _error = error; }); } Future openDocument(String path, Uint8List password) async { await FlutterFoxitpdf.openDocument(path, password); } }
Run the Project
You can run this project in Android Studio or by using the command below:
flutter run
Before running this command, you need to set up the device or emulator first.
After running the project successfully, the “complete_pdf_viewer_guide_android.pdf” document will be displayed as in figure 1-1:
Figure 1-1
Note: You may also clone the plugin and run example in the project. In that case, you need to copy the libs to the example/libs folder.
API Reference
Initialize Foxit PDF SDK
FlutterFoxitpdf.initialize(String, String);
Open a PDF document
FlutterFoxitpdf.openDocument(String, Uint8List)
Updated on February 26, 2019