private static Hyperlink CreateHyperlink(string text, string uri, MainDocumentPart main) { var hr = main.AddHyperlinkRelationship(new Uri(uri), true); return new Hyperlink( new Run( new RunProperties( new RunPropertiesBaseStyle( new Underline { Val = new EnumValue<UnderlineValues>(UnderlineValues.Single) }, new Color { Val = "0000FF" })), new Text(text))) { Id = hr.Id, TargetFrame = "_blank" }; }
/// <summary> /// Generates a hyperlink and embed it /// in a paragraph tag /// </summary> /// <param name="mainDocPart">The main doc part.</param> /// <param name="hyperLink">The hyper link.</param> /// <returns></returns> public static Paragraph GenerateParagraphWithHyperLink(MainDocumentPart mainDocPart, String hyperLink) { //this will be display as //the text String urlLabel = hyperLink; //build the hyperlink //file:// ensure that document does not corrupt System.Uri uri = new Uri(@"file://" + hyperLink); //add it to the document HyperlinkRelationship rel = mainDocPart.AddHyperlinkRelationship(uri, true); //get the hyperlink id string relationshipId = rel.Id; //create the new paragraph tag Paragraph newParagraph = new Paragraph( new DocumentFormat.OpenXml.Wordprocessing.Hyperlink( new ProofError() { Type = ProofingErrorValues.GrammarStart }, new DocumentFormat.OpenXml.Wordprocessing.Run( new DocumentFormat.OpenXml.Wordprocessing.RunProperties( new RunStyle() { Val = "Hyperlink" }), new DocumentFormat.OpenXml.Wordprocessing.Text(urlLabel) )) { History = OnOffValue.FromBoolean(true), Id = relationshipId }); return newParagraph; }
// Adds child parts and generates content of the specified part. public static void CreateMemo(MainDocumentPart part) { FontTablePart fontTablePart1 = part.AddNewPart<FontTablePart>("rId8"); GenerateFontTablePart1Content(fontTablePart1); DocumentSettingsPart documentSettingsPart1 = part.AddNewPart<DocumentSettingsPart>("rId3"); GenerateDocumentSettingsPart1Content(documentSettingsPart1); documentSettingsPart1.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new System.Uri("file:///C:\\Users\\Jia\\OneDrive\\Honor%20Council\\Memo%20template.dotx", System.UriKind.Absolute), "rId1"); StyleDefinitionsPart styleDefinitionsPart1 = part.AddNewPart<StyleDefinitionsPart>("rId2"); GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1); CustomXmlPart customXmlPart1 = part.AddNewPart<CustomXmlPart>("application/xml", "rId1"); GenerateCustomXmlPart1Content(customXmlPart1); CustomXmlPropertiesPart customXmlPropertiesPart1 = customXmlPart1.AddNewPart<CustomXmlPropertiesPart>("rId1"); GenerateCustomXmlPropertiesPart1Content(customXmlPropertiesPart1); EndnotesPart endnotesPart1 = part.AddNewPart<EndnotesPart>("rId6"); GenerateEndnotesPart1Content(endnotesPart1); FootnotesPart footnotesPart1 = part.AddNewPart<FootnotesPart>("rId5"); GenerateFootnotesPart1Content(footnotesPart1); WebSettingsPart webSettingsPart1 = part.AddNewPart<WebSettingsPart>("rId4"); GenerateWebSettingsPart1Content(webSettingsPart1); ThemePart themePart1 = part.AddNewPart<ThemePart>("rId9"); GenerateThemePart1Content(themePart1); part.AddHyperlinkRelationship(new System.Uri("mailto:[email protected]", System.UriKind.Absolute), true, "rId7"); GeneratePartContent(part); }