Tutorials

How to Use ComPDFKit Flutter SDK to Build a New Application

By ComPDFKit | Wed. 01 Nov. 2023
AndroidiOSFlutter

Flutter is widely used to build apps because it is an open-source framework that can be developed cross-platform, including iOS, Android, Web, Windows, Mac, and Linux. It allows developers to create apps from one codebase using a single programming language. Flutter offers customizable widgets and Hot-Reload that make it an excellent choice. Its native-like performance provides a better user experience.

 

ComPDFKit Flutter SDK is a toolkit based on Flutter. It is used to create a PDF viewer and editor like PDF Reader Pro, or add PDF functions on any Flutter app. With ComPDFKit Flutter SDK, developers can customize UI from the powerful PDF libraries according to personal requirements and run apps across Android and iOS seamlessly.

 

In this guide, we'll introduce what features ComPDFKit Flutter SDK can offer, and demonstrate how to use the ComPDFKit Flutter SDK to make a new PDF reader and editor for Android devices.



Features of ComPDFKit Flutter SDK

 

Standard features of ComPDFKit SDK for Flutter are listed below. If you would like to add addtional PDF functions aside from below-mentioned features, please contact our sales to get more information. 

Features Description
Viewer Display modes, PDF navigation, text search & selection, zooming, themes, text reflow
Annotations Various annotation types, create, delete and edit annotations, set and update annotation appearances, import and export annotations, flatten annotations, control annotation permission, predefine annotations, undo and redo
Forms Create, delete and edit forms, support all PDF form fields, flatten forms, import form data into PDF, export form data from PDF, etc.
Signatures Electronic signature and digital signature
Document Editor Manipulate pages like merge, split, and extract; edit pages, like delete, insert, crop, move, rotate, etc. view document information and extract images from PDF
Security Encrypt documents, customize the permission settings, add watermarks, header & footer, bates number, background and so on
Conversion Convert PDF files to any format you need
Content Editor Edit text like changing the size, color, font, alignment, position, etc. Edit PDF images like adding, deleting, moving, rotating, cropping, replacing, scaling, mirroring, copying, and extracting
Redaction Redact content including images, text, and vector graphics to remove sensitive information
Compare Documents Content comparison, overlay comparison, change list, set the colors for different changes
Compress Reduce PDF document size with no or minimum visual quality loss



Prerequisites of Using ComPDFKit Flutter SDK to Build a PDF Viewer for Android

 

If you are interested about how to use ComPDFKit Flutter SDK to create a new PDF viewer or editor like PDF Reader Pro after looking through the above feature list, keep learning about the preconditions of building a PDF reader for Android by using ComPDFKit Flutter SDK.

 

Please Install the Following Required Packages:

         - The latest stable version of Flutter

         - The latest stable version of Android Studio

         - The Android NDK

         - An Android Virtual Device

 

Operating Environment Requirements:

         - A minSdkVersion  of 21 or higher.

         - A compileSdkVersion  of 30 or higher.

         - A targetSdkVersion  of 30 or higher.

         - Android ABI(s): x86, x86_64, armeabi-v7a, arm64-v8a.



How to Create a New Project for Android Using ComPDFKit Flutter SDK

 

Step 1: Create A Flutter Project and Change Directory for It

1. Create a Flutter project called example  with the flutter  CLI:

flutter create --org com.compdfkit.flutter example

2. In the terminal app, change the location of the current working directory to your project:

cd example

 

Step 2:Apply the License Key of ComPDFKit Flutter SDK

1. open example/android/app/src/main/AndroidManifest.xml , add ComPDFKit License and Storage Permission

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.compdfkit.flutter.example">
    
    <!-- Required to read and write documents from device storage -->
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        ...>
        ...
        <!-- Please replace it with your ComPDFKit license -->
+        <meta-data
+            android:name="compdfkit_key"
+            android:value="{your license key}" />
+        <meta-data
+            android:name="compdfkit_secret"
+            android:value="{your license secret}" />
				...
    </application>
</manifest>

2. Open the app’s Gradle build file, android/app/build.gradle:

open android/app/build.gradle

3. Modify the minimum SDK version, All this is done inside the android section:

 android {
     defaultConfig {
-        minSdkVersion flutter.minSdkVersion
+        minSdkVersion 21
         ...
     }
 }

 

Step 3: Set Up ComPDFKit Flutter SDK

1. Add the ComPDFKit dependency in pubspec.yaml

dependencies:
   flutter:
     sdk: flutter
+  compdfkit_flutter: ^1.0.0

2. From the terminal app, run the following command to get all the packages:

flutter pub get

 

Step 4: Load a PDF Document on the Flutter App

1. Open lib/main.dart and replace the entire content with the following code. And fill in the license provided to you in the Viewer ComPDFKit.init method, this simple example will load a PDF document from the local device file system.

import 'dart:io';

import 'package:compdfkit_flutter/compdfkit.dart';

import 'package:flutter/material.dart';

const String DOCUMENT_PATH = 'pdfs/PDF_Document.pdf';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State createState() => _MyAppState();
}

class _MyAppState extends State {
  @override
  void initState() {
    super.initState();
    _init();
  }

  void _init() async {
    // Please replace it with your ComPDFKit license
    ComPDFKit.init('your license key', 'your license secret');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: ElevatedButton(
              onPressed: () async {
                showDocument(context);
              },
              child: const Text(
                'Open Document',
                style: TextStyle(color: Colors.white),
              )),
          ))),
    );
  }

  void showDocument(BuildContext context) async {
    final bytes = await DefaultAssetBundle.of(context).load(DOCUMENT_PATH);
    final list = bytes.buffer.asUint8List();

    final tempDir = await ComPDFKit.getTemporaryDirectory();
    final tempDocumentPath = '${tempDir.path}/$DOCUMENT_PATH';

    final file = await File(tempDocumentPath).create(recursive: true);
    file.writeAsBytesSync(list);
    ComPDFKit.openDocument(tempDocumentPath);
  }
}

2. Add the PDF documents you want to display in the project

        - Create a pdf directory

mkdir pdfs

         - Copy your example document into the newly created pdfs directory and name it PDF_Document.pdf

3. Specify the assets directory in pubspec.yaml

flutter:
+ assets:
+ - pdfs/

 

Step 5: Run the Created App on Android Device

1. Start your Android emulator, or connect a device.

2. Run the app with:

flutter run

 

iOS                                             Android

                     Build a New Android App



Conclusion

 

Flutter makes the process of creating an app much easier with its customizable widgets and high platform compatibility. To build a PDF viewer or editor or add PDF functions to your apps based on Flutter, you can choose a professional toolkit ComPDFKit Flutter SDK. Developers can add all functions that appear in the feature list of PDF Reader Pro , even those that aren't. A PDF viewer and editor created by ComPDFKit SDK for Flutter is able to run on both Android and iOS in the meantime.

 

Want to experience our Demo? Need to know how to add your own desired PDF features? Welcome to contact our sales and acquire quotes and more details.