哪种java API最适合将pdf转换为图像

时间:2014-03-07 05:27:37

标签: scala pdf-reader

我尝试使用3个Java API for pdf,但所有3个都无法正常工作。 1. PDFFile 2. PDDocument 3. PDFDocumentReader

如果我有一个带有2层的pdf,其中上层有点透明,那么当上面3个API将其转换为图像时,只有上层进入图像而没有透明度。但这两层都必须来。

所以建议我使用其他API来满足我的要求

PDF文件代码:

val raf = new RandomAccessFile(file, "r")
  val channel = raf.getChannel()
  val buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())
  raf.close()
  val pdffile = new PDFFile(buf)
  val numPgs = pdffile.getNumPages() + 1
  for (i <- 1 until numPgs) {

    val page = pdffile.getPage(i)
    val pwdt = page.getBBox().getWidth().toDouble
    val phgt = page.getBBox().getHeight().toDouble
    val rect = new Rectangle(0, 0, pwdt.toInt, phgt.toInt)
    val rsiz = resize(method, size, pwdt, phgt)
    val img = page.getImage(rsiz("width"), rsiz("height"),

      rect, null, true, true)
    result ::= buffer(img)

PDDocument代码:

val doc = PDDocument.load(new FileInputStream(file));
val pages = doc.getDocumentCatalog().getAllPages()
for (i <- 0 until pages.size()) {
      val page = pages.get(i)
      val before = page.asInstanceOf[PDPage].convertToImage()
}

PDFDocumentReader的代码:

val inputStream = new FileInputStream(file)
        val document = new PDFDocumentReader(inputStream)
        val numPgs = document.getNumberOfPages

        for (i <- 0 until numPgs) {
          val pageDetail = new PageDetail("", "", i, "")
          val resourceDetails = document.getPageAsImage(pageDetail)
          val image = ImageIO.read(new ByteArrayInputStream(resourceDetails.getBytes()))
          result ::= image
}

0 个答案:

没有答案