• Word格式处理控件Aspose.Words for .NET教程——使用超链接和HTML


    Aspose.Words for .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

    >>Aspose.Words for .NET已经更新至最新版,添加了新节点以处理多节结构化文档标签,改进了SmartArt冷渲染的性能,RevisionOptions类扩展了新的属性

                                                       ​​​​​aspose.words 最新版点击下载体验

    了解更多Aspose文档管理产品  Aspose技术交流群(761297826)


    插入超链接

    用于DocumentBuilder.InsertHyperlink 在文档中插入超链接。此方法接受三个参数:要在文档中显示的链接的文本,链接目标(URL或文档中书签的名称)以及布尔值参数(如果URL是其中的书签名称,则应为true)文件。DocumentBuilder.InsertHyperlink 内部调用 DocumentBuilder.InsertField。该方法始终在URL的开头和结尾添加撇号。请注意,需要使用该Font 属性为超链接显示文本指定字体格式。下面的示例使用DocumentBuilder将超链接插入文档中。

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    
    builder.Write("Please make sure to visit ");
    
    // Specify font formatting for the hyperlink.
    builder.Font.Color = Color.Blue;
    builder.Font.Underline = Underline.Single;
    // Insert the link.
    builder.InsertHyperlink("Aspose Website", "http://www.aspose.com", false);
    
    // Revert to default formatting.
    builder.Font.ClearFormatting();
    
    builder.Write(" for more information.");
    dataDir = dataDir + "DocumentBuilderInsertHyperlink_out.doc";
    doc.Save(dataDir);

    替换或修改超链接

    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);

    插入HTML

    您可以轻松地将包含HTML片段或整个HTML文档的HTML字符串插入Word文档。只需将此字符串传递给 DocumentBuilder.InsertHtml 方法即可。该方法的有用实现之一是在邮件合并期间将HTML字符串存储在数据库中并将其插入文档中,以获取添加的格式化内容,而不是使用文档构建器的各种方法来构建它。下面的示例显示使用DocumentBuilder将HTML插入文档中。

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    
    builder.InsertHtml(
        "

    Paragraph right

    " + "Implicit paragraph left" + "
    Div center
    " + "

    Heading 1 left.

    "); dataDir = dataDir + "DocumentBuilderInsertHtml_out.doc"; doc.Save(dataDir);
  • 相关阅读:
    scapy sniff与command方法
    布隆过滤器和布谷鸟过滤器
    C语言学习笔记(十)
    RNA 20. SCI 文章中单样本免疫浸润分析 (ssGSEA)
    二十、java版 SpringCloud分布式微服务云架构之Java 异常处理
    手动安装Ruby 1.9.3并升级RubyGems
    2023数学建模国赛B题完整论文来啦!(含一二问求解代码及三四问仿真模拟代码)
    xshell---git上传文件到gitee远程仓库配置
    HTML那些重要的知识点
    使用 Dify 和 AWS Bedrock 玩转 Anthropic Claude 3
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126049506