如何使用PDFBox获取当前页码

问题描述 投票:0回答:1

当用户正在阅读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);
    }
java netbeans pdfbox
1个回答
0
投票
  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())
  }
© www.soinside.com 2019 - 2024. All rights reserved.