C# 添加Word文本和圖片超鏈接的方法
超鏈接簡單來講就是內容鏈接,通過設置超鏈接可以實現(xiàn)對象與網(wǎng)頁、站點之間的連接。鏈接目標可以是網(wǎng)頁、圖片、郵件地址、文件夾或者是應用程序。設置鏈接的對象可以是文本或者圖片。
在以下內容中,我將介紹如何用C#編程語言對Word文檔中的文本和圖片進行超鏈接設置。執(zhí)行該操作需要使用免費版組件Spire.Doc for. NET,可在這里下載安裝(https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-NET.html)
1.添加文本超鏈接
步驟一:創(chuàng)建一個Document實例并添加Section
Document doc = new Document(); Section section = doc.AddSection();
步驟二:添加指向網(wǎng)址的超鏈接
Paragraph para1 = section.AddParagraph(); para1.AppendHyperlink("www.google.com","www.google.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\2017NobelPrize.docx"; para3.AppendHyperlink(filePath, "點擊打開文檔", HyperlinkType.FileLink);
步驟五:設置段落之間的間距
para1.Format.AfterSpacing = 15f; para2.Format.AfterSpacing = 15f;
步驟六:保存文檔
doc.SaveToFile("文本超鏈接.docx", FileFormat.Docx2013);
完成操作步驟后,運行該項目生成文件,如下圖所示:
2、添加圖片超鏈接
步驟一:創(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\images\Google.jpg"); Spire.Doc.Fields.DocPicture picture = para.AppendPicture(image); para.AppendHyperlink("www.google.com", picture, HyperlinkType.WebLink);
步驟四:保存文檔
doc.SaveToFile("圖片超鏈接.docx", FileFormat.Docx2013);
完成操作步驟,運行程序得到如下文件:
總結
以上所述是小編給大家介紹的C# 添加Word文本和圖片超鏈接的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
C#實現(xiàn)HTML轉WORD及WORD轉PDF的方法
這篇文章主要介紹了C#實現(xiàn)HTML轉WORD及WORD轉PDF的方法,涉及C#實現(xiàn)HTML、WORD及PDF等文件格式轉換的相關技巧,需要的朋友可以參考下2015-09-09