When you get a document that contains multiple hyperlinks that you don’t want to jump to, you may want to remove the hyperlinks from the document. In this article, I will introduce how to remove all the hyperlinks in a Word document at once but keep the text programmatically in Java.
Add Dependencies
In order to remove hyperlinks from a Word document, I used Free Spire.Doc for Java library. There are two ways to include Free Spire.Doc for Java in your Java project:
For maven projects:
Add the following dependencies to your project’s pom.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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> |
The latest version of Free Spire.Doc for Java is 3.9.0 (at the time of writing this article).
For non-maven projects:
Download Free Spire.Doc for Java pack from here: Download- Free Spire.Doc for Java, extract the zip file, then add Spire.Doc.jar in the lib folder into your project as a dependency.
Using the code
The following example shows how to remove all the hyperlinks in a Word document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.awt.*; import java.util.ArrayList; public class RemoveHyperlinks { private static ArrayList<Field> hyperlinks = new ArrayList<>(); public static void main(String []args){ //Load document Document doc = new Document(); doc.loadFromFile( "Input.docx" ); //Get all hyperlinks for ( int i = 0 ;i<doc.getSections().getCount();i++) { Section section = doc.getSections().get(i); //Get the hyperlinks in section body iterateTextBody(section.getBody()); //Get the hyperlinks in header or footer HeaderFooter header = section.getHeadersFooters().getHeader(); HeaderFooter footer = section.getHeadersFooters().getFooter(); iterateTextBody(header); iterateTextBody(footer); } //Remove all the hyperlinks for ( int i = hyperlinks.size() - 1 ; i >= 0 ; i--) { flattenHyperlinks(hyperlinks.get(i)); } hyperlinks.clear(); //Save the document doc.saveToFile( "RemoveHyperlinks.docx" , FileFormat.Docx); } //Get hyperlinks from Body object private static void iterateTextBody(Body body) { for ( int i = 0 ;i< body.getChildObjects().getCount();i++) { DocumentObject obj = body.getChildObjects().get(i); if (obj.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) { iterateParagraph((Paragraph)obj); } if (obj.getDocumentObjectType().equals(DocumentObjectType.Table)) { iterateTable((Table)obj); } } } private static void iterateParagraph(Paragraph paragraph) { for (DocumentObject cObject : (Iterable<DocumentObject>)paragraph.getChildObjects()) { if (cObject.getDocumentObjectType().equals(DocumentObjectType.Field)) { Field field = (Field)cObject; if (field.getType().equals( FieldType.Field_Hyperlink)) { hyperlinks.add(field); } } } } private static void iterateTable(Table table) { for (TableRow row:(Iterable<TableRow>)table.getRows()){ for (TableCell cell : (Iterable<TableCell>)row.getCells()){ for (Paragraph para : (Iterable<Paragraph>)cell.getParagraphs()){ iterateParagraph(para); } } } } //Flatten the hyperlink fields private static void flattenHyperlinks(Field field) { int ownerParaIndex = field.getOwnerParagraph().ownerTextBody().getChildObjects().indexOf(field.getOwnerParagraph()); int fieldIndex = field.getOwnerParagraph().getChildObjects().indexOf(field); Paragraph sepOwnerPara = field.getSeparator().getOwnerParagraph(); int sepOwnerParaIndex = field.getSeparator().getOwnerParagraph().ownerTextBody().getChildObjects().indexOf(field.getSeparator().getOwnerParagraph()); int sepIndex = field.getSeparator().getOwnerParagraph().getChildObjects().indexOf(field.getSeparator()); int endIndex = field.getEnd().getOwnerParagraph().getChildObjects().indexOf(field.getEnd()); int endOwnerParaIndex = field.getEnd().getOwnerParagraph().ownerTextBody().getChildObjects().indexOf(field.getEnd().getOwnerParagraph()); formatFieldResultText(field.getSeparator().getOwnerParagraph().ownerTextBody(), sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex); field.getEnd().getOwnerParagraph().getChildObjects().removeAt(endIndex); for ( int i = sepOwnerParaIndex; i >= ownerParaIndex; i--) { if (i == sepOwnerParaIndex && i == ownerParaIndex) { for ( int j = sepIndex; j >= fieldIndex; j--) { field.getOwnerParagraph().getChildObjects().removeAt(j); } } else if (i == ownerParaIndex) { for ( int j = field.getOwnerParagraph().getChildObjects().getCount()- 1 ; j >= fieldIndex; j--) { field.getOwnerParagraph().getChildObjects().removeAt(j); } } else if (i == sepOwnerParaIndex) { for ( int j = sepIndex; j >= 0 ; j--) { sepOwnerPara.getChildObjects().removeAt(j); } } else { field.getOwnerParagraph().ownerTextBody().getChildObjects().removeAt(i); } } } //Remove the font color and underline format of the hyperlinks private static void formatFieldResultText(Body ownerBody, int sepOwnerParaIndex, int endOwnerParaIndex, int sepIndex, int endIndex) { for ( int i = sepOwnerParaIndex; i <= endOwnerParaIndex; i++) { Paragraph para = (Paragraph)ownerBody.getChildObjects().get(i); if (i == sepOwnerParaIndex && i == endOwnerParaIndex) { for ( int j = sepIndex + 1 ; j < endIndex; j++) { formatText((TextRange)para.getChildObjects().get(j)); } } else if (i == sepOwnerParaIndex) { for ( int j = sepIndex + 1 ; j < para.getChildObjects().getCount(); j++) { formatText((TextRange)para.getChildObjects().get(j)); } } else if (i == endOwnerParaIndex) { for ( int j = 0 ; j < endIndex; j++) { formatText((TextRange)para.getChildObjects().get(j)); } } else { for ( int j = 0 ; j < para.getChildObjects().getCount(); j++) { formatText((TextRange)para.getChildObjects().get(j)); } } } } private static void formatText(TextRange tr) { //Set the text color to black tr.getCharacterFormat().setTextColor(Color.black); //Set the text underline style to none tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None); } } |
The following is the screenshot of the input Word document:
The following is the output Word document:
Comments
Post a Comment