翻譯|使用教程|編輯:李顯亮|2019-07-25 10:11:41.470|閱讀 636 次
概述:Spire.Doc for .NET是一個專業(yè)的Word .NET庫,設(shè)計用于幫助開發(fā)人員高效地開發(fā)創(chuàng)建、閱讀、編寫、轉(zhuǎn)換和打印任何來自.NET平臺的Word文檔文件的功能。本篇文章介紹了如何添加和刪除word中的超鏈接。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.Doc for .NET是一個專業(yè)的Word .NET庫,設(shè)計用于幫助開發(fā)人員高效地開發(fā)創(chuàng)建、閱讀、編寫、轉(zhuǎn)換和打印任何來自.NET( C#, VB.NET, ASP.NET)平臺的Word文檔文件的功能。
本系列教程將為大家?guī)?strong>Spire.Doc for .NET在使用過程中的各類實際操作,本篇文章介紹了如何添加和刪除word中的超鏈接。
超鏈接指的是在Word文本或者圖片中插入能跳轉(zhuǎn)到其他位置或?qū)ο蟮逆溄樱R姷某溄涌?以鏈接到網(wǎng)址、電子郵箱地址、外部文件和書簽。
添加文本超鏈接
//創(chuàng)建一個Document實例并添加section Document doc = new Document(); Section section = doc.AddSection(); //添加指向網(wǎng)址的超鏈接 Paragraph para1 = section.AddParagraph(); para1.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com",HyperlinkType.WebLink); //添加指向郵件地址的超鏈接 Paragraph para2 = section.AddParagraph(); para2.AppendHyperlink("mailto:support @ e-iceblue.com", "support @ e-iceblue.com",HyperlinkType.EMailLink); //添加指向外部文件的超鏈接 Paragraph para3 = section.AddParagraph(); string filePath = @"C:\Users\Administrator\Desktop\月銷售統(tǒng)計表.xls"; para3.AppendHyperlink(filePath, "點擊此處打開另一個文檔",HyperlinkType.FileLink); //設(shè)置段落之間的間距 para1.Format.AfterSpacing = 15f; para2.Format.AfterSpacing = 15f; //保存文檔 doc.SaveToFile("文本超鏈接.docx", FileFormat.Docx2013);
添加圖片超鏈接
//創(chuàng)建一個Document實例并添加section Document doc = new Document(); Section section = doc.AddSection(); //添加段落 Paragraph para = section.AddParagraph(); //添加圖片到段落并插入網(wǎng)站鏈接 Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png"); Spire.Doc.Fields.DocPicture picture = para.AppendPicture(image); para.AppendHyperlink("www.e-iceblue.com", picture, HyperlinkType.WebLink); //保存文檔 doc.SaveToFile("圖片超鏈接.docx", FileFormat.Docx2013);
格式化文本超鏈接
默認狀態(tài)下,超文本鏈接通常顯示為藍色帶下劃線。為了讓超鏈接更醒目,我們也可以更改超鏈接的顯示形式,例如去掉下劃線、改變文本顏色、設(shè)置字體大小。
//創(chuàng)建一個Document實例并添加section Document doc = new Document(); Section section = doc.AddSection(); //添加段落 Paragraph para = section.AddParagraph(); //添加超鏈接到段落并返回到一個TextRange對象 TextRange txtRange = para.AppendHyperlink("www.e-iceblue.com", "WWW.E-ICEBLUE.COM", HyperlinkType.WebLink); //在TextRange中設(shè)置字體名字、大小、顏色、樣式 txtRange.CharacterFormat.FontName = "黑體"; txtRange.CharacterFormat.TextColor = Color.Purple; txtRange.CharacterFormat.FontSize = 15; txtRange.CharacterFormat.Bold = true; txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None; //保存文檔 doc.SaveToFile("格式化超鏈接.docx", FileFormat.Docx2013);
源文檔:
//創(chuàng)建Word對象并加載文檔 Document document = new Document(); document.LoadFromFile(@"hyperlinks.docx"); foreach (Section section in document.Sections) { //刪除正文里的超鏈接 foreach (DocumentObject obj in section.Body.ChildObjects) { RemoveLinks(obj,document); } //刪除頁眉頁腳中的超鏈接 foreach (HeaderFooter hf in section.HeadersFooters) { foreach (DocumentObject hfobj in hf.ChildObjects) { RemoveLinks(hfobj, document); } } } //保存文檔 document.SaveToFile("RemoveLinks.docx",FileFormat.Docx); private static void RemoveLinks(DocumentObject obj,Document document) { //刪除段落中的超鏈接 RemoveLinksInPara(obj,document); //刪除表格中的超鏈接 if (obj.DocumentObjectType == DocumentObjectType.Table) { foreach (TableRow row in (obj as Table).Rows) { foreach (TableCell cell in row.Cells) { foreach (DocumentObject cobj in cell.ChildObjects) { RemoveLinksInPara(cobj,document); } } } } } private static void RemoveLinksInPara(DocumentObject obj,Document document) { if (obj.DocumentObjectType == DocumentObjectType.Paragraph) { var objs = (obj as Paragraph).ChildObjects; for (int i = 0; i < objs.Count; i++) { if (objs[i].DocumentObjectType == DocumentObjectType.Field) { //獲取超鏈接域 Field field = objs[i] as Field; if (field.Type == FieldType.FieldHyperlink) { //獲取超鏈的文本或圖片對象 DocumentObject dObj = field.NextSibling.NextSibling as DocumentObject; //刪除文本超鏈接,保留文本和樣式 if (dObj is TextRange) { //獲取超鏈接文本樣式 CharacterFormat format = (dObj as TextRange).CharacterFormat; format.UnderlineStyle = UnderlineStyle.None; format.TextColor = Color.Black; //創(chuàng)建TextRange并把超鏈接的文本賦給它 TextRange tr = new TextRange(document); tr.Text = field.FieldText; //應(yīng)用樣式 tr.ApplyCharacterFormat(format); //刪除文本超鏈接域 objs.RemoveAt(i); //重新插入文本 objs.Insert(i, tr); } //刪除圖片超鏈接,保留圖片 if (dObj is DocPicture) { //刪除圖片超鏈接域 objs.RemoveAt(i); //重新插入圖片 objs.Insert(i, dObj); } } } } } }
結(jié)果
*購買Spire.Doc正版授權(quán)的朋友可以點擊哦~~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: