0

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

如果我有一个 pdf 有 2 层,其中上层有点透明,那么当以上 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
}
4

0 回答 0