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.

Tue Jun 28, 2022 3:41 am

Hi there,

May I ask is possible that PdfTextFindCollection able to find text and draw a rectangle based on a specific line? As there have multiple same texts in pdf and my expected output is to draw a rectangle for (Trnx Date, Due Date, and Item Description) from the first item description.

Code: Select all
try ( Stream<String> lines = Files.lines(Paths.get(extractText), Charset.defaultCharset())) {
                String extracBasedOnLine = lines.skip(lineNo - 1).findFirst().get();
                int lineWord = pdfText.indexOf(extracBasedOnLine);
                if (columnNo == 0) {
                    result = pdfText.substring(lineWord, lineWord + maxLength);
                } else {
                    result = pdfText.substring(lineWord + columnNo, lineWord + maxLength);
                }
                PdfTextFindCollection pdfTextFindCollection = page.findText(result.trim());
                PdfTextFind[] extractedResult = pdfTextFindCollection.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) {
                System.out.println("Error == " + e);
                System.out.println("result == " + result.trim());
            }


I'm very appreciate it if you could help.

Regards,
Elizabeth

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

Tue Jun 28, 2022 10:32 am

Hi Elizabeth,

Thanks for your inquiry.

I'm sorry that Spire.Pdf does not support this case. Maybe you can try the ideas in the code below, but there are definitely cases that cannot be handled. This is just a possible way I personally think, so just for your reference.

Code: Select all
        PdfTextFindCollection pdfTextFindCollection=page.findText("CHARGES MAINTENANCE",EnumSet.of(TextFindParameter.WholeWord));
        PdfTextFind find = pdfTextFindCollection.getFinds()[0];
        //Draw rect for the key word
        page.getCanvas().drawRectangle(......);
        String text=find.getLineText();

        String []items=text.split(" ");
        for(String item:items){
            PdfTextFindCollection finds=page.findText(item,EnumSet.of(TextFindParameter.WholeWord));
            //Draw rects for other words in the same line
            page.getCanvas().drawRectangle(......);
        }
Sincerely,
Andy
E-iceblue support team
User avatar

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

Wed Jun 29, 2022 1:46 am

Hi Andy.Zhou,


Thanks for your help. I will try it.


Regards,
Elizabeth

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

Return to Spire.PDF