1. 程式人生 > >C# 如何添加文本和圖片超鏈接到Word

C# 如何添加文本和圖片超鏈接到Word

.net c# 超鏈接

超鏈接簡單來講就是內容鏈接,通過設置超鏈接可以實現對象與網頁、站點之間的連接。鏈接目標可以是網頁、圖片、郵件地址、文件夾或者是應用程序。設置鏈接的對象可以是文本或者圖片。

在以下內容中,我將介紹如何用C#編程語言對Word文檔中的文本和圖片進行超鏈接設置。執行該操作需要使用免費版組件Spire.Doc for. NET,可在這裏下載安裝(https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-NET.html


1.添加文本超鏈接

步驟一創建一個Document實例並添加section

Document doc = newDocument();
Section section =doc.AddSection();

步驟二:添加指向網址的超鏈接

Paragraph para1 = section.AddParagraph();
para1.AppendHyperlink("www.google.com","www.google.com",HyperlinkType.WebLink);

步驟三:添加指向郵件地址的超鏈接

Paragraph para2 = section.AddParagraph();
para2.AppendHyperlink("mailto:[email protected]", "[email protected]", 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.添加圖片超鏈接

步驟一:創建一個Document實例並添加section

Document doc = newDocument();
Section section =doc.AddSection();

步驟二:添加段落

Paragraph para = section.AddParagraph();

步驟三:添加圖片到段落並插入網站鏈接

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#編程語言對如何添加文本及圖片超鏈接的代碼操作演示,希望上述方法對你有所幫助,感謝閱讀!

本文出自 “mochou” 博客,請務必保留此出處http://miayo.blog.51cto.com/13270370/1971707

C# 如何添加文本和圖片超鏈接到Word