How make Text to PDF Converter App with PDF Viewer

In this video tutorial, I have shown how can we make a "Text to PDF Converter App with PDF Viewer using ITEXT and BARTEC libraries.  Watch the youtube video:





The source code for the entire project is given below.

Code snippet for MainActivity.Java

package com.learnandroid.texttopdf;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
    EditText name, email;
    Button btnPdf, btnView;
    PdfWriter writer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (EditText) findViewById(R.id.name);
        email  = (EditText) findViewById(R.id.email);
        btnPdf = (Button) findViewById(R.id.btnpdf);
        btnView = (Button) findViewById(R.id.btnview);
        btnPdf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = name.getText().toString();
                String useremail = email.getText().toString();
                String path = getExternalFilesDir(null).toString()+"/user.pdf";
                File file = new File(path);
                if(!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

               
                Document document = new Document(PageSize.A4);
                try {
                    writer = PdfWriter.getInstance(document, new FileOutputStream(file.getAbsoluteFile()));
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                document.open();


          Font myfont = new Font(Font.FontFamily.HELVETICA, 24, Font.BOLD);
                paragraph.add(new Paragraph("User Name: "+username, myfont));
                paragraph.add(new Paragraph("\n"));
                paragraph.add(new Paragraph("User Email: "+useremail, myfont));
                try {
                    document.add(paragraph);
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
           
                document.close();

                Toast.makeText(MainActivity.this, "PDF Created Successfully", Toast.LENGTH_SHORT).show();
                Intent intent  = new Intent(getApplicationContext(), NewActivity.class);
                startActivity(intent);
            }
        });
    }
}

Code snippet for activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:hint="Write your name here"
        />
    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:hint="Write your email here"
        android:layout_below="@+id/name"
        />
    <Button
        android:id="@+id/btnpdf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Convert to PDF"
        android:layout_marginTop="50dp"
        android:layout_below="@+id/email"/>

    <Button
        android:id="@+id/btnview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="View PDF"
        android:layout_marginTop="50dp"
        android:layout_below="@+id/btnpdf"/>
</RelativeLayout>


Code snippet for NewActivity.Java

package com.learnandroid.texttopdf;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.File;
public class NewActivity extends AppCompatActivity {
    PDFView pdfView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        pdfView = (PDFView) findViewById(R.id.view);
        String path = getExternalFilesDir(null).toString()+"/user.pdf";
        File file  = new File(path);
        pdfView.fromFile(file)
                .swipeHorizontal(false)
                .enableDoubletap(true)
                .enableAnnotationRendering(false)
                .defaultPage(0)
                .password(null)
                .scrollHandle(null)
                .load();
    }
}


Code snippet for activity_new.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".NewActivity">
    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

Subscribe Our YouTube Channel for More Videos and source codes.




Comments

  1. Youtube - Watch This Video On YouTube
    youtube · youtube · youtube to mp3 download youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube · youtube.

    ReplyDelete
  2. I will share it with my other friends as the information is really very useful. add page numbers to pdf file online free. Keep sharing your excellent work.

    ReplyDelete
  3. Thanks for your post. It's very helpful post for us. You can also visit Convert Your Text To Pdf Online for more Victor Steel related information. I would like to thanks for sharing this article here.

    ReplyDelete

Post a Comment