Last active
January 10, 2020 12:25
-
-
Save mhd-zulqarnain/ee99c42c4d35dcdcaffd610507f75402 to your computer and use it in GitHub Desktop.
Pdf generation itext android containing watermark ,images and table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//..fragement stuff and import | |
/// dependency itextg implementation 'com.itextpdf:itextg:5.5.10' | |
fun generatePdf() { | |
val id = System.currentTimeMillis() | |
val dest = Utils.getPath(activity!!) + id + ".pdf" | |
if (File(dest).exists()) { | |
File(dest).delete() | |
} | |
try { | |
val document = Document() | |
// Location to save | |
val pdfWriter = PdfWriter.getInstance(document, FileOutputStream(dest)) | |
//to add watermark add pdfeventlistener | |
dfWriter.setPageEvent(PDFEventListener()) | |
// Open to write | |
document.open() | |
// Document Settings | |
val pagesize = Rectangle(PageSize.A4.getWidth(), PageSize.A4.getHeight()) | |
document.pageSize = pagesize | |
document.setPageCount(2) | |
document.addCreationDate() | |
document.addAuthor(resources.getString(R.string.app_name)) | |
document.addCreator(resources.getString(R.string.app_name)) | |
val mValueFontSize = 12.0f | |
/** | |
* FONT.... | |
*/ | |
val urName = BaseFont.createFont( | |
"assets/fonts/open_sans.ttf", | |
"UTF-8", BaseFont.EMBEDDED | |
) | |
val fontGn = Font(urName, mValueFontSize, Font.NORMAL, BaseColor.BLACK) | |
val fontval = Font(urName, mValueFontSize, Font.NORMAL, BaseColor.GRAY) | |
// LINE SEPARATOR | |
val lineSeparator = LineSeparator() | |
lineSeparator.lineColor = BaseColor(0, 0, 0, 40) | |
//<editor-fold desc="title image and text"> | |
// Adding image and title.... | |
val stream = ByteArrayOutputStream() | |
val titletable = PdfPTable(2) | |
titletable.setWidthPercentage(100F) | |
titletable.setWidths(intArrayOf(1, 3)) | |
try { | |
val ims = activity!!.getAssets().open("assetlogo.png") | |
val bmp = BitmapFactory.decodeStream(ims) | |
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream) | |
val image = Image.getInstance(stream.toByteArray()) | |
val ic = PdfPCell(image, true) | |
ic.horizontalAlignment = Element.ALIGN_LEFT | |
ic.setBorder(Rectangle.NO_BORDER) | |
titletable.addCell(ic) | |
} catch (e: BadElementException) { | |
e.printStackTrace() | |
} catch (e: IOException) { | |
e.printStackTrace() | |
} | |
val titleFont = Font(urName, 18.0f, Font.BOLD, BaseColor.BLACK) | |
val titleChunk = Chunk("REGAL AUTOMOBILE INDUSTRIES LTD.", titleFont) | |
val titilePara = Paragraph(titleChunk) | |
titilePara.alignment = Element.ALIGN_CENTER | |
val cell = PdfPCell() | |
val desFont = Font(urName, 15.0f, Font.NORMAL, BaseColor.BLACK) | |
val titleDes = Chunk("Provisional Sales Order", desFont) | |
val titleDesPara = Paragraph(titleDes) | |
titleDesPara.alignment = Element.ALIGN_CENTER | |
cell.addElement(titilePara) | |
cell.addElement(titleDesPara) | |
cell.setVerticalAlignment(Element.ALIGN_TOP) | |
cell.setBorder(Rectangle.NO_BORDER) | |
titletable.addCell(cell) | |
document.add(titletable) | |
//</editor-fold> | |
document.add(Paragraph(" ")) | |
val tbnam = PdfPTable(2) | |
tbnam.setWidthPercentage(100F) | |
tbnam.setWidths(intArrayOf(1, 2)) | |
val bkfnm = getParagraphCell(getString(R.string.full_name), true) | |
val bkfnmv = getParagraphCellValue(order._fullname.value, true) | |
tbnam.addCell(bkfnm) | |
tbnam.addCell(bkfnmv) | |
document.add(tbnam) | |
// document.add(Paragraph(" ")) | |
val tbfan = PdfPTable(2) | |
tbfan.setWidthPercentage(100F) | |
tbfan.setWidths(intArrayOf(1, 2)) | |
val bkfan = getParagraphCell(getString(R.string.father_name), true) | |
val bkfanV = getParagraphCellValue(order._fathername.value, true) | |
tbfan.addCell(bkfan) | |
tbfan.addCell(bkfanV) | |
document.add(tbfan) | |
document.add(Paragraph(" ")) | |
/*stemp*/ | |
val ttst = Chunk(toTitleCase(getString(R.string.applicant_thumb)), tfFont) | |
val ttParast = Paragraph(ttst) | |
document.add(ttParast) | |
document.add(Paragraph(" ")) | |
val ttermst = PdfPTable(2) | |
ttermst.setWidthPercentage(100F) | |
ttermst.setWidths(intArrayOf(2, 2)) | |
val bkblnk = getParagraphCell("", true) | |
val tcellstm = PdfPCell() | |
val ttChunkstm = Chunk(getString(R.string.three_line), fontval) | |
val pst = Paragraph(ttChunkstm) | |
tcellstm.addElement(pst) | |
ttermst.addCell(tcellstm) | |
ttermst.addCell(bkblnk) | |
document.add(ttermst) | |
//end | |
document.close() | |
///pdf generated use path to preview or share | |
} catch (ie: IOException) { | |
Toast.makeText( | |
activity!!, | |
"App doesn't have permission", | |
Toast.LENGTH_SHORT | |
).show() | |
} catch (ie: DocumentException) { | |
} catch (ae: ActivityNotFoundException) { | |
Toast.makeText( | |
activity!!, | |
"No application found to open this file.", | |
Toast.LENGTH_SHORT | |
).show() | |
} | |
} | |
/* | |
* utils for pdf generation | |
* */ | |
fun getParagraphCell(str: String, toTitleCase: Boolean): PdfPCell { | |
var cp = "" | |
if (!toTitleCase) { | |
cp = str | |
} else | |
cp = toTitleCase(str)!! | |
val cell = PdfPCell() | |
val titleChunk = Chunk(cp, fontGn) | |
val p = Paragraph(titleChunk) | |
p.setAlignment(Element.ALIGN_LEFT) | |
cell.addElement(p) | |
cell.setVerticalAlignment(Element.ALIGN_BOTTOM) | |
cell.setBorder(Rectangle.NO_BORDER) | |
return cell | |
} | |
fun getParagraphCellValue(str: String?, flag: Boolean): PdfPCell { | |
var cp = "" | |
if (!flag && str != null) { | |
cp = toTitleCase(str)!! | |
} else | |
if (str != null) { | |
cp = str | |
} | |
val cell = PdfPCell() | |
val titleChunk = Chunk(cp, fontval) | |
val p = Paragraph(titleChunk) | |
p.setAlignment(Element.ALIGN_LEFT) | |
cell.addElement(p) | |
cell.setVerticalAlignment(Element.ALIGN_BOTTOM) | |
return cell | |
} | |
fun getFormattedDate(date: String): String { | |
var tmp = "" | |
if (date.isNotEmpty()) { | |
tmp = date.split("T")[0] | |
} | |
return tmp | |
} | |
fun toTitleCase(str: String?): String? { | |
if (str == null) { | |
return null | |
} | |
var space = true | |
val builder = StringBuilder(str) | |
val len = builder.length | |
for (i in 0 until len) { | |
val c = builder[i] | |
if (space) { | |
if (!Character.isWhitespace(c)) { | |
// Convert to title case and switch out of whitespace mode. | |
builder.setCharAt(i, Character.toTitleCase(c)) | |
space = false | |
} | |
} else if (Character.isWhitespace(c)) { | |
space = true | |
} else { | |
builder.setCharAt(i, Character.toLowerCase(c)) | |
} | |
} | |
return builder.toString() | |
} | |
/* | |
* PDFEventListener class | |
* */ | |
class PDFEventListener : PdfPageEventHelper() { | |
override fun onEndPage( | |
writer: PdfWriter, | |
document: Document | |
) { | |
val urName = BaseFont.createFont( | |
"assets/fonts/open_sans.ttf", | |
"UTF-8", BaseFont.EMBEDDED | |
) | |
val mValueFontSize = 72.0f | |
val font =Font(urName, mValueFontSize, Font.NORMAL, BaseColor.LIGHT_GRAY) | |
val canvas = writer.directContentUnder | |
val watermark = Phrase( | |
"P R E V I E W",font | |
) | |
ColumnText.showTextAligned( | |
canvas, | |
Element.ALIGN_CENTER, | |
watermark, | |
337f, | |
500f, | |
45f | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment