How to create and read a PDF file in your Android 11 App?

Опубликовано: 30 Октябрь 2021
на канале: Programmer World
6,729
64

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: [email protected]

Complete source code and other details can be found in the below link:
https://programmerworld.co/android/ho...


However, the main Java code is copied below also for reference:

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");
}
}
}