C # how to add Word text and image hyperlinks,
A hyperlink is a content link. By setting a hyperlink, you can connect objects to webpages and sites. The link target can be a webpage, image, email address, folder, or application. The link object can be text or image.
In the following content, I will introduce how to use C # programming language to set hyperlinks for text and images in Word documents. To do this, use the free version of Spire. Doc for. NET, where you can download the installation (https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-NET.html)
1. Add a text hyperlink
Step 1: create a Document instance and add Section
Document doc = new Document();Section section = doc.AddSection();
Step 2: Add a hyperlink to the URL
Paragraph para1 = section.AddParagraph();para1.AppendHyperlink("www.google.com","www.google.com",HyperlinkType.WebLink);
Step 3: Add a hyperlink to the email address
Paragraph para2 = section.AddParagraph();para2.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink);
Step 4: Add a hyperlink to an external file
Paragraph para3 = section. addParagraph (); string filePath = @ "C: \ Users \ Administrator \ Desktop \ descrinobelprize.docx"; para3.AppendHyperlink (filePath, "click to open the document", HyperlinkType. fileLink );
Step 5: Set the spacing between paragraphs
para1.Format.AfterSpacing = 15f;para2.Format.AfterSpacing = 15f;
Step 6: Save the document
Doc. SaveToFile (" .docx", FileFormat. Docx2013 );
After completing the operation steps, run the project to generate a file, as shown in:
2. Add an image hyperlink
Step 1: create a Document instance and add Section
Document doc = new Document();Section section = doc.AddSection();
Step 2: Add a paragraph
Paragraph para = section.AddParagraph();
Step 3: add an image to a section and add a website hyperlink
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 ("image hyperlink .docx", FileFormat. Docx2013 );
Complete the operation steps and run the program to get the following file:
The above is a code operation demonstration on how to add text and image hyperlinks using the C # programming language. I hope the above method will help you. Thank you for reading!