Convert Word Documents to PDF using Android Library

Word to PDF Converter in Android

The Word to PDF is one of the widely practiced document conversions and this is the reason MS Word provides a built-in feature to save Word documents as PDF. Since PDF is a preferred format for sharing the documents or keeping them online, the need for Word to PDF conversion occurs in various scenarios. On the other hand, Android-powered smartphones have made humans’ life easier by putting a multitude of functionality within the phones via apps. Keeping an eye on these trends, in this article, I’ll show you how to convert Word documents to PDF within an Android app. For demonstration, we’ll build a simple Word to PDF Converter app for Android within a few steps having the following features.

  • Convert a Word document to PDF
  • Save PDF in phone’s storage
  • View PDF within the app

Word to PDF Converter Library for Android

For converting MS Word documents into PDF format, we’ll use Aspose.Words for Android via Java that lets you convert DOC/DOCX documents to PDF files seamlessly using a couple of lines of code. You can either download the API or install it using the Maven configuration.

Steps to Convert Word to PDF in Android

The following are the steps to create a simple Word to PDF Converter app in Android using Aspose.Words for Android via Java:

  • Create a new project in Android Studio (or Eclipse) and select the “Empty Activity” template.
create new project in android studio
  • Configure your project.
Configure Android Project
  • Open the build.gradle file.
update build.gradle in android studio
  • Add the following repositories section in build.gradle.
repositories {
    mavenCentral()
    maven { url "https://repository.aspose.com/repo/" }
}
  • Add the following entries in the dependencies section of build.gradle.
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.android.support:multidex:2.0.0'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
compile (group: 'com.aspose', name: 'aspose-words', version: '20.6', classifier: 'android.via.java')
  • Enable multidex by adding the following entry under the defaultConfig section in build.gradle.
// enable multiDex
multiDexEnabled true
  • The complete build.gradle file will look like the following:
  • Open activity_main.xml file.
update layout xml
  • Paste the following script for the layout of the main activity.
  • Open MainActivity.java file.
add word to pdf converter code
  • Paste the following Java code in MainActivity.java.
  • Build the app and run it within your Android smartphone or a virtual device.
  • Allow this app to access the storage by going to Settings->Apps->Permissions->Permission manager->Storage.
Android Word to PDF Converter

Word to PDF Converter Android App – Demo

The following is the demonstration of how to convert a Word DOCX document to PDF using Word to PDF Converter app we have just created.

Word to PDF Convetrer – Source Code

Download the complete source code of Word to PDF Converter app from GitHub.

Conclusion

In this article, you have learned how to convert Word to PDF within Android apps. You can integrate similar functionality within your own app or enhance this converter up to your desired level. You may learn more about Aspose.Words for Android via Java from the documentation.

See Also