This video shows the steps to create and read a PDF file in an Android 11 App. It uses iTextpdf library for reading the PDF file and PdfDocument API from android.graphics.pdf to create the PDF file.
It creates and reads from the same PDF file but the concept shown in this video can be used for any PDF file.
The PDF file generated in this video is in “Download” folder but any folder to which the App has access can be used to create and access the file.
Read and Write to external storage permission is sought in this App by defining the permissions in the respective Manifest file and Java code.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Source Code:
package com.programmerworld.createandreadpdf;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView textView;
private String stringFilePath = Environment.getExternalStorageDirectory().getPath() + "/Download/ProgrammerWorld.pdf";
private File file = new File(stringFilePath);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
}
public void buttonCreatePDF(View view){
PdfDocument pdfDocument = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600, 1).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
Paint paint = new Paint();
String stringPDF = editText.getText().toString();
int x = 10, y = 25;
for (String line:stringPDF.split("\n")){
page.getCanvas().drawText(line,x,y, paint);
y+=paint.descent()-paint.ascent();
}
pdfDocument.finishPage(page);
try {
pdfDocument.writeTo(new FileOutputStream(file));
}
catch (Exception e){
e.printStackTrace();
textView.setText("Error in Creating");
}
pdfDocument.close();
}
public void buttonReadPDF(View view){
try {
PdfReader pdfReader = new PdfReader(file.getPath());
String stringParse = PdfTextExtractor.getTextFromPage(pdfReader,1).trim();
pdfReader.close();
textView.setText(stringParse);
}
catch (Exception e){
e.printStackTrace();
textView.setText("Error in Reading");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.createandreadpdf">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_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/Theme.CreateAndReadPDF">
<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>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="136dp"
android:layout_height="37dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.465"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.425" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="91dp"
android:layout_marginTop="33dp"
android:ems="10"
android:hint="@string/enter_your_text_here"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="128dp"
android:layout_marginTop="36dp"
android:onClick="buttonCreatePDF"
android:text="@string/create_pdf"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="132dp"
android:layout_marginTop="32dp"
android:onClick="buttonReadPDF"
android:text="@string/read_pdf"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.programmerworld.createandreadpdf"
minSdkVersion 30
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.itextpdf:itextg:5.5.10'
}
bro isse pdf file save nahi ho rha
What is the error or exception it is throwing? Can you please share a bit more details.
Cheers
Programmer World
–
” open failed: EACCES (Permission denied) ”
I have this problem, knowing that the permission to access the files has been approved, and this problem only appears in the version of Android 11