Exporting Data as PDF in Android. How to use the iText Android library | by Annas Surdyanto | Feb, 2022

Learn how to use the iText Android library

Picture by CURVD® on Unsplash

One of many PDF libraries that’s developer-friendly to implement is the iText library. To begin utilizing this library, we have to implement the iText dependency in our app Gradle.

implementation ("com.itextpdf:itextpdf:5.5.13.1")
<manifest
...
<uses-permission android:title="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:title="android.permission.READ_EXTERNAL_STORAGE"/>
android:requestLegacyExternalStorage="true"

To begin, let’s create a PDF service class in order that our UI class is just not contaminated with our PDF configuration. On this class, we will straight outline our font fashion.

PDF Service Class Introduction
PDF File and Doc Initialization

Writing a doc is definitely much like typing straight right into a Doc file in Microsoft Workplace. What makes it totally different is simply now we have no cursor and we have to management it programmatically.

doc.add(Paragraph(" "))
personal enjoyable createParagraph(content material: String): Paragraph
val paragraph = Paragraph(content material, BODY_FONT)
paragraph.firstLineIndent = 25f
paragraph.alignment = Component.ALIGN_JUSTIFIED
return paragraph
doc.add(createParagraph(“PARAGRAPH_CONTENT”))
doc.newPage()
personal enjoyable createTable(column: Int, columnWidth: FloatArray): PdfPTable 
val desk = PdfPTable(column)
desk.widthPercentage = 100f
desk.setWidths(columnWidth)
desk.headerRows = 1
desk.defaultCell.verticalAlignment = Component.ALIGN_CENTER
desk.defaultCell.horizontalAlignment = Component.ALIGN_CENTER
return desk
personal enjoyable createCell(content material: String): PdfPCell 
val cell = PdfPCell(Phrase(content material))
cell.horizontalAlignment = Component.ALIGN_CENTER
cell.verticalAlignment = Component.ALIGN_MIDDLE
//setup padding
cell.setPadding(8f)
cell.isUseAscender = true
cell.paddingLeft = 4f
cell.paddingRight = 4f
cell.paddingTop = 8f
cell.paddingBottom = 8f
return cell

We’re going to have a paper consisting of two paragraphs and our person knowledge having 4 variables (id, first title, center title, final title).

knowledge class Consumer(
val id: Int,
val firstName: String,
val middleName: String,
val lastName: String
)
val userData = listOf(
Consumer(1, "Amir", "Tan", "Khan"),
Consumer(2, "Michael", "Calvin", "Gonzalez"),
Consumer(3, "Alim", "Bin", "Hasan"),
Consumer(4, "John", "Carl", "Tean"),
Consumer(5, "James", "Brown", "Chilly"),
Consumer(6, "Virats", "Kader", "Can"),
Consumer(7, "Lim", "Lui", "Pao"),
Consumer(8, "Endro", "Tava", "Pero"),
Consumer(9, "Dani", "Pedro", "Leo"),
Consumer(10, "Leonardo", "Chris", "Luiza")
)
Create PDF Operate Pattern
Calling Create PDF Operate in Exercise
End result Preview

Exporting knowledge into PDF is a cool function that’s really easy to implement so long as we all know the underlying idea of how an iText doc provides a component.

More Posts