標籤:ddc 網站 https image nts blog 檔案夾 調試 app
超連結簡單來講就是內容連結,通過設定超連結可以實現對象與網頁、網站之間的串連。連結目標可以是網頁、圖片、郵件地址、檔案夾或者是應用程式。設定連結的對象可以是文本或者圖片。在下面的樣本中,將講述如何通過使用類庫來添加Word超連結。同理,我們也可以格式化超連結,例如,設定超連結文本顏色,底線,連結地址等,也可以刪除文檔中已經存在的一些超連結,例如:頁首處的連結、本文段落中的連結、表格中的連結、圖片中的連結。以上操作我們都可以通過藉助下面的類庫來實現。
內容要點:
using System;using Spire.Doc;using System.Drawing;using Spire.Doc.Documents;
步驟 2 :建立文檔
//建立一個Document執行個體並添加sectionDocument doc = new Document();Section section = doc.AddSection();
步驟 3:根據需要設定連結到不同對象的超連結
//添加指向網址的超連結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);
步驟 4 :設定段間距
para1.Format.AfterSpacing = 15f;para2.Format.AfterSpacing = 15f;
步驟 5 :儲存檔案
doc.SaveToFile("文本超連結.docx", FileFormat.Docx2013);
完成代碼後,調試運行程式,產生穩定,如下所示:
全部代碼如下:
using System;using Spire.Doc;using System.Drawing;using Spire.Doc.Documents;namespace Insert_Word{ class Program { static void Main(string[] args) { //建立一個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); } }}
1.2 添加圖片連結
步驟 1 :添加using指令
using System;using Spire.Doc;using System.Drawing;using Spire.Doc.Documents;
步驟 2 :建立文檔
Document doc = new Document();Section section = doc.AddSection();Paragraph para = section.AddParagraph();
步驟 3 :添加連結到圖片
//添加圖片到段落並插入網站連結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);
步驟 4 :儲存文檔
doc.SaveToFile("圖片超連結.docx", FileFormat.Docx2013);
測試效果:
2. 設定超連結格式
一般情況下,對文本設定超連結都是預設的藍色字型,帶有底線,在下面的操作中,我們可以自行設定超連結的文本字型、字型大小、顏色、底線等。
全部代碼:
using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace FormatHyperlink{ class Program { static void Main(string[] args) { //初始化一個Docuemtn類對象,並添加section Document document = new Document(); Section section = document.AddSection(); //添加段落,並設定超連結文本和連結網址。設定字型、字型大小、字型顏色、底線等。 Paragraph para = section.AddParagraph(); para.AppendText("HyperLink: "); TextRange txtRange = para.AppendHyperlink("www.google.com", "www.google.com", HyperlinkType.WebLink); txtRange.CharacterFormat.FontName = "Times New Roman"; txtRange.CharacterFormat.FontSize = 14; txtRange.CharacterFormat.TextColor = System.Drawing.Color.Green; txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.None; //儲存並開啟文檔 document.SaveToFile("result1.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("result1.docx"); } }}
測試效果:
3. 刪除超連結
下面的測試文檔中,多處文檔內容包含超連結,包括頁首處的文字超連結、本文段落中的文字超連結、表格中的圖片超連結等,可通過下面的代碼將超連結刪除。
測試文檔:
全部代碼步驟:
using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using Spire.Doc.Formatting;using System.Drawing;namespace RemoveHyperlink_Doc{ class Program { static void Main(string[] args) { //建立Word對象並載入文檔 Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx"); //遍曆文檔中所有section 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); System.Diagnostics.Process.Start("RemoveLinks.docx"); }//自訂方法RemoveLinks()刪除段落、表格中的超連結 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); } } } } } //自訂方法RemoveLinksInPara()刪除文檔段落中的所有超連結 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; //建立TextRange並把超連結的文本賦予TextRange TextRange tr = new TextRange(document); tr.Text = field.FieldText; //應用樣式 tr.ApplyCharacterFormat(format); //刪除文本超連結域 objs.RemoveAt(i); //重新插入文本 objs.Insert(i, tr); } //刪除圖片超連結,保留圖片 if (dObj is DocPicture) { //刪除圖片超連結域 objs.RemoveAt(i); //重新插入圖片 objs.Insert(i, dObj); } } } } } } }}
測試效果:
以上是本次關於“C#操作Word超連結的方法”的全部內容。
如需轉載,請註明出處!!
C# 插入、格式化、刪除Word超連結