locked
How to Add Hyperlink via SDK to Word RRS feed

  • Question

  • This may seem to be obvious to everyone, since I cannot find the answer... :-)

    I do have the code to add the hyperlink relationship:

    ExternalRelationship hyperlinkRelationship = _report.MainDocumentPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
                    , new Uri("http://www.microsoft.com"));
    
    But, I have not found out how to add the actual hyperlink to the document. This is the markup:

    <w:hyperlink r:id="">
       <w:r>
          <w:t>Microsoft</w:t>
       </w:r>
    </w:hyperlink>
    

    Where its ID will come from the ExternalRelationship just created (hyperlinkRelationship.Id).

    Would you happen to know how to add this hyperlink to the main document to a content control and share?

    Thank you!
    • Edited by ElsaC Tuesday, June 9, 2009 7:47 PM
    Tuesday, June 9, 2009 7:33 PM

All replies

  • Hi Elsa,

    For this kind of problems, you can use the DocumentReflector tool with SDK to see how to generated the correct C# code of the document.
    Firstly, you create the document you want in Word application. Then use the tool to open the file and reflect the code for you.

    For your case, you need some code look like: (supppose you want to insert a hyperlink in the 1st paragraph)

    Paragraph para = document.MainDocumentPart.Document.Descendants<Paragraph>().First();
    Hyperlink link = new Hyperlink(
    new Run(
    new RunProperties(
    new RunStyle() { Val = "Hyperlink" }),
    new Text("baidu")
    ) { RsidRunProperties = new HexBinaryValue() { Value = "002A46DA" } }
    ) { History = BooleanValues.One, Id = "rId4" };
    para.AppendChild<Hyperlink>(link);

    Besides, could you tell me more about your user scenario? what's the document will look like? and how will the result documents be used typically? Thanks:)
    Thursday, June 11, 2009 5:28 AM