Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon May 09, 2022 9:12 am

Hi there,

Extracted text = "D - 10-01"

May I know how to solve the problem when there have multiple text find collection in a PDF and I only need to draw a rectangle based on the position that I extracted by using Java. As currently now there have multiple same extracted keyword on the pdf and by using PdfTextFind[], it will draw a rectangle at the last position of the extracted text. How about if I want to draw a rectangle for the first or second occurrence and not for the last ? Hope you can help me solve my problems. Thanks. :D

Below here is my code....


Code: Select all
result = pdfText.substring(lineWord + columnNo, lineWord + maxLength);
try {
                PdfTextFindCollection pdfTextFineCollection = page.findText(result.trim());
                PdfTextFind[] extractedResult = pdfTextFineCollection.getFinds();

                for (PdfTextFind find : extractedResult) {

                    Point2D pointF = find.getPosition();
                    Dimension2D widthHeight = find.getSize();
                    double x = pointF.getX();
                    double y = pointF.getY();
                    double width = widthHeight.getWidth();
                    double height = widthHeight.getHeight();
                    position.setX(x);
                    position.setY(y);
                    position.setHeight(height);
                    position.setWidth(width);

                }
            } catch (Exception e) {
}


elizabethC
 
Posts: 10
Joined: Thu Apr 28, 2022 3:15 am

Mon May 09, 2022 10:02 am

Hello,

Thanks for your inquiry.

Based on your description and code, maybe you can sort the result array by their x,y coordinates before drawing the rectangle. And then you can draw the rectangle on the position as you want (such as the position of the second keyword from top to bottom.). Hope this can help you.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue May 10, 2022 1:10 am

Hello,

I means that you can sort the result of "page.findText().getFinds()" by their x,y coordinates. Then you can choose the one you need to draw with the rectangle.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue May 10, 2022 10:05 am

Hello Andy,

May I ask can I straight away sort the result based on "page.findText()" and directly get the position without go for PdfTextFind array ? Because I want directly get the position of the extracted word. :lol:

elizabethC
 
Posts: 10
Joined: Thu Apr 28, 2022 3:15 am

Wed May 11, 2022 1:42 am

Hello,

Thanks for your reply.
I'm sorry that the "page.findText()" method does not provide a direct parameter for sorting. You can only sort the search results according to your own needs.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Wed May 11, 2022 2:03 am

Hello Andy,

As currently now I'm using extractText functions to extract out the text from PDF to string. If the sort results is more than one occurrence, how can I decide which X and Y value should I take as I need to get the accurate extracted position?

elizabethC
 
Posts: 10
Joined: Thu Apr 28, 2022 3:15 am

Wed May 11, 2022 5:56 am

I'm sorry, I seem to be a little confused and don't quite understand what you mean. Could you please give a simple example to illustrate what exactly you need to achieve? Thank you very much for your patience.
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.PDF