Using JPedal in Other JVM Languages
It’s very easy to use JPedal in other JVM languages.
When setting up JPedal, follow the document on “How to Set Up”. If you can’t find relevant documents, consider directly setting up JPedal as the project library.
Below are two examples for using JPedal in Kotlin, Scala, and Groovy.
Converting PDF to BMP
Kotlin with Gradle
package org.example
import com.idrsolutions.image.JDeli
import com.idrsolutions.image.bmp.options.BmpEncoderOptions
import org.jpedal.examples.images.ConvertPagesToImages
import org.jpedal.exception.PdfException
import java.io.File
import java.io.IOException
fun main() {
val convert = ConvertPagesToImages("inputFile.pdf")
try {
if (convert.openPDFFile()) {
for (page in 1..convert.pageCount) {
val bi = convert.getPageAsImage(page)
val out = File("outputFolder$page.bmp")
// Setters to control output
val options = BmpEncoderOptions()
JDeli.write(bi, options, out)
}
}
} catch (e: PdfException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
} finally {
convert.closePDFfile()
}
}
Groovy with Maven
import com.idrsolutions.image.JDeli
import com.idrsolutions.image.bmp.options.BmpEncoderOptions
import org.jpedal.examples.images.ConvertPagesToImages
import org.jpedal.exception.PdfException
static void main(String[] args) {
def convert = new ConvertPagesToImages("inputFile.pdf")
try {
if (convert.openPDFFile()) {
for (page in 1..convert.pageCount) {
def bi = convert.getPageAsImage(page)
def out = new File("outputFolder${page}.bmp")
// Setters to control output
def options = new BmpEncoderOptions()
JDeli.write(bi, options, out)
}
}
} catch (PdfException | IOException e) {
e.printStackTrace()
} catch (Exception e) {
e.printStackTrace()
} finally {
convert.closePDFfile()
}
}
Scala with sbt
import com.idrsolutions.image.JDeli
import com.idrsolutions.image.bmp.options.BmpEncoderOptions
import org.jpedal.examples.images.ConvertPagesToImages
import org.jpedal.exception.PdfException
import java.awt.image.BufferedImage
import java.io.{File, IOException}
object Main {
def main(args: Array[String]): Unit = {
val convert = new ConvertPagesToImages("inputFile.pdf")
try {
if (convert.openPDFFile()) {
for (page <- 1 to convert.getPageCount) {
val bi: BufferedImage = convert.getPageAsImage(page)
val out = new File(s"outputFolder$page.bmp")
// Setters to control output
val options = new BmpEncoderOptions()
JDeli.write(bi, options, out)
}
}
} catch {
case e: PdfException => e.printStackTrace()
case e: IOException => e.printStackTrace()
case e: Exception => e.printStackTrace()
} finally {
convert.closePDFfile()
}
}
}
Splitting pages
Kotlin with Gradle
package org.example
import org.jpedal.manipulator.PdfManipulator
import java.io.File
fun main() {
val inputFile = File("inputFile.pdf")
val outputFolder = File("outputFolder")
val pageToSplitAt = 3 // example to split after page 3
PdfManipulator.splitInHalf(inputFile, outputFolder, pageToSplitAt)
}
Groovy with Maven
import org.jpedal.manipulator.PdfManipulator
static void main(String[] args) {
def pageToSplitAt = 3 // example to split after page 3
PdfManipulator.splitInHalf(
new File("inputFile.pdf"),
new File("outputFolder"),
pageToSplitAt
)
}
Scala with sbt
import org.jpedal.manipulator.PdfManipulator
import java.io.{File}
object Main {
def main(args: Array[String]): Unit = {
val pageToSplitAt = 3 // example to split after page 3
PdfManipulator.splitInHalf(
new File("inputFile.pdf"),
new File("outputFolder"),
pageToSplitAt
)
}
}