Word文書のハイパーリンクは、ウェブページやファイル、メールボックスなどにジャンプしたり、文書の一部にジャンプしたりするのに使える、とても便利なツールです。ハイパーリンク自体はとても実用的なものですが、送られてきた文書、特にウェブページからダウンロードしたりコピーした文書には、不要なハイパーリンクが大量に含まれていることがあります。それらをひとつひとつ手作業で削除すると、多くの時間を浪費することになります。Javaプログラムを使えば、不要なハイパーリンクを素早く削除することができます。この記事では、Javaプログラムを使用して、Word文書内のすべてのハイパーリンクをすばやく削除する方法を紹介します。
この記事の方法は、無料のWord文書処理Java API、Free Spire.Doc for Javaが必要です。このAPIは、公式サイトからダウンロードするか、Mavenでプロジェクトに導入することができます。
<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>
JavaでWord文書のすべてのハイパーリンクを削除
すべてのハイパーリンクを削除する 参照メソッドにジャーを導入することに加えて、ハイパーリンクの削除を容易にするために、2つのメソッドをカスタマイズすることもできる。 具体的な手順は以下の通り:
- Document オブジェクトを作成する。
- Document.loadFromFile() メソッドを使用して、Word文書をディスクから読み込みます。
- カスタムの FindAllHyperlinks() メソッドを使用して、ドキュメント内のすべてのハイパーリンクを検索します。
- すべてのハイパーリンクをループし、カスタム FlattenHyperlinks() メソッドですべてのハイパーリンクと書式設定を削除します。
- Document.saveToFile() メソッドを使用して、ドキュメントをファイルに保存します。
コード例は以下のとおりで、カスタム FindAllHyperlinks() および FlattenHyperlinks() メソッドは以下のコードで取得できます:
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 = "例.docx";
Document doc = new Document();
doc.loadFromFile(input);
// すべてのハイパーリンクを見つけます
ArrayList<Field> hyperlinks = findAllHyperlinks(doc);
// すべてのハイパーリンクと書式を削除するためにループします
for (int i = hyperlinks.size() - 1; i >= 0; i--) {
flattenHyperlink(hyperlinks.get(i));
}
// ドキュメントをファイルに保存します
String output = "output/ハイパーリンクの削除.docx";
doc.saveToFile(output, FileFormat.Docx);
}
// ドキュメント内のすべてのハイパーリンクを取得するためのfindAllHyperlinks()メソッドを作成します
private static ArrayList<Field> findAllHyperlinks(Document document) {
ArrayList<Field> hyperlinks = new ArrayList<>();
// ドキュメント内のすべてのハイパーリンクを検索します
for (Section section : (Iterable<? extends Section>) document.getSections()) {
for (DocumentObject object : (Iterable<? extends DocumentObject>) section.getBody().getChildObjects()) {
if (object.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) {
Paragraph paragraph = (Paragraph) object;
for (DocumentObject cObject : (Iterable<? extends DocumentObject>) paragraph.getChildObjects()) {
if (cObject.getDocumentObjectType().equals(DocumentObjectType.Field)) {
Field field = (Field) cObject;
if (field.getType().equals(FieldType.Field_Hyperlink)) {
hyperlinks.add(field);
}
}
}
}
}
}
return hyperlinks;
}
// ハイパーリンクと書式を削除するためのflattenHyperlink()メソッドを作成します
public static void flattenHyperlink(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);
}
}
上記は、Javaコードを使ってWord文書のハイパーリンクを削除する方法についてです。 その他の機能については、Spire.Doc for Javaチュートリアルを参照してください。