Hyperlinks are simply links to content, which allows you to connect objects to Web pages and sites by setting up hyperlinks. A link destination can be a Web page, picture, email address, folder, or application. The object that sets the link can be text or a picture. In the following example, you'll tell how to add a word hyperlink by using a class library. Similarly, we can format hyperlinks, for example, to set hyperlink text color, underline, link address, and so on, or you can delete some hyperlinks that already exist in the document, such as a link at the header, a link in a body paragraph, a link in a table, a link in a picture. We can do this by using the following class library.
Content Highlights:
using System;using Spire.Doc;using System.Drawing;using Spire.Doc.Documents;
Step 2: Create a document
//创建一个Document实例并添加sectionDocument doc = new Document();Section section = doc.AddSection();
Step 3: Set hyperlinks to different objects as needed
//添加指向网址的超链接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);
Step 4: Set the segment spacing
para1.Format.AfterSpacing = 15f;para2.Format.AfterSpacing = 15f;
Step 5: Save the file
doc.SaveToFile("文本超链接.docx", FileFormat.Docx2013);
After the code is completed, debug the run program, generating stability as follows:
The
all code is as follows:
Using system;using spire.doc;using system.drawing;using spire.doc.documents;namespace Insert_Word{class Program { static void Main (string[] args) {//Create a document instance and add section Document doc = new documen T (); Section section = Doc. AddSection (); Add a hyperlink to the URL Paragraph para1 = section. Addparagraph (); Para1. Appendhyperlink ("www.google.com", "www.google.com", Hyperlinktype.weblink); Add a hyperlink to the mail address Paragraph para2 = section. Addparagraph (); Para2. Appendhyperlink ("mailto:[email protected]", "[email protected]", hyperlinktype.emaillink); Add a hyperlink to an external file Paragraph para3 = section. Addparagraph (); String filePath = @ "C:\Users\Administrator\Desktop\2017NobelPrize.docx"; Para3. Appendhyperlink (FilePath, "Click to open Document", Hyperlinktype.filelink); Sets the spacing between paragraphs para1. format.afterspacing = 15f; Para2. Format.afterspacing = 15f; Save document Doc. SaveToFile ("text hyperlink. docx", fileformat.docx2013); } }}
1.2 Add a picture link
Step 1: Add a using directive
using System;using Spire.Doc;using System.Drawing;using Spire.Doc.Documents;
Step 2: Create a document
Document doc = new Document();Section section = doc.AddSection();Paragraph para = section.AddParagraph();
Step 3: Add links to pictures
//添加图片到段落并插入网站链接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);
Step 4: Save the document
doc.SaveToFile("图片超链接.docx", FileFormat.Docx2013);
Test results:
2. Set the hyperlink format
In general, the text is set to hyperlink is the default blue font, with an underscore, in the following operation, we can set the text font, font size, color, underline, etc. of the hyperlink.
All code:
Using spire.doc;using spire.doc.documents;using Spire.doc.fields;namespace formathyperlink{class Program {s tatic void Main (string[] args) {//Initializes a Docuemtn class object and adds section document document = new Docume NT (); Section section = document. AddSection (); Add paragraphs and set hyperlink text and link URLs. Set the font, font size, font color, underline, and so on. 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; Save and open documents document. SaveToFile ("Result1.docx", fileformat.docx2013); System.Diagnostics.Process.Start ("Result1.docx"); } }}
Test results:
3. Remove hyperlinks
In the following test document, multiple document content contains hyperlinks, including text hyperlinks at the header, text hyperlinks in body paragraphs, picture hyperlinks in tables, and so on, which can be removed by using the following code.
Test Document:
All code steps:
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) {//Create Word Object and load Document Document document = new document (); Document. LoadFromFile (@ "C:\Users\Administrator\Desktop\sample.docx"); Iterates through all the section foreach sections in the document. Sections) {//delete the hyperlink in the body of foreach (DocumentObject obj in the section. body.childobjects) {removelinks (obj, document); }//Remove the hyperlink in the header footer foreach (HeaderFooter hf in section. HeadersFooters) {foreach (DocumentObject hfobj in HF. childobjects) {removelinks (hfobj, document); }}}//Save document documents. SavetOfile ("Removelinks.docx", fileformat.docx); System.Diagnostics.Process.Start ("Removelinks.docx"); }//Custom Method Removelinks () remove a hyperlink from a paragraph, table private static void Removelinks (DocumentObject obj,document Document) {//delete hyperlink in paragraph Removelinksinpara (obj,document); Deletes a hyperlink in a table if (obj. Documentobjecttype = = documentobjecttype.table) {foreach (TableRow row in (obj as Tabl e). Rows) {foreach (TableCell cell in row. Cells) {foreach (DocumentObject cobj in cell). childobjects) {Removelinksinpara (cobj,document); } } } } }//Custom method Removelinksinpara () Delete all hyperlinks in a document paragraph private static VOID Removelinksinpara (documentobject obj,document document) {//Traverse all sub-objects in the documents paragraph if (OB J.documentobjecttype = = documentobjecttype.paragraph) {var objs = (obj as Paragraph). childobjects; for (int i = 0; i < Objs. Count; i++) {if (Objs[i]. Documentobjecttype = = Documentobjecttype.field) {//Get hyperlink field Field field = Objs[i] as field; if (field. Type = = Fieldtype.fieldhyperlink) {//Gets the text or picture object of the hyperlink DocumentObject dobj = field. Nextsibling.nextsibling as DocumentObject; Delete text hyperlinks, preserve text and style if (Dobj is TextRange) { Gets the hyperlink text style Characterformat format = (dobj as TextRange). Characterformat; Format. Underlinestyle = Underlinestyle.none; Format. TextColor = Color.Black; Create a TextRange and assign the text of the hyperlink to TextRange TextRange tr = new TextRange (document); Tr. Text = field. Fieldtext; Apply the style tr. Applycharacterformat (format); Deletes the text hyperlink field Objs. RemoveAt (i); Reinsert the text Objs. Insert (i, TR); }//delete picture hyperlink, keep picture if (Dobj is docpicture) {//Delete picture hyperlink field Objs. RemoveAt (i); Reinsert the picture Objs. Insert (i, dobj); } } } } } } }}
Test results:
This is all about the "how to manipulate Word hyperlinks in C #".
If you want to reprint, please specify the source!!
C # INSERT, format, delete word hyperlinks