Word文書内のハイパーリンクは、Webページやファイル、メールボックスなどに移動したり、文書の特定の部分に移動したりすることができる便利なツールです。ハイパーリンク自体はとても便利なものですが、特にインターネットからダウンロードしたりコピーしたものなど、不要なハイパーリンクを大量に含む文書を入手することがあります。これをひとつひとつ手作業で削除するのは時間の無駄です。この記事では、無料のFree Spire.Doc for Javaを使って、Word文書からすべてのハイパーリンクをすばやく削除し、その他の書式を維持する方法について説明します。
【依存関係の追加】
この方法は、無料のFree Spire.Doc for Javaが必要ですので、先にjarファイルをインポートしてください。
1. Maven
Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。
<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.1.0</version>
</dependency>
</dependencies>
2. 公式サイトよりJarファイルをダウンロード
まず、Free Spire.Doc for Java の公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Doc.jarファイルを依存関係としてプロジェクトにインポートしてください。
Word文書内のすべてのハイパーリンクをすばやく削除し、他の書式を変更しない
Word文書内のすべてのハイパーリンクを削除する手順は、すべてのハイパーリンクを探し出し、リンクを削除し、テキストの色を変更し、下線を削除することです。
詳細な手順は以下の通りです。
- Document クラスのオブジェクトを作成します。
- Word文書を Document.loadFromFile() で読み込みます。
- カスタムメソッド 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 removeAllHyperlinks {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成し、Word文書を読み込む
String input = "Java.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 = "ハイパーリンクの削除.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ドキュメント】
この記事は、Free Spire.Doc for Javaを使用してWord文書内のハイパーリンクを削除するための詳細な手順を示しています。この記事を読んでもまだ質問がある場合、またはWord文書処理スキルについてもっと知りたい場合は、Spire.Doc Forumに移動してください。