Hi, the following code snippet shows you how to find text by a pattern and highlight the results with yellow.
importcom.spire.pdf.general.find.PdfTextFind;importjava.awt.*;publicclassFindByRegularExpression{publicstaticvoidmain(String[]args)throwsException{//Load a PDF documentPdfDocumentpdf=newPdfDocument();pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.pdf");//Creat a PdfTextFind collection PdfTextFind[]results;//Loop through the pagesfor(Objectpage:(Iterable)pdf.getPages()){PdfPageBasepageBase=(PdfPageBase)page;//Define a patternStringpattern="\\#\\w+\\b";//Find all results that match the patternresults=pageBase.findText(pattern).getFinds();//Highlight results with yellowfor(PdfTextFindfind:results){find.applyHighLight(Color.yellow);}}//Save to filepdf.saveToFile("output.pdf");}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi, the following code snippet shows you how to find text by a pattern and highlight the results with yellow.