Create PDF in Android App Using PDF Jet library

PDFjet is a library which allows to write information and make complex designs as we want in Android Application. At times we may require to generate PDF in Android application, for that we can use the PDFjet Library. There are several advantages for using this library.

PDFjet offers great flexibility, attractive performance and many useful features.

PDFjet is very easy to use and consistent with a fully object oriented API.

The main questions that arises are,

How we can use the library in Android app development ?

Which classes we should use for a particular type of design ?

Which components will be useful if the user wants to add Image, Text, Padding, Margin ?

What if the user wants some text in a Paragraph ?

Likewise, there will be many such scenarios and questions. So, for all those scenarios I will be highlighting some components which you can use according to your android app development requirement.

First things First : You may think if I want to add any component to my page then from where should I start writing?, or What code should I use so that this component takes this position and other components at other positions? and the other main point is how we can add the component or draw it on Page?

For that we need to add in every Component one method i.e. setPosition(x Position, y Position). This method allows you to write any component at any Position we want. And for drawing it we first need to make PDF object, in that we need to add Page object. This page can be used in all component to draw.

Something like below,

1

2

PDF pdfDoc=newPDF(fos);

Page page1=newPage(pdfDoc,Letter.PORTRAIT);

So now for any component we just need to call drawOn() method which is showed in all the components listed below.

Main Components of PDFjet library

1) Font

To apply text size and text font you need to first declare a font and then this font can be used in all the components which you make for the pdf generation.

1

2

Font f1=newFont(pdfDoc,CoreFont.HELVETICA);

f1.setSize(8);

2) Text

For any pdf generation we require some information to write to it and for that we require TEXT. Now for adding text to pdf, this library provides three Major Components.

TextBox

TextLine

CompositeTextLine

2.a) TextBox

With Textbox you can set required Text, or any String that you want. Also, you can make a Box type design and apply borders to it so that it acquires some area, and in that you can perform relevant things. It also allows you to change the color of Text and also of the Border. The main advantage of this is it allows auto wrapping of text.

1

2

3

4

5

6

7

8

TextBox m_textbox=newTextBox(f1);

m_textbox.setText(“Desired Text”);

m_textbox.setLocation(xPosition,yPosition);

m_textbox.setFgColor(Color.black);

m_textbox.setHeight(75);

m_textbox.setWidth(560);

m_textbox.setMargin(5);

m_textbox.drawOn(page1);

2.b) TextLine

It is simply used to write a text in One line. But here you can’t wrap the text. But you can set the color of text and one main advantage of using this is it provides SuperScript and SubScript Effect to any text.

1

2

3

4

5

TextLine textLine=newTextLine(f2);

textLine.setText(“Desired Text”);

textLine.setColor(Color.black);

textLine.setLocation(xPosition,yPosition);

textLine.setTextEffect(Effect.SUPERSCRIPT);

2.c) CompositeTextLine

It is used to add multiple textLines in one component. If we have more than one text line and we want to add them one after the other than CompositeTextLine is the best option.

1

2

3

4

CompositeTextLine com=newCompositeTextLine(xPosition,yPosition);

com.addComponent(textLine);

com.setSuperscriptPosition();

com.drawOn(page1);

3) Box

If any user requires to design an area in which he/she can write or draw any other component as well as need to fill that area with some specific color than he/she can use Box component. And for that we need to add the size of the box so one can use setSize() method.

1

2

3

4

5

6

7

Box m_box=newBox();

m_box.setPosition(xPosition,yPosition);

m_box.setSize(560,20);

m_box.setColor(Color.black);

m_box.setFillShape(true);

m_box.setLineWidth(2);

m_box.drawOn(page1);

4) Table

One of the main component which is supported by PDFjet is Table. Here we can add ArrayList so that in that list we can add Cell to write data to that cell, for any number of rows we want.

1

2

3

4

5

6

7

8

9

10

11

Table table=newTable();

List<List<Cell>>m_list=newArrayList<List<Cell>>();

List<Cell>m_cellList=newArrayList<Cell>();

Cell m_cell=newCell(f1);

m_cell.setTextAlignment(Align.LEFT);

m_cell.setWidth(560);

m_cell.setNoBorders();

m_cell.setFgColor(Color.black);

m_cell.setBottomPadding(3);

m_cell.setTopPadding(4);

Now you just need to add the cellList in the main ArrayList.

1

2

m_cellList.add(m_cell);

m_list.add(m_cellList);

And add this ArrayList in the Table.

1

2

3

table.setData(m_list);

table.setLocation(23,p_point);

table.drawOn(page1);

5) Image

To add Image to the pdf document it is quite an easy task. You just need to add the Image component and add the images from the assets folder of your project or any other folder where you have stored.

1

2

3

4

5

Image image;

image=newImage(PdfDocumentObject,”Path of Image”,ImageType.PNG);

image.setPosition(xPosition,yPosition);

image.scaleBy(0.6);

image.drawOn(page1);

How to get this Library ?

PdfJet team provides free trial of the jar so that user can check the things they want and then they need to purchase the License version. The reference link for the same is : http://pdfjet.com/

So in the end I would like to conclude that from android application to generate PDF is somewhat a challenging task, and also there are not many references available on the net from which we could get help. So I am sure this article might help you in making complex designs and make PDF as you desire. 🙂

Android Team @ WeblineIndia

We have developed lots of Android apps for various domains like – Business, Social, Medical, News, Sports, Education, etc. We also have expertise in custom SDK development.

Following well defined standard development processes, coding standards, tools and techniques, it has always helped us to provide best mobile apps to our clients. Our main focus has always been to exceed client expectations and deliver easy to use technically strong apps. On the side, we also love contributing back to the community.