チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.Doc for Java»ハイパーリンク»Java:Word ドキュメントからハイパーリンクを削除する方法
2022-08-08

Java:Word ドキュメントからハイパーリンクを削除する方法

ハイパーリンクは通常テキストに表示されます。 ハイパーリンクをクリックすると、ウェブサイト、ドキュメント、電子メール、またはその他の要素にアクセスできます。Word ドキュメントの中には、特にインターネットから生成されたものには、広告のような不快なハイパーリンクが含まれていることがあります。この記事では、Spire.Doc for Java を使用して、Word ドキュメントから特定のハイパーリンクまたはすべてのハイパーリンクを削除する方法を説明します。

Spire.Doc for Java をインストールします

まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。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</artifactId>
        <version>10.7.10</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 removeOneHyperlink {
    public static void main(String[] args) {

        //Documentクラスのオブジェクトを作成し、Wordドキュメントを読み込む
        String input = "実例.docx";
        Document doc = new Document();
        doc.loadFromFile(input);

        //すべてのハイパーリンクを検索する
        ArrayList hyperlinks = FindAllHyperlinks(doc);

        //最初のハイパーリンクの形式を削除する
        FlattenHyperlinks(hyperlinks.get(0));

        //ドキュメントを保存する
        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);

        //テキストの下線スタイルを「None」にする
        tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);
    }
}

Java:Word ドキュメントからハイパーリンクを削除する方法

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 removeAllHyperlinks {
    public static void main(String[] args) {
        //Documentクラスのオブジェクトを作成し、Wordドキュメントを読み込む
        String input = "実例.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);

        //テキストの下線スタイルを「None」に設定する
        tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);
    }
}

Java:Word ドキュメントからハイパーリンクを削除する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 557 times