我正在开发android中的翻译应用程序,我想使用PDFBox来操作pdf文件 . 我最挑剔的一个是:

  • 我希望在用户阅读pdf书籍并选择特殊单词时获得选定的文本位置 .

  • 之后我想在第一步的位置提取上创建一个注释 .

第二个问题是由我完成的 . 代码是:

public static void setHighLightText(Integer i) {
        try {
            PDDocument pdfDocument = PDDocument.load(new File("/home/saeed/Documents/Book/installArch.pdf"));
            PDPage page = (PDPage) pdfDocument.getDocumentCatalog().getPages().get(i);
            List<PDAnnotation> annots = page.getAnnotations();
            PDAnnotationTextMarkup markup = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_STRIKEOUT);
            PDColor yellow = new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE);
            markup.setColor(yellow);
            PDRectangle position = new PDRectangle();
            position.setLowerLeftX(50);
            position.setLowerLeftY(50);
            position.setUpperRightX(150);
            position.setUpperRightY(150);
            markup.setRectangle(position);

            float[] quads = new float[8];

            quads[0] = position.getLowerLeftX(); // x1
            quads[1] = position.getUpperRightY() - 2; // y1
            quads[2] = position.getUpperRightX(); // x2
            quads[3] = quads[1]; // y2
            quads[4] = quads[0]; // x3
            quads[5] = position.getLowerLeftY() - 2; // y3
            quads[6] = quads[2]; // x4
            quads[7] = quads[5]; // y5

            markup.setQuadPoints(quads);
            markup.setSubtype("Highlight");
            markup.setModifiedDate(Calendar.getInstance().getTime().toString());
            markup.setContents("Hi Saeed!");
            annots.add(markup);

            pdfDocument.save("/home/saeed/Documents/Book/installArch.pdf");
            System.out.println("Text Added Successfully!!!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

但我的主要问题是,如何用PDFBox提取pdf中选定单词的当前位置?提前致谢 .