Welcome folks today in this blog post we will be seeing how to export raw text to pdf
document in android using the iTextPDF
library in java. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new android
project inside the android studio and then you will see the below directory
structure as shown below at the end
of the project.
Now first of all you need to go to build.gradle
file and copy paste the below dependencies
as shown below
build.gradle
1 2 3 |
dependencies { implementation 'com.itextpdf:itextpdf:5.5.13.2' } |
Now we need to edit the AndroidManifest.xml
file and add the necessary permissions
for saving the pdf
document inside the external storage
and also we need to add the provider
tag as well as shown below
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.Imagetopdf" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.imagetopdf.provider" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application> </manifest> |
Here you need to replace your own package name
and structure and now we need to create this file_paths.xml
file inside the xml
directory which holds the path where we will be saving the pdf
documents.
xml/file_paths.xml
1 2 3 4 |
<paths> <external-path name="external_files" path="." /> <root-path name="external_storage_root" path="/" /> </paths> |
Editing the Main Layout
Now we need to edit the activity_main.xml
file for the project where we will have the simple EditText
widget where user can write any text and then we have the button
to export the text to pdf document.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="start|top" android:hint="Enter text here" /> <Button android:id="@+id/export_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Export to PDF" android:layout_below="@id/edit_text" android:layout_centerHorizontal="true" android:layout_marginTop="16dp"/> </RelativeLayout> |
And now we need to edit the MainActivity.java
file and copy paste the following code
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.example.rawtexttopdf; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.FileProvider; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.itextpdf.text.Document; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; public class MainActivity extends AppCompatActivity { EditText editText; Button exportButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.edit_text); exportButton = findViewById(R.id.export_button); exportButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String text = editText.getText().toString().trim(); if (text.isEmpty()) { Toast.makeText(MainActivity.this, "Please enter some text", Toast.LENGTH_SHORT).show(); return; } try { File pdfFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "my_pdf_file.pdf"); FileOutputStream outputStream = new FileOutputStream(pdfFile); Document document = new Document(); PdfWriter.getInstance(document, outputStream); Uri pdfUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", pdfFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(pdfUri, "application/pdf"); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); document.open(); document.add(new Paragraph(text)); document.close(); Toast.makeText(MainActivity.this, "PDF file exported successfully", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "Error exporting PDF file", Toast.LENGTH_SHORT).show(); } } }); } } |
As you can see in the above java
code we are getting the reference of the button
and then we are binding the onClick
listener to it and inside it we are getting the text
written inside the EditText
widget and then we are importing the iTextPDF
library and inserting the paragraph
of text inside the pdf document and then we are saving it inside the external
storage and opening it using the Intent
class when it is saved.