Aspose.Words for .NET使用教程——如何替换或修改超链接
Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
接下来我们将在Aspose.Words中学会如何替换或修改超链接。
替换或修改超链接
Microsoft Word文档中的超链接是一个字段。Word文档中的字段是一个复杂的结构,由多个节点组成,这些节点包括字段开头,字段代码,字段分隔符,字段结果和字段结尾。字段可以嵌套,包含丰富的内容并跨越文档中的多个段落或部分。
FieldHyperlink类实现HYPERLINK字段。 下面的示例查找Word文档中的所有超链接,并更改其URL和显示名称。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithHyperlink();
string NewUrl = @"http://www.aspose.com";
string NewName = "Aspose - The .NET & Java Component Publisher";
Document doc = new Document(dataDir + "ReplaceHyperlinks.doc");
// Hyperlinks in a Word documents are fields.
foreach (Field field in doc.Range.Fields)
{
if (field.Type == FieldType.FieldHyperlink)
{
FieldHyperlink hyperlink = (FieldHyperlink)field;
// Some hyperlinks can be local (links to bookmarks inside the document), ignore these.
if (hyperlink.SubAddress != null)
continue;
hyperlink.Address = NewUrl;
hyperlink.Result = NewName;
}
}
dataDir = dataDir + "ReplaceHyperlinks_out.doc";
doc.Save(dataDir);
用静态文本替换字段
当希望将文档另存为静态副本时,例如在电子邮件中作为附件发送时,通常需要这样做。将DATE或TIME字段等字段转换为静态文本将使它们能够显示与发送它们相同的日期。在某些情况下,可能需要从文档中删除条件IF字段,然后将其替换为最新的文本结果。例如,将IF字段的结果转换为静态文本,这样,如果文档中的字段被更新,它将不再动态更改其值。
例如,下图显示了如何将“IF”字段存储在文档中。文本包含在特殊的字段节点FieldStart和FieldEnd中。所述FieldSeparator节点分隔字段内的文本到域代码和域结果。字段代码定义字段的一般行为,而当Microsoft Word或Aspose.Words更新此字段时,字段结果将存储最新结果。字段结果是存储在字段中并在查看时显示在文档中的结果。
当然还可以使用演示项目“ DocumentExplorer”以分层形式在下面看到该结构。
请注意,此技术无法在页眉或页脚的某些字段上正确使用。例如,尝试将页眉或页脚中的PAGE字段转换为静态文本将导致相同的值出现在所有页面上。这是因为页眉和页脚在多个页面上重复,并且当它们保留为字段时,尤其要对其进行处理,以便为每个页面显示正确的结果。但是,转换后,标头中的字段将转换为静态文本行。此文本行将被评估为好像是该部分中的最后一页,这将导致标题中的PAGE字段中的任何一个在所有页面上显示最后一页。下面的代码示例演示如何用其最新结果替换该字段。
public class FieldsHelper
{
/// <summary>
/// Converts any fields of the specified type found in the descendants of the node into static text.
/// </summary>
/// <param name="compositeNode">The node in which all descendants of the specified FieldType will be converted to static text.</param>
/// <param name="targetFieldType">The FieldType of the field to convert to static text.</param>
public static void ConvertFieldsToStaticText(CompositeNode compositeNode, FieldType targetFieldType)
{
compositeNode.Range.Fields.Cast<Field>().Where(f => f.Type == targetFieldType).ToList().ForEach(f => f.Unlink());
}
}
ConvertFieldsToStaticText方法接受两个参数,一个CompositeNode和一个FieldType枚举。能够将任何复合节点传递给此方法,这样可以仅在文档的特定部分将字段转换为静态文本。例如,可以传递Document对象,并将指定类型的字段从整个文档转换为静态文本,或者可以传递节的Body对象,仅转换在该正文中找到的字段。
传递给该方法的FieldType枚举指定应将哪种类型的字段转换为静态文本。文档中遇到的任何其他类型的字段将保持不变。 下例显示了如何将文档中指定类型的所有字段转换为静态文本。您可以从此处下载以下示例的模板文件。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
string fileName = "TestFile.doc";
Document doc = new Document(dataDir + fileName);
// Pass the appropriate parameters to convert all IF fields encountered in the document (including headers and footers) to static text.
FieldsHelper.ConvertFieldsToStaticText(doc, FieldType.FieldIf);
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the document with fields transformed to disk.
doc.Save(dataDir);
下面的示例显示如何将文档正文中指定类型的所有字段转换为静态文本。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
string fileName = "TestFile.doc";
Document doc = new Document(dataDir + fileName);
// Pass the appropriate parameters to convert PAGE fields encountered to static text only in the body of the first section.
FieldsHelper.ConvertFieldsToStaticText(doc.FirstSection.Body, FieldType.FieldPage);
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the document with fields transformed to disk.
doc.Save(dataDir);
下面的示例显示如何将段落中指定类型的所有字段转换为静态文本。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithFields();
string fileName = "TestFile.doc";
Document doc = new Document(dataDir + fileName);
// Pass the appropriate parameters to convert all IF fields to static text that are encountered only in the last
// Paragraph of the document.
FieldsHelper.ConvertFieldsToStaticText(doc.FirstSection.Body.LastParagraph, FieldType.FieldIf);
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the document with fields transformed to disk.
doc.Save(dataDir);
相关文章
- ASP .NET CORE MVC 部署Windows 系统上 IIS具体步骤---.Net Core 部署到 IIS位系统中的步骤
- 使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到SUSE Linux Enterprise Server 12 SP2 64 位(GNOME 版本3.20.2)
- 使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到Ubuntu 16.04
- .Net魔法堂:史上最全的ActiveX开发教程——自动更新、卸载篇
- Asp.Net Web API 2第十三课——ASP.NET Web API中的JSON和XML序列化
- 使用.NET Framework中的对象来检索网页和处理其内容
- 重新整理asp.net core 实操篇——简介
- 重新整理 .net core 实践篇 ———— dotnet-dump [外篇]
- .NET Core的日志[4]:将日志写入EventLog
- .NET中Flags枚举的使用
- npm——安装教程、安装vue脚手架(ASP.NET Core微服务(五)——【vue脚手架解析接口】过度章节)
- Asp.net button防止点击多次数据提交
- Net Core 微服务 - 如何在docker容器里运行一个简单的.net core web api 服务
- Dynamsoft Dynamic .NET TWAIN for net Crack
- 【.Net实用方法总结】 整理并总结System.IO中TextWriter类及其方法介绍
- ASP.NET Core入门级教程_下篇_模型、持久化、文件、错误处理、日志