チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
ハイパーリンクは、デジタル文書において、2つのものを結びつけるために使用する最も重要なツールです。読者が Word ドキュメント内のハイパーリンクをクリックすると、ドキュメント内の別の場所、別のファイルやウェブサイト、または新しい電子メールメッセージに移動することができます。この記事では、Spire.Doc for Java を使用して、Java で Word ドキュメントにハイパーリンクを追加する方法について紹介します。
まず、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>11.4.2</version>
</dependency>
</dependencies>
Spire.Doc for Java では、段落内のテキストや画像に Web リンク、メールリンク、ファイルリンク、ブックマークリンクを追加する Paragraph.appendHyperlink() メソッドを提供しています。以下は、その詳細な手順です。
import com.spire.doc.BookmarkStart;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.HyperlinkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocPicture;
public class AddHyperlinks {
public static void main(String[] args) {
//Wordドキュメントを作成する
Document doc = new Document();
//セクションを追加する
Section section = doc.addSection();
//段落を追加する
Paragraph paragraph = section.addParagraph();
paragraph.getStyle().getCharacterFormat().setFontName("BIZ UDGothic");
paragraph.appendBreak(BreakType.Line_Break);
paragraph.appendHyperlink("https://www-iceblue.com/", "ホーム ページ", HyperlinkType.Web_Link);
//改行を追加する
paragraph.appendBreak(BreakType.Line_Break);
paragraph.appendBreak(BreakType.Line_Break);
//メールリンクを追加する
paragraph.appendHyperlink("mailto:このメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。", "メールで問い合わせる。", HyperlinkType.E_Mail_Link);
//改行を追加する
paragraph.appendBreak(BreakType.Line_Break);
paragraph.appendBreak(BreakType.Line_Break);
//ファイルリンクを追加する
String filePath = "C:/報告.xlsx";
paragraph.appendHyperlink(filePath, "クリックすると報告が開きます。", HyperlinkType.File_Link);
//改行を追加する
paragraph.appendBreak(BreakType.Line_Break);
paragraph.appendBreak(BreakType.Line_Break);
//別のセクションを追加し、ブックマークを作成する
Section section2 = doc.addSection();
Paragraph bookmarkParagraph = section2.addParagraph();
bookmarkParagraph.appendText("ここにブックマークがあります。");
BookmarkStart start = bookmarkParagraph.appendBookmarkStart("myBookmark");
bookmarkParagraph.getItems().insert(0, start);
bookmarkParagraph.appendBookmarkEnd("myBookmark");
//ブックマークにリンクする
paragraph.appendHyperlink("myBookmark", "このドキュメント内の場所にジャンプします。", HyperlinkType.Bookmark);
//改行を追加する
paragraph.appendBreak(BreakType.Line_Break);
paragraph.appendBreak(BreakType.Line_Break);
//画像リンクを追加する
String imagePath = "C:/ロゴ.png";
DocPicture picture = paragraph.appendPicture(imagePath);
paragraph.appendHyperlink("https://www.e-iceblue.com/", picture, HyperlinkType.Web_Link);
//ドキュメントを保存する
doc.saveToFile("ハイパーリンクの追加.docx", FileFormat.Docx_2013);
}
}
ドキュメント内の既存のテキストにハイパーリンクを追加するのは、少し複雑です。まず対象となる文字列を見つけ、段落内の文字列をハイパーリンクフィールドに置き換える必要があります。以下はその手順です。
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.FileFormat;
import com.spire.doc.Hyperlink;
import com.spire.doc.documents.*;
import com.spire.doc.fields.Field;
import com.spire.doc.fields.FieldMark;
import com.spire.doc.fields.TextRange;
import com.spire.doc.interfaces.IParagraphBase;
import com.spire.doc.interfaces.ITextRange;
import java.awt.*;
public class AddHyperlinksToExistingText {
public static void main(String[] args) {
//Documentのオブジェクトを作成する
Document document = new Document();
//Wordドキュメントを読み込む
document.loadFromFile("C:/サンプル.docx");
//ドキュメント内で「二酸化炭素」という文字列が出現する箇所をすべて探す
TextSelection[] selections = document.findAllString("二酸化炭素", true, true);
//最初の出現を得る
TextRange range = selections[0].getAsOneRange();
//そのテキストが含まれる段落を取得する
Paragraph paragraph = range.getOwnerParagraph();
//このテキストの段落内での位置を取得する
int index = paragraph.getItems().indexOf(range);
//このテキストを段落から削除する
paragraph.getItems().remove(range);
//ハイパーリンク フィールドを作成する
Field field = new Field(document);
field.setType(FieldType.Field_Hyperlink);
Hyperlink hyperlink = new Hyperlink(field);
hyperlink.setType(HyperlinkType.Web_Link);
hyperlink.setUri("https://ja.wikipedia.org/wiki/%E4%BA%8C%E9%85%B8%E5%8C%96%E7%82%AD%E7%B4%A0");
paragraph.getItems().insert(index, field);
//段落にフィールドマーク「start」を挿入する
IParagraphBase item = document.createParagraphItem(ParagraphItemType.Field_Mark);
FieldMark start = (FieldMark)item;
start.setType(FieldMarkType.Field_Separator);
paragraph.getItems().insert(index + 1, start);
//2つのフィールドマークの間にテキスト範囲を挿入する
ITextRange textRange = new TextRange(document);
textRange.setText("二酸化炭素");
textRange.getCharacterFormat().setFontName(range.getCharacterFormat().getFontName());
textRange.getCharacterFormat().setFontSize(range.getCharacterFormat().getFontSize());
textRange.getCharacterFormat().setBold(range.getCharacterFormat().getBold());
textRange.getCharacterFormat().setTextColor(Color.blue);
textRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.Single);
paragraph.getItems().insert(index + 2, textRange);
//段落にフィールドマーク「end」を挿入する
IParagraphBase item2 = document.createParagraphItem(ParagraphItemType.Field_Mark);
FieldMark end = (FieldMark)item2;
end.setType(FieldMarkType.Field_End);
paragraph.getItems().insert(index + 3, end);
//ドキュメントを保存する
document.saveToFile("既存のテキストにハイパーリンクの挿入.docx", FileFormat.Docx_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word ドキュメント内のハイパーリンクは、読者が他の Web ページ、外部ファイル、電子メールアドレス、またはドキュメントの特定の場所にすばやくアクセスできるようにします。ドキュメントに多数のハイパーリンクが含まれている場合には、手動でリンクを取得するよりも、プログラミングされた方法でリンク情報を直接検索して抽出するほうが時間の節約になります。この記事では、Spire.Doc for Java を使用して Word でハイパーリンクを検索して抽出する方法を示します。
まず、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.11.6</version>
</dependency>
</dependencies>
Spire.Doc for Java が提供する Field.getFieldText() メソッドと Field.getValue() メソッドは、それぞれハイパーリンクのテキストとリンクの取得をサポートしています。具体的な手順は以下の通りです。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.Field;
import java.io.*;
import java.util.ArrayList;
public class findHyperlinks {
public static void main(String[] args) throws IOException {
//Documentインスタンスを作成し、Wordをディスクからロードする
String input = "sample.docx";
Document doc = new Document();
doc.loadFromFile(input);
//ArrayList<Field>のオブジェクトを作成する
ArrayList<Field> hyperlinks = new ArrayList<Field>();
//セクション内のアイテムをループして、すべてのハイパーリンクを検索する
for (Section section : (Iterable<Section>) doc.getSections()) {
for (DocumentObject object : (Iterable<DocumentObject>) section.getBody().getChildObjects()) {
if (object.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) {
Paragraph paragraph = (Paragraph) object;
for (DocumentObject cObject : (Iterable<DocumentObject>) paragraph.getChildObjects()) {
if (cObject.getDocumentObjectType().equals(DocumentObjectType.Field)) {
Field field = (Field) cObject;
if (field.getType().equals(FieldType.Field_Hyperlink)) {
hyperlinks.add(field);
}
}
}
}
}
}
//最初のハイパーリンクのテキストとリンクを取得する
String hyperlinksText = hyperlinks.get(0).getFieldText();
String hyperlinkAddress = hyperlinks.get(0).getValue();
//最初のハイパーリンクのテキストとリンクをTXTに保存する
String output = "result.txt";
writeStringToText("Text:\r\n" + hyperlinksText+ "\r\n" + "Link:\r\n" + hyperlinkAddress, output);
}
//TXTファイルにテキストとリンクを書き込む方法を作成する
public static void writeStringToText(String content, String textFileName) throws IOException {
File file = new File(textFileName);
if (file.exists())
{
file.delete();
}
FileWriter fWriter = new FileWriter(textFileName, true);
try {
fWriter.write(content);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Spire.Doc for Java が提供する Field.getFieldText() メソッドと Field.getValue() メソッドは、それぞれハイパーリンクのテキストとリンクの取得をサポートしています。具体的な手順は以下の通りです。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.Field;
import java.io.*;
import java.util.ArrayList;
public class findHyperlinks {
public static void main(String[] args) throws IOException {
//Documentインスタンスを作成し、Wordをディスクからロードする
String input = "sample.docx";
Document doc = new Document();
doc.loadFromFile(input);
//ArrayList<Field>のオブジェクトを作成する
ArrayList<Field> hyperlinks = new ArrayList<Field>();
String hyperlinkText = "";
String hyperlinkAddress = "";
//セクション内のアイテムをループして、すべてのハイパーリンクを検索する
for (Section section : (Iterable<Section>) doc.getSections()) {
for (DocumentObject object : (Iterable<DocumentObject>) section.getBody().getChildObjects()) {
if (object.getDocumentObjectType().equals(DocumentObjectType.Paragraph)) {
Paragraph paragraph = (Paragraph) object;
for (DocumentObject cObject : (Iterable<DocumentObject>) paragraph.getChildObjects()) {
if (cObject.getDocumentObjectType().equals(DocumentObjectType.Field)) {
Field field = (Field) cObject;
if (field.getType().equals(FieldType.Field_Hyperlink)) {
hyperlinks.add(field);
//すべてのハイパーリンクのテキストとリンクを取得する
hyperlinkText += field.getFieldText() + "\r\n";
hyperlinkAddress += field.getValue() + "\r\n";
}
}
}
}
}
}
//テキストとリンクをTXTに保存する
String output = "result.txt";
writeStringToText("Text:\r\n " + hyperlinkText + "\r\n" + "Link:\r\n" + hyperlinkAddress + "\r\n", output);
}
//TXTにテキストとリンクを書き込む方法を作成する
public static void writeStringToText(String content, String textFileName) throws IOException {
File file = new File(textFileName);
if (file.exists())
{
file.delete();
}
FileWriter fWriter = new FileWriter(textFileName, true);
try {
fWriter.write(content);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
ハイパーリンクは通常テキストに表示されます。 ハイパーリンクをクリックすると、ウェブサイト、ドキュメント、電子メール、またはその他の要素にアクセスできます。Word ドキュメントの中には、特にインターネットから生成されたものには、広告のような不快なハイパーリンクが含まれていることがあります。この記事では、Spire.Doc for Java を使用して、Word ドキュメントから特定のハイパーリンクまたはすべてのハイパーリンクを削除する方法を説明します。
まず、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 ドキュメントから指定したハイパーリンクを削除する詳細な手順は以下のとおりです。
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);
}
}
Word ドキュメントからすべてのハイパーリンクを削除する詳細な手順は次のとおりです。
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);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
Word 文書におけるハイパーリンクは、通常、青い書体に下線を付けて表示されます。実は、ハイパーリンクの既定のスタイルが気に入らない場合、リンクを維持したままハイパーリンクの色を変更したり、下線を削除して独自のハイパーリンクのスタイルにすることができます。この記事では、Spire.Doc for Java を使用して、Word のハイパーリンクの色を変更したり、下線を削除したりする方法を紹介します。
まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://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>
詳細な手順は以下の通りです。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class ChangeHyperlink {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document document = new Document();
//セクションを追加する
Section section = document.addSection();
//段落を追加する
Paragraph para= section.addParagraph();
para.appendText("普通のハイパーリンク: ");
//ハイパーリンクを挿入する
TextRange textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
textRange.getCharacterFormat().setFontName("Times New Roman");
textRange.getCharacterFormat().setFontSize(12f);
para.appendBreak(BreakType.Line_Break);
//段落を追加する
para = section.addParagraph();
para.appendText("このハイパーリンクの色を変更する: ");
//ハイパーリンクを挿入する
textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
textRange.getCharacterFormat().setFontName("Times New Roman");
textRange.getCharacterFormat().setFontSize(12f);
//ハイパーリンクの色を赤に変更する
textRange.getCharacterFormat().setTextColor(Color.RED);
para.appendBreak(BreakType.Line_Break);
//段落を追加する
para = section.addParagraph();
para.appendText("このハイパーリンクの下線を削除する: ");
//ハイパーリンクを挿入する
textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
textRange.getCharacterFormat().setFontName("Times New Roman");
textRange.getCharacterFormat().setFontSize(12f);
//ハイパーリンクの下線を削除する
textRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);
//ドキュメントを保存する
document.saveToFile("ハイパーリンクのスタイルの変更.docx", FileFormat.Docx_2013);
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。