在PDFBox中获取选定的文本位置

时间:2016-12-18 17:02:58

标签: pdfbox

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

  1. 我想在用户阅读pdf书时获得选定的文本位置 并选择特殊字词。
  2. 之后我想在位置提取上创建一个注释 第一步。
  3. 第二个问题是由我完成的。代码是:

    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中选定单词的当前位置? 提前谢谢。

0 个答案:

没有答案
相关问题