超連結簡單來講就是内容連結,通過設定超連結可以實作對象與網頁、站點之間的連接配接。連結目标可以是網頁、圖檔、郵件位址、檔案夾或者是應用程式。設定連結的對象可以是文本或者圖檔。
在以下内容中,我将介紹如何用C#程式設計語言對Word文檔中的文本和圖檔進行超連結設定。執行該操作需要使用免費版元件Spire.Doc for. NET,可在這裡下載下傳安裝(https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-NET.html)
1.添加文本超連結
步驟一:建立一個Document執行個體并添加Section
Document doc = new Document();
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 doc = new Document();
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#程式設計語言對如何添加文本及圖檔超連結的代碼操作示範,希望上述方法對你有所幫助,感謝閱讀!