Word文档中的超链接是非常方便的工具,可以用来跳转到网页、文件、邮箱等,甚至可以跳转到文档的某个部分。虽然超链接本身很有用,但有时我们会收到包含很多不需要的超链接的文档,尤其是从Web下载和复制时。如果手动一一删除太浪费时间,那么我们可以使用一个程序来帮助我们快速删除超链接。本文将介绍一种用程序快速删除Word文档中所有超链接的方法。本文使用的方法需要一个免费的Jar:FreeSpire.DocforJava,Jar文件可以通过以下方式导入。1.使用Maven将以下代码复制到项目文件夹中的“pom.xml”文件中com.e-icebluee-icebluehttps://repo.e-iceblue.com/nexus/content/groups/public/e-icebluespire.doc.free5.2.02.官网下载从Spire.DocforJava免费版官网下载免费版,解压后,在“ProjectStructure”中找到“Modules”,然后在“Dependencies”中添加解压后的Spire.Doc.jar文件“lib”文件夹。删除Word文档中的所有超链接要删除所有超链接,除了引用导入的Jar中的方法外,还需要自定义两个方法。具体步骤如下:创建一个Document对象。使用Document.loadFromFile()从磁盘加载Word文档。使用自定义FindAllHyperlinks()方法查找文档中的所有超链接。遍历所有超链接,并使用自定义FlattenHyperlinks()方法删除所有超链接和格式。使用Document.saveToFile()方法将文档保存到文件。代码示例如下,自定义的FindAllHyperlinks()和FlattenHyperlinks()方法可以在如下代码中获取:doc.documents.Paragraph;导入com.spire.doc.documents.UnderlineStyle;导入com.spire.doc.fields.Field;导入com.spire.doc.fields.TextRange;导入java.awt.*;导入java.util。数组列表;publicclassremoveHyperlink{publicstaticvoidmain(String[]args){//创建一个Document对象并从磁盘加载一个Word文档Stringinput="D:/Example/Example.docx";文件doc=newDocument();doc.loadFromFile(输入);//查找所有超链接ArrayListhyperlinks=FindAllHyperlinks(doc);//循环所有超链接,去除超链接和格式for(inti=hyperlinks.size()-1;i>=0;i--){FlattenHyperlinks(hyperlinks.get(i));}//将文档保存到文件中Stringoutput="D:/javaOutput/RemoveHyperlink.docx";doc.saveToFile(输出,FileFormat.Docx);}//创建FindAllHyperlinks()方法来PrivatestaticArrayListFindAllHyperlinks(Documentdocument){ArrayList超链接=newArrayList();DocumentObjectobject:(Iterable)section.getBody().getChildObjects()){if(object.getDocumentObjectType().equals(DocumentObjectType.Paragraph)){Paragraphparagraph=(Paragraph)object;for(DocumentObjectcObject:(Iterable)paragraph.getChildObjects()){if(cObject.getDocumentObjectType().equals(DocumentObjectType.Field)){字段字段=(Field)cObject;如果(field.getType().equals(FieldType.Field_Hyperlink)){hyperlinks.add(fields);}}}}}}返回超链接;}//创建FlattenHyperlinks()方法来移除超链接及格式publicstaticvoidFlattenHyperlinks(Fieldfield){intownerParaIndex=field.getOwnerParagraph().ownerTextBody().getChildObjects().indexOf(field.getOwnerParagraph());intfieldIndex=field.getOwnerParagraph().getChildObjects().indexOf(field);段落sepOwnerPara=field.getSeparator().getOwnerParagraph();intsepOwnerParaIndex=field.getSeparator().getOwnerParagraph().ownerTextBody().getChildObjects().indexOf(field.getSeparator().getOwnerParagraph());intsepIndex=field.getSeparator().getOwnerParagraph().getChildObjects().indexOf(field.getSeparator());intendIndex=field.getEnd().getOwnerParagraph().getChildObjects().indexOf(field.getEnd());intendOwnerParaIndex=field.getEnd().getOwnerParagraph().ownerTextBody().getChildObjects().indexOf(field.getEnd().getOwnerParagraph());FormatFieldResultText(field.getSeparator().getOwnerParagraph().ownerTextBody(),sepOwnerParaIndex,endOwnerParaIndex,sepIndex,endIndex);field.getEnd().getOwnerParagraph().getChildObjects().removeAt(endIndex);对于(inti=sepOwnerParaIndex;i>=ownerParaIndex;i--){if(i==sepOwnerParaIndex&&i==ownerParaIndex){for(intj=sepIndex;j>=fieldIndex;j--){field.getOwnerParagraph().getChildObjects().removeAt(j);}}elseif(i==ownerParaIndex){for(intj=field.getOwnerParagraph().getChildObjects().getCount()-1;j>=fieldIndex;j--){field.getOwnerParagraph().getChildObjects().removeAt(j);}}否则如果(我==sepOwnerParaIndex){for(intj=sepIndex;j>=0;j--){sepOwnerPara.getChildObjects().removeAt(j);}}else{field.getOwnerParagraph().ownerTextBody().getChildObjects().removeAt(i);}}}//创建FormatFieldResultText()方法去除超链接字体颜色和下划线格式privatestaticvoidFormatFieldResultText(BodyownerBody,intsepOwnerParaIndex,intendOwnerParaIndex,intsepIndex,intendIndex){for(inti=sepOwnerParaIndex;i<=endOwnerParaIndex;i++){段落para=(段落)ownerBody.getChildObjects().get(i);如果(i==sepOwnerParaIndex&&i==endOwnerParaIndex){for(intj=sepIndex+1;j