How to convert image to pdf in android

In this tutorial we will create an application which will help you to convert an image to a pdf file. There is no widget in android in which you can load the pdf file. For doing that you need to either have a pdf viewer application or you need to load it in a drive using webview. So in this example we learn how to convert image to pdf and how to display a pdf file in an android application.

The demo of this application is available at PDF Scanner for android

Steps

  1. Create new project in android studio
  2. Compile the itext pdf library
  3. Implement the code for converting image to pdf
  4. Implement the code for viewing pdf in android application

Create project in android studio

Refer Android beginners app development guide if you are beginner or if you don’t know how to create project in android studio.

Compile the itext pdf library

There are many ways to convert image to pdf in android. But for avoiding  complexity of the code  we will use a library which will help the further task easier. For bringing itext library in your project you need to add the following dependency to your build.gradle file.

compile 'com.itextpdf:itextg:5.5.10'

Implement the code for converting image to pdf

In this step we will implement the code for converting image to pdf. Here we are considering that image is already in your sd card. If the image which has to be converted has to be clicked using camera please refer how to save image using camera in android. Now add the following function in your activity and you can call this function on the action like a button click or something to create the pdf for the image stored in specified location

public void CreatePdf(){
    Random r = new Random();
    int i1 = r.nextInt(10000 - 5) + 5;
    String name = "codesfor"+i1+".pdf";
    Document doc = new Document();
//path of the pdf file which will get saved
    File exportDir = new File(Environment.getExternalStorageDirectory(), "/codesforscanner/"+name);
    try {
        PdfWriter.getInstance(doc, new FileOutputStream(exportDir));
        doc.open();
//image file path which needs to be converted
        String filename = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"codesforscanner/codesfor.jpg";
        Image image = Image.getInstance(filename);
        int indentation = 0;
        float scaler = ((doc.getPageSize().getWidth() - doc.leftMargin()
                - doc.rightMargin() - indentation) / image.getWidth()) * 100;

        image.scalePercent(scaler);
        doc.add(image);
        System.setProperty("http.agent", "Chrome");

        Intent intent = new Intent(this,PdfViewer.class);
        intent.putExtra("name",name);
        startActivity(intent);
        finish();

    } catch (DocumentException | IOException e) {
        e.printStackTrace();
    } finally {
        doc.close();
    }
}

Implement the code for viewing pdf in android application

After the completion of above step a pdf file will be located on the path which you specified. Now we need to open it in our application. By default android is not having any widget in which we can open pdf document. So  here we will use one library which will provide one custom widget for opening pdf document in android application. So first you need to add the following dependency to your build.gradle file

compile 'com.github.barteksc:android-pdf-viewer:2.0.3'

Now add the following widget to your activity’s layout file

<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Now you just need to use the following code to display the pdf file in your activity.

pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromFile(exportDir)
// .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
.pages(0) // all pages are displayed by default
.enableSwipe(true) // allows to block changing pages using swipe
.swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
.password(null)
.scrollHandle(null)
.load();

Now run the application . You will be able to convert the image to pdf and will be able to display the pdf inside the activity of android application . You can download the sample application of this tutorial at PDF Scanner for android

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *