Невозможно извлечь значения из PDF для определенных координат, используя java apache pdfbox - PullRequest
0 голосов
/ 14 марта 2019

Моя задача - извлечь текст из PDF для определенных координат.

Я использовал клиент Apache Pdfbox для извлечения данных.

Чтобы получить координаты x, y, высоты и ширины из PDF, я использую инструмент изменения PDF X, который находится в миллиметрах. Когда я передаю значение в прямоугольнике, значения не получают пустое значение.

public String getTextUsingPositionsUsingPdf(String pdfLocation, int pageNumber, double x, double y, double width,
                double height) throws IOException {
            String extractedText = "";
            // PDDocument Creates an empty PDF document. You need to add at least
            // one page for the document to be valid.
            // Using load method we can load a PDF document
            PDDocument document = null;
            PDPage page = null;
            try {
                if (pdfLocation.endsWith(".pdf")) {
                    document = PDDocument.load(new File(pdfLocation));
                    int getDocumentPageCount = document.getNumberOfPages();
                    System.out.println(getDocumentPageCount);

                    // Get specific page. THe parameter is pageindex which starts with // 0. If we need to
                    // access the first page then // the pageIdex is 0 PDPage
                    if (getDocumentPageCount > 0) {
                        page = document.getPage(pageNumber + 1);
                    } else if (getDocumentPageCount == 0) {
                        page = document.getPage(0);
                    }
                    // To create a rectangle by passing the x axis, y axis, width and height 
                    Rectangle2D rect = new Rectangle2D.Double(x, y, width, height);
                    String regionName = "region1";

                    // Strip the text from PDF using PDFTextStripper Area with the
                    // help of Rectangle and named need to given for the rectangle
                    PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                    stripper.setSortByPosition(true);
                    stripper.addRegion(regionName, rect);
                    stripper.extractRegions(page);
                    System.out.println("Region is " + stripper.getTextForRegion("region1"));
                    extractedText = stripper.getTextForRegion("region1");
                } else {
                    System.out.println("No data return");
                }
            } catch (IOException e) {
                System.out.println("The file  not found" + "");
            } finally {
                document.close();
            }
            // Return the extracted text and this can be used for assertion
            return extractedText;
        }

Пожалуйста, предложите, мой путь правильный или нет ..

1 Ответ

1 голос
/ 14 марта 2019

Я использовал этот PDF tutorialspoint.com / uipath / uipath_tutorial.pdf .. Где я пытаюсь найти текст «часть соревнований», который имеет x = 55,6 мм, y = 168,8 ширины = 210,0 мм и высота = 297,0. Но я получаю пустое значение

Я проверил ваш метод с этими входами:

System.out.println("Extracting like Venkatachalam Neelakantan from uipath_tutorial.pdf\n");
float MM_TO_UNITS = 1/(10*2.54f)*72;
String text = getTextUsingPositionsUsingPdf("src/test/resources/mkl/testarea/pdfbox2/extract/uipath_tutorial.pdf",
        0, 55.6 * MM_TO_UNITS, 168.8 * MM_TO_UNITS, 210.0 * MM_TO_UNITS, 297.0 * MM_TO_UNITS);
System.out.printf("\n---\nResult:\n%s\n", text);

( ExtractText test testUiPathTutorial)

и получил результат

 part of contents of this e-book in any manner without written consent 

te the contents of our website and tutorials as timely and as precisely as 
, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. 
guarantee regarding the accuracy, timeliness or completeness of our 
tents including this tutorial. If you discover any errors on our website or 
ease notify us at contact@tutorialspoint.com 

i 

Предполагая, что вы на самом деле искали «часть содержимого», а не «часть контестов», просто отсутствует «a»; вероятно, при измерении вы искали начало видимой буквы, но фактическое происхождение глифа немного раньше. Если вы выберете чуть меньше x , например, 54,6 мм, вы также получите «а».

Очевидно, неудивительно, что вы получаете больше, чем «часть содержимого», учитывая ширину и высоту вашего прямоугольника.

Если вас интересует постоянная MM_TO_UNITS, взгляните на этот ответ .

...