他のJVM言語でJPedalを使用する
他のJVM言語でJPedalを使用することは非常に簡単です。
JPedalをセットアップする際は、「セットアップ方法」のドキュメントに従ってください。関連するドキュメントが見つからない場合は、JPedalをプロジェクトライブラリとして直接セットアップすることを検討してください。
以下は、Kotlin、Scala、GroovyでJPedalを使用する2つの例です。
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()
}
}
}
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
)
}
}
