Last active
February 20, 2021 13:47
-
-
Save VincentJoshuaET/1b323958f46d1b8b3f1f945347764e34 to your computer and use it in GitHub Desktop.
Kotlin extension functions and properties for iTextPDF
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
import com.itextpdf.io.image.ImageDataFactory | |
import com.itextpdf.kernel.colors.Color | |
import com.itextpdf.layout.Document | |
import com.itextpdf.layout.ElementPropertyContainer | |
import com.itextpdf.layout.IPropertyContainer | |
import com.itextpdf.layout.element.* | |
import com.itextpdf.layout.property.Property | |
import com.itextpdf.layout.property.TransparentColor | |
import com.itextpdf.layout.property.UnitValue | |
import java.io.ByteArrayOutputStream | |
inline fun Table.addCell(rowSpan: Int = 1, colSpan: Int = 1, block: Cell.() -> Unit = {}) { | |
addCell(Cell(rowSpan, colSpan).apply(block)) | |
} | |
inline fun Table.addParagraph(title: String?, block: Paragraph.() -> Unit) { | |
val paragraph = title?.let(::Paragraph) ?: Paragraph() | |
addCell(paragraph.apply(block)) | |
} | |
inline var <T : IPropertyContainer> ElementPropertyContainer<T>.fontColor: Color? | |
get() = getProperty<TransparentColor?>(Property.FONT_COLOR)?.color | |
set(value) { | |
setFontColor(value) | |
} | |
inline var <T : IElement> BlockElement<T>.padding: Float | |
get() = getProperty<UnitValue>(Property.PADDING_TOP).value | |
set(value) { | |
setPadding(value) | |
} | |
inline var <T : IElement> BlockElement<T>.bold: Boolean | |
get() = getProperty(Property.BOLD_SIMULATION) | |
set(value) { | |
if (value) setBold() | |
} | |
inline var Table.useAllAvailableWidth: Boolean | |
get() { | |
val value = getProperty<UnitValue>(Property.WIDTH) | |
return value.isPercentValue && value.value == 100F | |
} | |
set(value) { | |
if (value) useAllAvailableWidth() | |
} | |
inline var Table.fixedLayout: Boolean | |
get() = getProperty<String>(Property.TABLE_LAYOUT) == "fixed" | |
set(value) { | |
if (value) setFixedLayout() | |
} | |
inline var <T : IElement> BlockElement<T>.fontSize: Float | |
get() = getProperty<UnitValue>(Property.FONT_SIZE).value | |
set(value) { | |
setFontSize(value) | |
} | |
inline fun Cell.addParagraph(title: String, block: Paragraph.() -> Unit) { | |
add(Paragraph(title).apply(block)) | |
} | |
fun Cell.addImage(stream: ByteArrayOutputStream) { | |
add(Image(ImageDataFactory.create(stream.toByteArray())).setAutoScale(true)) | |
} | |
inline fun Document.addParagraph(title: String, block: Paragraph.() -> Unit) { | |
add(Paragraph(title).apply(block)) | |
} | |
inline fun table(columns: Int, block: Table.() -> Unit): Table { | |
return Table(UnitValue.createPercentArray(columns)).apply(block) | |
} | |
inline fun paragraph(title: String, block: Paragraph.() -> Unit): Paragraph { | |
return Paragraph(title).apply(block) | |
} | |
operator fun Document.plusAssign(image: Image) { | |
add(image) | |
} | |
operator fun Document.plusAssign(areaBreak: AreaBreak) { | |
add(areaBreak) | |
} | |
operator fun Document.plusAssign(element: IBlockElement) { | |
add(element) | |
} | |
Usage: | |
document.addParagraph(section.title) { | |
bold = true | |
setUnderline(2F, -2F) | |
fontSize = 18F | |
} | |
val table = table(2) { | |
useAllAvailableWidth = true | |
fixedLayout = true | |
bold = true | |
fontSize = 10F | |
} | |
table.addParagraph(field.alias) { | |
fontColor = ColorConstants.GRAY | |
padding = 2F | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment