Android Generate PDF From View Convert Activity XML Layout ScrollView

Android Generate PDF From View Tutorial is today’s theme.

You can create the pdf file from any layout of your application programmatically.

In this example, we will generate our pdf file from xml layouts like linearlayout and scrollview.

I have not used any third party library in this tutorial.

At the end of the tutorial, you will be able to generate the PDF document from the current activity.

We will also save the generated pdf file in the internal storage.

Our Generated PDF

Below video showcases our PDF file.

Step 1. Changes in the gradle file

We are going to use PdfDocument class in this example. Now this PdfDocument class supports sdk version greater than 18.

Hence, we have to set the minSdkVersion as greater than 18 in the build.gradle(Module: app) file.

We require two permissions (Read and Write) in this example.

Add above two permissions in the AndroidManifest.xml file.

I have set the targetSdkVersion as 22 here. If you are targeting sdk version greater than 22 then you need to request for runtime permissions.

You can get reference for runtime permissions from this android marshmallow permissions guide.

So the final source code for build.gradle(Module: app) file is like below

Step 2. activity_main.xml and

Now we will create a pdf file from linearlayout.

This linearlayout contains one image, one textview and two buttons.

The height of the linearlayout is not so long that we need to use scrollview here. However, we will also learn how to make a pdf file that covers the whole scrollview later.

Copy the following code in activity_main.xml file

Now add below source code in file

Step 3. Information about Main Activity code

Now let us learn the code of the Main Activity step by step.

Consider below code

two buttons and linearlayout is found here with help of their ids.

llPdf is the linearlayout using which we will create the PDF file.

Let us understand following code

click event of btnScroll will open a new activity. We will create pdf file with scrollview in this activity.

Second is the button click where we will generate the pdf document from linearlayout.

First of all, we will create the bitmap from the height and width of the linearlayout using loadBitmapFromView() method.

Here is the code for the loadBitmapFromView() method.

We need to pass height and width of the linearlayout in the parameter.

This method will create a bitmap using canvas class and will return that bitmap.

After this, compiler will call the createPdf() method, which will finally generate the PDF file.

Code for createPdf() method is as per following lines

First the compiler will set the height and width of the pdf by using WindowManager and DisplayMetrics classes.

PdfDocument class will create a new pdf file object.

Then the compiler will fill this newly generated pdf file using Canvas and Paint classes.

Read the below line

Above line will define a name of the pdf file and a path where this pdf document will be saved.

After generating the pdf file successfully, compiler will open this file using openGeneratedPDF() method.

Learning openGeneratedPDF()

Following is the source code for this method

We need to give a path to the pdf file in the below line

After this, compiler will open a pdf document using Intent as per shown in the source code.

Step 4. Making PDF from Scrollview

Create a new activity named Scroll Activity.

Add below code in activity_scroll.xml

I have taken one button, one imageview and three textview. All these three widgets are the childd of the linearlayout.

Now scrollview is the parent of the linearlayout. We will generate the pdf file using this linearlayout which is the child of the scrollview.

Further, write below codes in the file

I have used the similar method to create the pdf document and to open it.

So you can get reference from step 3 about this thing.

Download Source Code For Android generate pdf from view

[sociallocker]Download Source Code For PDF From View Example[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

2 Responses

  1. milad says:

    your website is incredible and thanks for your tutorials. .I am developer.I would be glad to visit my instagram page(miladrsh96)

Leave a Reply

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