C # how to add Word text and image hyperlinks,

Source: Internet
Author: User

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!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.