'Highlight specific word in a pdf document using itext7
I want to create a pdf report in which some of the text alone is need to be highlighted while generating report.
my current code
private static Text returnCorrectColor(String letter,String htext, PdfFont helveticaFont, PdfFont helveticaBoldFont) {
ArrayList<String>htextaslist = new ArrayList<>(Arrays.asList(htext.trim().split("\\r?\\n")));
String v=StringUtils.normalizeSpace( htext.trim());
Boolean flag=false;
try {
for (int i=0;i<htextaslist.size();i++) {
System.out.println("entire linr"+letter);
System.out.println("htext"+htextaslist.get(i));
if (letter.trim().contains(htextaslist.get(i).trim())) {
System.out.println("boolran"+"0inside");
flag=true;
if(flag)
{
break;
}
}
else {
flag=false;
}
}
} catch (PatternSyntaxException e) {
// TODO: handle exception
flag=true;
}
if(flag) {
return new Text(letter)
.setBackgroundColor(Color.GREEN)
.setFontColor(Color.BLACK)
.setFont(helveticaBoldFont);
} else {
return new Text(String.valueOf(letter))
.setFontColor(Color.BLACK)
.setFont(helveticaFont);
}
}
with this i'm able to highlight the entire line but what i need is to highlight a specific text that is "htext" only to be highlighted in a line.
And i don't hardcode the data in paragraph i call this method inside a for loop like
outputaslist = new ArrayList<>(Arrays.asList(output.split("\\r?\\n")));
for (int s = 0; s < outputaslist.size(); s++) {
paragraph.add(returnCorrectColor(outputaslist.get(s),testcaselist.get(i).getHtext(), helveticaFont, helveticaBoldFont));
paragraph.add("\n");
}
my current ouput is like the above one but i expect the one below

Is there any way to do this?
one of my sample input
outputActive Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
1185/systemd-resolv
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1441/cupsd
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1780/nginx: master
tcp 0 0 127.0.0.1:35373 0.0.0.0:* LISTEN 1616/containerd
tcp 0 0 10.10.100.242:51938 13.33.60.102:443 TIME_WAIT -
tcp 0 0 10.10.100.242:52762 34.210.242.116:443 ESTABLISHED 6994/firefox
I would like to highlight only listen in the output
Solution 1:[1]
I'm a little late to the party, but I had a similar issue,
To do this you have to find the locations of your words, then highlight those words.
public static List<IPdfTextLocation> PdfFindKeywordLocations(PdfDocument pdfDocument, Keyword keyword, int pageNumber)
{
string cleanKeyword = DataFormatHelper.ReplaceStringText(keyword.Name, new Dictionary<string, string>()
{
{"[ ]", @"\W"}
});
Regex regex = new Regex(cleanKeyword, RegexOptions.IgnoreCase);
RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy(regex);
PdfCanvasProcessor parser = new PdfCanvasProcessor(extractionStrategy);
parser.ProcessPageContent(pdfDocument.GetPage(pageNumber));
return extractionStrategy.GetResultantLocations().ToList();
}
private static void PdfHighlightLocations(PdfDocument pdfDocument, IEnumerable<IPdfTextLocation> locations, int pageNumber)
{
PdfCanvas pdfCanvas = new PdfCanvas(pdfDocument.GetPage(pageNumber).NewContentStreamBefore(), pdfDocument.GetPage(pageNumber).GetResources(), pdfDocument);
foreach (var l in locations)
{
pdfCanvas.SaveState().SetFillColor(ColorConstants.GREEN).Rectangle(l.GetRectangle().GetX(), l.GetRectangle().GetY(), l.GetRectangle().GetWidth(), l.GetRectangle().GetHeight()).Fill().RestoreState();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robert Sullivan |

