Requirement
Now I have a Word document as
below and it mainly introduces the Java computer language. My requirement is
that I want to add the hyperlink called https://en.wikipedia.org/wiki/Java_(programming_language)
for all “Java” text in this article.
Solution
Obviously, it’ll take a long time
to manually add hyperlinks for every text. After testing many third-party
libraries, I found one called Free Spire.Doc for Java which can use Java codes to search all matched text “Java”
in the article and then add the hyperlink for them all at once. Now I’ll show
you how to do it.
Preparation
Before running codes, you need to do some preparations. First, you need to install JDK and IntelliJ IDEA in your computer. The next thing is that download Free Spire.Doc for Java from the link, unzip it and find Spire.Doc.jar in the “lib” folder, finally manually add the jar to IDEA. Of course, here there is an easier way to add the Jar file which is typing the following configuration in the Maven pom.xml.
<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>
Using
the code
Create a Java Class in IDEA, write
and run the following codes.
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.FieldMarkType;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.documents.UnderlineStyle;
import com.spire.doc.fields.Field;
import com.spire.doc.fields.FieldMark;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class Hyperlink {
public static void main(String[] args) {
//Load a Word sample document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");
//Find all the matched text
TextSelection[] text = doc.findAllString("Java", false, true);
for (int i =0; i<text.length;i++)
{
//Get the TextRange
TextSelection seletion= text[i];
TextRange tr = seletion.getAsOneRange();
int index = tr.getOwnerParagraph().getChildObjects().indexOf(tr);
//Add hyperlinks to the matched text
Field field = new Field(doc);
field.setCode( "HYPERLINK \"" + "https://en.wikipedia.org/wiki/Java_(programming_language)" + "\"");
field.setType( FieldType.Field_Hyperlink);
tr.getOwnerParagraph().getChildObjects().insert(index, field);
FieldMark fm = new FieldMark(doc, FieldMarkType.Field_Separator);
tr.getOwnerParagraph().getChildObjects().insert(index + 1, fm);
//Set the format of hyperlinks
tr.getCharacterFormat().setTextColor( Color.BLUE);
tr.getCharacterFormat().setUnderlineStyle( UnderlineStyle.Single);
tr.getCharacterFormat().setBold(true);
FieldMark fmend = new FieldMark(doc, FieldMarkType.Field_End);
tr.getOwnerParagraph().getChildObjects().insert(index + 3, fmend);
field.setEnd(fmend);
}
//Save the resulting document
doc.saveToFile("output/SearchTextAndAddHyperlink.docx", FileFormat.Docx_2013);
}
}
Output
No comments:
Post a Comment