翻譯|使用教程|編輯:李顯亮|2020-07-13 16:17:31.480|閱讀 450 次
概述:用于 DocumentBuilder.InsertHyperlink 在文檔中插入超鏈接。在本文中將展示如何插入、替換、修改超鏈接,以及HTML。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words for .NET是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
>>Aspose.Words for .NET已經更新至v20.7,添加了新節點以處理多節結構化文檔標簽,改進了SmartArt冷渲染的性能,RevisionOptions類擴展了新的屬性,點擊下載體驗
用于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字符串插入Word文檔。只需將此字符串傳遞給 DocumentBuilder.InsertHtml 方法即可。該方法的有用實現之一是在郵件合并期間將HTML字符串存儲在數據庫中并將其插入文檔中,以獲取添加的格式化內容,而不是使用文檔構建器的各種方法來構建它。下面的示例顯示使用DocumentBuilder將HTML插入文檔中。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.InsertHtml( "<P align='right'>Paragraph right</P>" + "<b>Implicit paragraph left</b>" + "<div align='center'>Div center</div>" + "<h1 align='left'>Heading 1 left.</h1>"); dataDir = dataDir + "DocumentBuilderInsertHtml_out.doc"; doc.Save(dataDir);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn