Aspose.Words for .NET如何替換或修改超連結
在當前版本,沒有在Aspose.Words
嵌入功能來處理超連結欄位。
超連結在Microsoft Word
文件是欄位,一個欄位包含欄位程式碼和欄位結果,在當前版本的Aspose.Words
中,沒有單一的物件代表了一個欄位。
以下這個例子展示瞭如何建立一個簡單的類,它代表一個文件中的超連結。它的建構函式接受一個FieldStart
物件,這個物件必須有FieldType.FieldHyperlink
型別。使用超連結類後,您可以獲取或設定跳轉目標地址,名字和IsLocal
屬性。現在很容易在整個文件改變超連結目標和名稱。在這個例子中,所有的超連結都改為“http://aspose.com”。
Example
找到一個Word
文件所有超連結和改變他們的URL
和顯示名稱。
using System;
using System.Text;
using System.Text.RegularExpressions;
using Aspose.Words;
using Aspose.Words.Fields;
namespace Examples
{
/// <summary>
/// Shows how to replace hyperlinks in a Word document.
/// </summary>
public class ExReplaceHyperlinks : ExBase
{
/// <summary>
/// Finds all hyperlinks in a Word document and changes their URL and display name.
/// </summary>
public void ReplaceHyperlinks()
{
// Specify your document name here.
Document doc = new Document(MyDir + "ReplaceHyperlinks.doc");
// Hyperlinks in a Word documents are fields, select all field start nodes so we can find the hyperlinks.
NodeList fieldStarts = doc.SelectNodes("//FieldStart");
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType.Equals(FieldType.FieldHyperlink))
{
// The field is a hyperlink field, use the "facade" class to help to deal with the field.
Hyperlink hyperlink = new Hyperlink(fieldStart);
// Some hyperlinks can be local (links to bookmarks inside the document), ignore these.
if (hyperlink.IsLocal)
continue;
// The Hyperlink class allows to set the target URL and the display name
// of the link easily by setting the properties.
hyperlink.Target = NewUrl;
hyperlink.Name = NewName;
}
}
doc.Save(MyDir + "ReplaceHyperlinks Out.doc");
}
private const string NewUrl = @"http://www.aspose.com";
private const string NewName = "Aspose - The .NET & Java Component Publisher";
}
/// <summary>
/// This "facade" class makes it easier to work with a hyperlink field in a Word document.
///
/// A hyperlink is represented by a HYPERLINK field in a Word document. A field in Aspose.Words
/// consists of several nodes and it might be difficult to work with all those nodes directly.
/// Note this is a simple implementation and will work only if the hyperlink code and name
/// each consist of one Run only.
///
/// [FieldStart][Run - field code][FieldSeparator][Run - field result][FieldEnd]
///
/// The field code contains a string in one of these formats:
/// HYPERLINK "url"
/// HYPERLINK \l "bookmark name"
///
/// The field result contains text that is displayed to the user.
/// </summary>
internal class Hyperlink
{
internal Hyperlink(FieldStart fieldStart)
{
if (fieldStart == null)
throw new ArgumentNullException("fieldStart");
if (!fieldStart.FieldType.Equals(FieldType.FieldHyperlink))
throw new ArgumentException("Field start type must be FieldHyperlink.");
mFieldStart = fieldStart;
// Find the field separator node.
mFieldSeparator = fieldStart.GetField().Separator;
if (mFieldSeparator == null)
throw new InvalidOperationException("Cannot find field separator.");
mFieldEnd = fieldStart.GetField().End;
// Field code looks something like [ HYPERLINK "http:\\www.myurl.com" ], but it can consist of several runs.
string fieldCode = fieldStart.GetField().GetFieldCode();
Match match = gRegex.Match(fieldCode.Trim());
mIsLocal = (match.Groups[1].Length > 0); //The link is local if \l is present in the field code.
mTarget = match.Groups[2].Value;
}
/// <summary>
/// Gets or sets the display name of the hyperlink.
/// </summary>
internal string Name
{
get
{
return GetTextSameParent(mFieldSeparator, mFieldEnd);
}
set
{
// Hyperlink display name is stored in the field result which is a Run
// node between field separator and field end.
Run fieldResult = (Run)mFieldSeparator.NextSibling;
fieldResult.Text = value;
// But sometimes the field result can consist of more than one run, delete these runs.
RemoveSameParent(fieldResult.NextSibling, mFieldEnd);
}
}
/// <summary>
/// Gets or sets the target url or bookmark name of the hyperlink.
/// </summary>
internal string Target
{
get
{
string dummy = null; // This is needed to fool the C# to VB.NET converter.
return mTarget;
}
set
{
mTarget = value;
UpdateFieldCode();
}
}
/// <summary>
/// True if the hyperlink's target is a bookmark inside the document. False if the hyperlink is a url.
/// </summary>
internal bool IsLocal
{
get
{
return mIsLocal;
}
set
{
mIsLocal = value;
UpdateFieldCode();
}
}
private void UpdateFieldCode()
{
// Field code is stored in a Run node between field start and field separator.
Run fieldCode = (Run)mFieldStart.NextSibling;
fieldCode.Text = string.Format("HYPERLINK {0}\"{1}\"", ((mIsLocal) ? "\\l " : ""), mTarget);
// But sometimes the field code can consist of more than one run, delete these runs.
RemoveSameParent(fieldCode.NextSibling, mFieldSeparator);
}
/// <summary>
/// Retrieves text from start up to but not including the end node.
/// </summary>
private static string GetTextSameParent(Node startNode, Node endNode)
{
if ((endNode != null) && (startNode.ParentNode != endNode.ParentNode))
throw new ArgumentException("Start and end nodes are expected to have the same parent.");
StringBuilder builder = new StringBuilder();
for (Node child = startNode; !child.Equals(endNode); child = child.NextSibling)
builder.Append(child.GetText());
return builder.ToString();
}
/// <summary>
/// Removes nodes from start up to but not including the end node.
/// Start and end are assumed to have the same parent.
/// </summary>
private static void RemoveSameParent(Node startNode, Node endNode)
{
if ((endNode != null) && (startNode.ParentNode != endNode.ParentNode))
throw new ArgumentException("Start and end nodes are expected to have the same parent.");
Node curChild = startNode;
while ((curChild != null) && (curChild != endNode))
{
Node nextChild = curChild.NextSibling;
curChild.Remove();
curChild = nextChild;
}
}
private readonly Node mFieldStart;
private readonly Node mFieldSeparator;
private readonly Node mFieldEnd;
private bool mIsLocal;
private string mTarget;
/// <summary>
/// RK I am notoriously bad at regexes. It seems I don't understand their way of thinking.
/// </summary>
private static readonly Regex gRegex = new Regex(
"\\S+" + // one or more non spaces HYPERLINK or other word in other languages
"\\s+" + // one or more spaces
"(?:\"\"\\s+)?" + // non capturing optional "" and one or more spaces, found in one of the customers files.
"(\\\\l\\s+)?" + // optional \l flag followed by one or more spaces
"\"" + // one apostrophe
);
}
}
相關文章
- Aspose.Words使用教程之在文件中找到並替換文字
- 網站程式碼修改替換流程圖,輕鬆掌握程式碼修改替換流程網站流程圖
- 公司網站連結被替換怎麼辦?網站
- cad.net HandOverTo替換物件物件
- 正則替換 修改字元 去除空格字元
- 公司網站連結錯誤?公司網站域名替換網站
- 網站圖修改後怎麼替換網站
- C#解析Markdown文件,實現替換圖片連結操作C#
- 使用列舉ENUM替換Switch或If-Else
- .NET正則替換URL引數值
- Highcharts的credits配置去掉或修改“Highcharts.com”連結標籤
- Asp.Net分頁生成頁碼超連結方法ASP.NET
- 使用Aspose.Words元件進行word文件書籤替換,文件、圖表插入,轉pdf等元件
- 【OracleSQL】常用自動替換總結OracleSQL
- 定積分之換元積分法公式(或稱變數替換法)公式變數
- linux-如何快速替換IPLinux
- Python 在PDF中新增、替換、或刪除圖片Python
- Asp.Net MVC路由引數獲取、替換ASP.NETMVC路由
- 如何給 Table/tr/td 新增超連結?
- HTML 替換元素與非替換元素HTML
- ln 超連結
- php網站修改備案號連結地址,如何在PHP網站中修改備案號連結地址PHP網站
- Python 在Excel中插入、替換、提取、或刪除圖片PythonExcel
- 如何快速替換SOLIDWORKS工程圖模板Solid
- 教你如何用 openresty 完美替換 nginxRESTNginx
- python 檔案操作(二) 替換性修改檔案內容Python
- mysql修改某個欄位(替換關鍵字內容)MySql
- Vi替換
- 替換空格
- 【HTML】03超連結HTML
- 怎樣替換公司網站?網站後臺密碼修改網站密碼
- Z-Blog內鏈關鍵字替換外掛的修改
- Polardb 如何替換MYSQL 之 IMCI 列式攻略MySql
- 教你如何替換@PathVariable中的變數變數
- LayerZero 如何替換IBC的傳輸層
- 我是如何替換Spring Cloud Netflix的?SpringCloud
- Java 操作PDF中的超連結——新增、更新、刪除超連結Java
- 如何刪除Word文件中的全部超連結