Word文档中的超链接是很不便的工具,能够用来跳转到网页、文件、邮箱等,甚至能够跳转该文档中的某一部分。尽管超链接自身是很实用的,然而有时咱们获取的文档中蕴含大量不须要的超链接,尤其是从网络下载复制的内容。如果手动逐个删除又会十分浪费时间,这时便能够用程序来帮忙咱们疾速去除超链接。本文将介绍一种应用程序疾速删除Word文档中所有超链接的办法。
本文所应用的办法须要用到收费的Jar:Free Spire.Doc for Java,可通过如下路径引入Jar文件。
1. 应用Maven
复制上面的代码到我的项目文件夹下的“pom.xml“文件中
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url></repository></repositories><dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>5.2.0</version> </dependency></dependencies>
2. 官网下载
在Spire.Doc for Java免费版官网下载免费版,解压后,在“Project Structure“中,找到”Modules“,而后在其中的“Dependencies”中,增加解压出的“lib”文件夹下的Spire.Doc.jar文件。
删除Word文档中的所有超链接
删除所有超链接除了援用引入的Jar中的办法外,还须要自定义两个办法,具体操作步骤如此下:
- 创立 Document 的对象。
- 用 Document.loadFromFile() 从磁盘中加载 Word 文档。
- 用自定义的 FindAllHyperlinks() 办法找到文档中所有超链接。
- 循环所有超链接,用自定义的 FlattenHyperlinks() 办法移除所有超链接及格局。
- 用 Document.saveToFile() 办法保存文档到文件。
代码示例如下,自定义的FindAllHyperlinks()和FlattenHyperlinks()办法可在如下代码中获取:
Javaimport com.spire.doc.*;import com.spire.doc.documents.DocumentObjectType;import com.spire.doc.documents.Paragraph;import com.spire.doc.documents.UnderlineStyle;import com.spire.doc.fields.Field;import com.spire.doc.fields.TextRange;import java.awt.*;import java.util.ArrayList;public class removeHyperlink { public static void main(String[] args) { //创立 Document 的对象,并从磁盘加载Word文档 String input = "D:/示例/示例.docx"; Document doc = new Document(); doc.loadFromFile(input); //找到所有超链接 ArrayList hyperlinks = FindAllHyperlinks(doc); // 循环所有超链接,移除超链接及格局 for (int i = hyperlinks.size() -1; i >= 0; i--) { FlattenHyperlinks(hyperlinks.get(i)); } //保存文档到文件 String output = "D:/javaOutput/移除超链接.docx"; doc.saveToFile(output, FileFormat.Docx); } //创立 FindAllHyperlinks() 办法来获取文档中所有超链接 private static ArrayList FindAllHyperlinks(Document document) { ArrayList hyperlinks = new ArrayList(); //在文档中循环查找所有超链接 for (Section section : (Iterable)document.getSections()) { for (DocumentObject object : (Iterable)section.getBody().getChildObjects()) { if (object.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) { Paragraph paragraph = (Paragraph) object; for (DocumentObject cObject : (Iterable)paragraph.getChildObjects()) { if (cObject.getDocumentObjectType().equals(DocumentObjectType.Field)) { Field field = (Field) cObject; if (field.getType().equals(FieldType.Field_Hyperlink)) { hyperlinks.add(field); } } } } } } return hyperlinks; } //创立 FlattenHyperlinks() 办法来移除超链接及格局 public 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); } } } //创立 FormatFieldResultText() 办法来移除超链接的字体色彩和下划线格局 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)); } } } } //创立 FormatText() 办法把字体色彩改为彩色并移除下划线 private static void FormatText(TextRange tr) { //将字体设置为彩色 tr.getCharacterFormat().setTextColor(Color.black); //将下划线设置为无下划线 tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None); }}
删除成果示意:
以上援用均来自收费的Jar。