超链接通常出现在文本上,只需要点击这些超链接,我们就可以跳转到特定的网页、文档、邮箱地址或是其他元素。但有些文档中,尤其是复制网页内容生成的 Word 文档,可能会有一些令人讨厌的超链接,例如广告。本文将展示如何用 Spire.Doc for Java 移除 Word 文档中的超链接。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从 这个链接 下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.4.1</version>
</dependency>
</dependencies>
移除 Word 文档中的指定超链接
以下是实现移除 Word 文档中指定超链接的详细操作步骤:
- 创建 Document 的对象,并用 Document.loadFromFile() 从磁盘中加载 Word 文档。
- 用自定义的 FindAllHyperlinks() 方法找到文档中所有超链接。
- 用自定义的 FlattenHyperlinks() 方法移除第一个超链接及格式。
- 用 Document.saveToFile() 方法保存文档到文件。
- Java
import 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:/testp/示例.docx";
Document doc = new Document();
doc.loadFromFile(input);
//找到所有超链接
ArrayList hyperlinks = FindAllHyperlinks(doc);
//移除第一个超链接及格式
FlattenHyperlinks(hyperlinks.get(0));
//保存文档到文件
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);
}
}
移除 Word 文档中的所有超链接
移除 Word 文档中所有超链接的详细操作步骤如下:
- 创建 Document 的对象,并用 Document.loadFromFile() 从磁盘中加载 Word 文档。
- 用自定义的 FindAllHyperlinks() 方法找到文档中所有超链接。
- 循环所有超链接,用自定义的 FlattenHyperlinks() 方法移除超链接及格式。
- 用 Document.saveToFile() 方法保存文档到文件。
- Java
import 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:/testp/示例.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);
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。