netbeans 如何使用PDFBox获取当前页码

yhived7q  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(58)

当用户正在阅读PDF并完成阅读此文件并关闭PDF时,我需要保存用户在使用PDFBox库2.0.20关闭PDF之前阅读的最后一页

try {
      InputStream bos = new ByteArrayInputStream(lib.getPdf()); // lib.getPdf returns the pdf file in byte[] array.
      int tamañoPDF= bos.available();
      byte [] datosPDF = new byte [tamañoPDF];
      bos.read(datosPDF, 0 , tamañoPDF);
      OutputStream out = new FileOutputStream (button.getText());
      out.write(datosPDF);
      button.addActionListener(new ActionListener(){ //setting an action to a button.
      public void actionPerformed(ActionEvent ae)
        {try {
            Desktop.getDesktop().open(new File(button.getText())); //the pdf is opened
            out.close(); //here I think I have to insert the code that save the page number
            } catch (IOException ex) {
                            Logger.getLogger(InfoDelLibro.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    }
          });

    } catch (IOException ex) {
         Logger.getLogger(InfoDelLibro.class.getName()).log(Level.SEVERE, null, ex);
    }

字符串

qq24tv8q

qq24tv8q1#

fun extract(document: PDDocument?): String? {
    if (document === null) return null
    val totalPages = document.numberOfPages
    var pdfTextStripper = PDFTextStripper();
    var pageText: String = ""
    var array = JSONArray();
    for (i in 1..totalPages) {
      val rootObject= JSONObject()
      pdfTextStripper.setStartPage(i)
      pdfTextStripper.setEndPage(i)
      pageText = pdfTextStripper.getText(document)
      rootObject.put("pagenumber",(i).toString())
      rootObject.put("pageContent",pageText)
      array.put(rootObject)
    }
    return StringHandler.format((array).toString())
  }

字符串

相关问题