If somebody like the post and its helpful in your work then, add comments.

Wednesday, July 15, 2009

Open XML : Adding Hyperlink to word document by WordProcessingML

To add hyperlink in the Microsoft word document. We have to write following code:

using (WordprocessingDocument WorDocument = WordprocessingDocument.Create(@"c:\Test.docx", WordprocessingDocumentType.Document))
{
// Add a new main document part.
MainDocumentPart mainPart = WorDocument.AddMainDocumentPart();
//Create Document tree for simple document.
mainPart.Document = new Document();
//Create Body (this element contains other elements that we want to include
Body body = new Body();

//Hyperlink to add
Paragraph HprPara = new Paragraph(
new Hyperlink(
new Run(
new RunProperties(
new RunStyleId(){ Val = "Hyperlink" }),
new Text("TestLink")
){ RsidRunProperties = "00356238" }
){ History = BooleanValues.One, Id = "rId4" }
){ RsidParagraphAddition = "00651E0C", RsidRunAdditionDefault = "00356238" };

//External realationship to make for hyperlink
ExternalRelationship exprRealationship = mainPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", new Uri("http://www.google.com"), "rId4");

body.Append(HprPara);

mainPart.Document.Append(body);
// Save changes to the main document part.
mainPart.Document.Save();
}

1 comment:

  1. How to add clickable hyperlink in the footer of the page or at the end of every page using Open XML.

    Intekhab

    ReplyDelete