I introduced how to replace text in a Word document using Java codes in my previous tutorial. In this article, I’ll show you how to highlight text in Java programmatically using a free API called FreeSpire.Doc for Java. Normally, we can find all matched text by its content and then highlight it with color, or we can use index to search the location of the text we want to highlight.
About Free Spire.Doc for Java
It
is a free and professional Java Word API that enables Java applications to
create, manipulate, convert and print Word documents without using Microsoft
Office.
A
plenty of Word document processing tasks can be performed by Free Spire.Doc for
Java, such as insert image, add header and footer, create table, add form field
and mail merge field, add bookmark and watermark, add hyperlink, set background
color/image, add footnote and endnote, encrypt Word documents.
Create a test environment
Before
typing codes, in addition to installing JDK and Intellij IDEA, you need to add
Spire.Doc.jar to your project as a dependency. If you’re creating a Maven
project, just add the following configurations to the pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Or you can download the package from the E-iceblue website, unzip it and find Spire.Doc.jar in the
“lib” folder. Finally manually add it to your project.
Using the code
Highlight text by its content
Free Spire.Doc for Java provides two methods to find and highlight text in a Word document:
findString() and findAllString(). The findString() method finds the first matched text, while the
findAllString() method finds all matched text. The below example shows how to find all matched text
using findAllString() method and then highlight the text.
import com.spire.doc.*;
import com.spire.doc.documents.TextSelection;
import java.awt.*;
public class FindAndHighlightText {
public static void main(String[] args) {
//Load Word document
Document document = new Document("C:\\Users\\Test1\\Desktop\\Test.docx");
//Find all “sands” text
TextSelection[] textSelections = document.findAllString("sands", false, true);
//Set highlight color
for (TextSelection selection : textSelections) {
selection.getAsOneRange().getCharacterFormat().setHighlightColor(Color.YELLOW);
}
//Save the document
document.saveToFile("output/FindAndHightText.docx", FileFormat.Docx_2013);
}
}Output
Highlight text by its location
Free Spire.Doc for Java also supports search the location of the text by index and highlight it.
The following codes demonstrates how to highlight the text which is located at the 26th to 29th in
the second paragraph.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;
import java.awt.*;
import java.util.ArrayList;
import java.util.regex.Pattern;
public class HighlightText {
public static void main(String[] args) {
//Load the file
Document document = new Document();
document.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.docx");
//Find all words of the document
String pattern = "\\d+.\\d+|\\w+";
Pattern rgx=Pattern.compile(pattern);
TextSelection[] textSelections = document.findAllPattern(rgx);
//Define a TextRange list
ArrayList<TextRange> textrangeList = new ArrayList<TextRange>();
//If the found word in second paragraph, then add it to list
for (TextSelection selection: textSelections) {
TextRange tr = selection.getAsOneRange();
Paragraph oPara = selection.getAsOneRange().getOwnerParagraph();
Section sec = (Section)(selection.getAsOneRange().getOwnerParagraph().getOwner().getOwner()) ;
int paraIndex = sec.getBody().getParagraphs().indexOf(oPara);
if (paraIndex==1)
{
textrangeList.add(tr);
}
}
//Highlight the fifth to eighth words
for (int i = 25; i < 29; i++)
{
textrangeList.get(i).getCharacterFormat().setHighlightColor(Color.YELLOW);
}
document.saveToFile("output/HighlightText.docx", FileFormat.Docx);
}
}
Output
No comments:
Post a Comment