Skip to main content
Skip table of contents

Creating a Hello World Application With iText on Android

In this guide, We will build a simple native Android application that generates a “Hello World” PDF using iText 9. The generated PDF gets saved to the device’s local storage.

This tutorial uses a Java-based Android project, but the same steps could be followed if you prefer to work with Kotlin. The only difference would be in the syntax of the MainActivity class.

Before We Start

Gradle Configuration

Open the module-level build.gradle and add the following iText dependency:

CODE
dependencies {
    implementation 'com.itextpdf.android:itext-core-android:9.2.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
}

Make sure that the top-level settings.gradle include iText’s Android repository:

CODE
pluginManagement {
    repositories {
        google {
            content {
                includeGroupByRegex("com\\.android.*")
                includeGroupByRegex("com\\.google.*")
                includeGroupByRegex("androidx.*")
            }
        }
        mavenCentral()
        gradlePluginPortal()
        maven { url "https://repo.itextsupport.com/android" }
    }
}

Creating the Layout

Create a layout file at app/src/main/res/layout/activity_main.xml

Add a Button here with the following piece of XML code:

CODE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="vertical"
              android:padding="24dp">
    <Button
            android:id="@+id/generate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Generate Sample PDF" />
</LinearLayout>

MainActivity.Java

This is where the code for generating PDFs with iText resides:

JAVA
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button generateButton = findViewById(R.id.generate);
        generateButton.setOnClickListener(v -> generatePdf());
    }

    private void generatePdf() {
        try {
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "sample_01.pdf");
            PdfWriter writer = new PdfWriter(file.getAbsolutePath());
            PdfDocument pdfDoc = new PdfDocument(writer);
            Document doc = new Document(pdfDoc);
            doc.add(new Paragraph("Hello from iText in Android!"));
            doc.close();
            Toast.makeText(this, "PDF saved:\n" + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

Testing the PDF Generation

👉 Start an Android Emulator or connect a device
👉 Simply click on Run in your IDE
👉 Tap the Generate PDF Button in the UI → A sample PDF file will be saved to your device’s storage, which can be accessed through File Explorer on the device.

Screenshot 2025-08-12 174652.png

Our sample iText Android application

Resources

➡️ DemoiText.zip - Zip folder containing the iText Gradle project and the Android application

➡️ sample_01.pdf - A sample generated PDF

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.