How to create PDF file in your Android App? Complete source code using Android Studio

This video shows the steps to create a PDF file for various contents such as Texts and Strings in your Android App. This video shows the steps using the Android Studio Java code.

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

Source Code:

package com.example.createpdf;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

private EditText myEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myEditText = findViewById(R.id.editText);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
}

public void createMyPDF(View view){

PdfDocument myPdfDocument = new PdfDocument();
PdfDocument.PageInfo myPageInfo = new PdfDocument.PageInfo.Builder(300,600,1).create();
PdfDocument.Page myPage = myPdfDocument.startPage(myPageInfo);

Paint myPaint = new Paint();
String myString = myEditText.getText().toString();
int x = 10, y=25;

for (String line:myString.split(“\n”)){
myPage.getCanvas().drawText(line, x, y, myPaint);
y+=myPaint.descent()-myPaint.ascent();
}

myPdfDocument.finishPage(myPage);

String myFilePath = Environment.getExternalStorageDirectory().getPath() + “/myPDFFile.pdf”;
File myFile = new File(myFilePath);
try {
myPdfDocument.writeTo(new FileOutputStream(myFile));
}
catch (Exception e){
e.printStackTrace();
myEditText.setText(“ERROR”);
}

myPdfDocument.close();
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.createpdf”>

<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE”/>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

Top posts/ comments from YouTube channel:

It is possible to put images from camera on pdf file too?

You should refer to my below video in this regard:

https://youtu.be/sOKV3iHl5aM

I hope this video is helpful.

5 comments

  1. sir,
    I follow exactly your code but it comes out ERROR. I do not know where it went wrong.
    Would appreciate for any assisance.

    1. Whatever code we have, it is shared on this portal or the YouTube video. We do not have any other backups of the project files with us. Hope you understand.

      Cheers
      Programmer World

Leave a Reply

%d bloggers like this: