• 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);
  • 相关阅读:
    uniapp 高度铺满全屏
    CSS其他属性
    30岁之前一定要明白的道理
    Java毕设项目——书画拍卖网站(java+SSM+Maven+Mysql+Jsp)
    Kafka/Zookeeper集群搭建
    pytorch训练好的模型在加载和保存过程中的问题
    Spring底层原理学习笔记--第三讲--(bean生命周期与模板方法)
    程序员面试代码
    完美十进制数——去年天梯校赛
    【AI】Interesting Applications
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126049506