ハイパーリンクの追加または変更
Microsoft Wordドキュメント内のハイパーリンクはHYPERLINK
フィールドです。 Aspose.Wordsでは、ハイパーリンクはFieldHyperlinkクラスを介して実装されます。
ハイパーリンクの挿入
ドキュメントにハイパーリンクを挿入するには、InsertHyperlinkメソッドを使用します。 このメソッドは、次の3つのパラメーターを受け入れます:
- ドキュメントに表示されるリンクのテキスト
- リンク先(文書内のurlまたはブックマークの名前)
URL
がドキュメント内のブックマークの名前である場合にtrueにする必要があるBooleanパラメータ
InsertHyperlinkメソッドは、常にURLの先頭と末尾にアポストロフィを追加します。
Font
プロパティを使用して、ハイパーリンク表示テキストのフォント書式を明示的に指定する必要があることに注意してください。
次のコード例は、DocumentBuilderを使用してドキュメントにハイパーリンクを挿入する方法を示しています:
//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
builder->Write(u"Please make sure to visit "); | |
builder->get_Font()->set_Color(System::Drawing::Color::get_Blue()); | |
builder->get_Font()->set_Underline(Underline::Single); | |
builder->InsertHyperlink(u"Aspose Website", u"http://www.aspose.com", false); | |
builder->get_Font()->ClearFormatting(); | |
builder->Write(u" for more information."); | |
doc->Save(ArtifactsDir + u"AddContentUsingDocumentBuilder.InsertHyperlink.docx"); |
ハイパーリンクの置換または変更
Microsoft Word文書内のハイパーリンクはフィールドです。 Word文書のフィールドは、フィールドの開始、フィールドコード、フィールド区切り文字、フィールドの結果、フィールドの終了を含む複数のノードで構成される複雑な構 フィールドは入れ子にすることができ、リッチコンテンツを含み、ドキュメント内の複数の段落またはセクションにまたがることができます。
FieldHyperlink
クラスはHYPERLINK
フィールドを実装します。
次のコード例は、Word文書内のすべてのハイパーリンクを検索し、それらのURL
と表示名を変更する方法を示しています:
//For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = MakeObject<Document>(MyDir + u"Hyperlinks.docx"); | |
for (const auto& field : System::IterateOver(doc->get_Range()->get_Fields())) | |
{ | |
if (field->get_Type() == FieldType::FieldHyperlink) | |
{ | |
auto hyperlink = System::DynamicCast<FieldHyperlink>(field); | |
// Some hyperlinks can be local (links to bookmarks inside the document), ignore these. | |
if (hyperlink->get_SubAddress() != nullptr) | |
{ | |
continue; | |
} | |
hyperlink->set_Address(u"http://www.aspose.com"); | |
hyperlink->set_Result(u"Aspose - The .NET & Java Component Publisher"); | |
} | |
} | |
doc->Save(ArtifactsDir + u"WorkingWithFields.ReplaceHyperlinks.docx"); |