How To Open A Word or PDF Document On Android
Last updated Mar 30, 2021How to open PDF or Word document in Flutter application, Flutter we have different ways to open PDF or Word documents and view the PDF files. In this post we will create an example to show PDF or Word Document in flutter application. To open and view/edit PDF we are using a a library Flutter PDF Library which will be used to View, Edit PDF and Doc files. In this we have used permission_handler plugin to check the run time permissions in android. The latest permission_handler plugin is updated for null-safety feature. Integrate Flutter PDF Library we need to follow below steps
Step 1: Create a Flutter application
Step 2: Add required dependencies in pubspec.yaml file
permission_handler: ^6.1.1
pdftron_flutter:
git:
url: git://github.com/PDFTron/pdftron-flutter.git
|
Step 3: Update manifest file with below code
<application
android:name="io.flutter.app.FlutterApplication"
android:label="myapp"
android:icon="@mipmap/ic_launcher"
android:largeHeap="true"
android:usesCleartextTraffic="true"
>
|
Add permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
Step 4: Update main.dart file
import 'dart:async';
import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pdftron_flutter/pdftron_flutter.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(PDFViewer());
}
class PDFViewer extends StatefulWidget {
@override
_PDFViewerState createState() => _PDFViewerState();
}
class _PDFViewerState extends State<PDFViewer> {
String _pdf = "https://pdftron.s3.amazonaws.com/downloads/pl/PDFTRON_mobile_about.pdf";
String _document = "https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc";
@override
void initState() {
super.initState();
initPlatformState();
if (Platform.isIOS) {
} else {
// Request for permissions for android before opening document
launchWithPermission();
}
}
Future<void> launchWithPermission() async {
Map<Permission, PermissionStatus> permissions =await [Permission.storage,].request();
if (granted(permissions[Permission.storage])) {
}
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String version;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
PdftronFlutter.initialize("Insert commercial license key here after purchase");
version = await PdftronFlutter.version;
} on PlatformException {
version = '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;
}
void showViewer(file) {
PdftronFlutter.openDocument(file);
}
bool granted(PermissionStatus status) {
return status == PermissionStatus.granted;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.greenAccent,
appBar: AppBar(
backgroundColor: Colors.green,
title: const Text('PDFTron flutter app'),
),
body: Center(
child: GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
children: [
Card(
elevation: 10,
child: FlatButton(child: Text("View Doc",style: TextStyle(fontSize: 20),),color:Colors.blueGrey,textColor:Colors.white,highlightColor: Colors.red,onPressed: (){
showViewer(_document);
},),
),
Card(
elevation: 10,
child: FlatButton(child: Text("View PDF",style: TextStyle(fontSize: 20)),color:Colors.brown,textColor:Colors.white,highlightColor: Colors.red,onPressed: (){
showViewer(_pdf);
},),
)
],
),
/*,*/
),
),
);
}
}
|
Tags: Open PDF Flutter, Open Document, View Document in flutter
Other Flutter Examples
Article Contributed By :
|
|
|
|
4919 Views |