精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網(wǎng)站建設(shè)資訊

NEWS

網(wǎng)站建設(shè)資訊

C#插入、格式化、刪除Word超鏈接-創(chuàng)新互聯(lián)

超鏈接簡(jiǎn)單來(lái)講就是內(nèi)容鏈接,通過(guò)設(shè)置超鏈接可以實(shí)現(xiàn)對(duì)象與網(wǎng)頁(yè)、站點(diǎn)之間的連接。鏈接目標(biāo)可以是網(wǎng)頁(yè)、圖片、郵件地址、文件夾或者是應(yīng)用程序。設(shè)置鏈接的對(duì)象可以是文本或者圖片。在下面的示例中,將講述如何通過(guò)使用類(lèi)庫(kù)來(lái)添加Word超鏈接。同理,我們也可以格式化超鏈接,例如,設(shè)置超鏈接文本顏色,下劃線,鏈接地址等,也可以刪除文檔中已經(jīng)存在的一些超鏈接,例如:頁(yè)眉處的鏈接、正文段落中的鏈接、表格中的鏈接、圖片中的鏈接。以上操作我們都可以通過(guò)借助下面的類(lèi)庫(kù)來(lái)實(shí)現(xiàn)。
內(nèi)容要點(diǎn):

創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供武義企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都做網(wǎng)站、成都網(wǎng)站建設(shè)、HTML5、小程序制作等業(yè)務(wù)。10年已為武義眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)的建站公司優(yōu)惠進(jìn)行中。
  • 添加Word超鏈接
  • 格式化Word超鏈接
  • 刪除Word超鏈接
    工具使用:
  • Free Spire.Doc for .NET 6.3 (社區(qū)版)

    1. 添加Word超鏈接

    1.1 添加文本鏈接
    步驟 1 :添加using指令

using System;
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;

步驟 2 :創(chuàng)建文檔

//創(chuàng)建一個(gè)Document實(shí)例并添加section
Document doc = new Document();
Section section = doc.AddSection();

步驟 3:根據(jù)需要設(shè)置鏈接到不同對(duì)象的超鏈接

//添加指向網(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, "點(diǎn)擊打開(kāi)文檔", HyperlinkType.FileLink);

步驟 4 :設(shè)置段間距

para1.Format.AfterSpacing = 15f;
para2.Format.AfterSpacing = 15f;

步驟 5 :保存文件

doc.SaveToFile("文本超鏈接.docx", FileFormat.Docx2013);

完成代碼后,調(diào)試運(yùn)行程序,生成穩(wěn)定,如下所示:
C# 插入、格式化、刪除Word超鏈接

全部代碼如下:

using System;
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;

namespace Insert_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            //創(chuàng)建一個(gè)Document實(shí)例并添加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, "點(diǎn)擊打開(kāi)文檔", HyperlinkType.FileLink);

            //設(shè)置段落之間的間距 
            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 :創(chuàng)建文檔

Document doc = new Document();
Section section = doc.AddSection();
Paragraph para = section.AddParagraph();

步驟 3 :添加鏈接到圖片

//添加圖片到段落并插入網(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);

步驟 4 :保存文檔

doc.SaveToFile("圖片超鏈接.docx", FileFormat.Docx2013);

測(cè)試效果:
C# 插入、格式化、刪除Word超鏈接

2. 設(shè)置超鏈接格式

一般情況下,對(duì)文本設(shè)置超鏈接都是默認(rèn)的藍(lán)色字體,帶有下劃線,在下面的操作中,我們可以自行設(shè)置超鏈接的文本字體、字號(hào)、顏色、下劃線等。
全部代碼:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace FormatHyperlink
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化一個(gè)Docuemtn類(lèi)對(duì)象,并添加section
            Document document = new Document();
            Section section = document.AddSection();

            //添加段落,并設(shè)置超鏈接文本和鏈接網(wǎng)址。設(shè)置字體、字號(hào)、字體顏色、下劃線等。
            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;
            //保存并打開(kāi)文檔
            document.SaveToFile("result1.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result1.docx");
        }
    }
}

測(cè)試效果:
C# 插入、格式化、刪除Word超鏈接

3. 刪除超鏈接

下面的測(cè)試文檔中,多處文檔內(nèi)容包含超鏈接,包括頁(yè)眉處的文字超鏈接、正文段落中的文字超鏈接、表格中的圖片超鏈接等,可通過(guò)下面的代碼將超鏈接刪除。
測(cè)試文檔:
C# 插入、格式化、刪除Word超鏈接

全部代碼步驟:

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)
        {
            //創(chuàng)建Word對(duì)象并加載文檔
            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);
                }

                //刪除頁(yè)眉頁(yè)腳中的超鏈接
                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)        
         {
            //遍歷文檔段落中所有子對(duì)象
             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)
                       {
                           //獲取超鏈接的文本或圖片對(duì)象
                           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
                               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);
                              }
                          }
                      }
                  }
              }
         }
    }
}

測(cè)試效果:
C# 插入、格式化、刪除Word超鏈接

以上是本次關(guān)于“C#操作Word超鏈接的方法”的全部?jī)?nèi)容。

如需轉(zhuǎn)載,請(qǐng)注明出處!!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


名稱欄目:C#插入、格式化、刪除Word超鏈接-創(chuàng)新互聯(lián)
URL標(biāo)題:http://m.jcarcd.cn/article/ccipds.html
主站蜘蛛池模板: 乱子伦国| 精品自拍视频曝光 | 国自产拍91 | 国内精自线i | 国产精品国产 | 日本黄页网站大 | 国产一区在线免费 | 国精产品水蜜桃 | 精品一线二线在线 | 国产放荡对白 | 国产精品冒白 | 日韩夜间飞 | 国产在线视频琪琪 | 欧美亚洲综合色 | 91国在线精品 | 欧洲亚洲国产 | 日韩欧美国产师 | 欧美在线综合 | 91福利社区试看 | 国产精品第一页 | 欧美自拍日韩高清 | 97在线人人 | 午夜在线观看亚 | 国产精品一线 | 91国产精品 | www在线黄| 精品人无 | 91精品久 | 国产亚州 | 国产原创精品在线 | 97福利视| 精品久爱| 中文字幕在线不卡 | 日韩午夜视频 | 欧洲一卡2 | 97超级碰碰碰电影 | 成人免费福利片 | 福利影院在线播放 | 国产精品三级三级 | 欧美日韩一区在线 | 91看片婬黄大片欧 |