/// <summary> /// Add a note to the FootNotes part and ensure it exists. /// </summary> /// <param name="description">The description of an acronym, abbreviation, some book references, ...</param> /// <returns>Returns the id of the footnote reference.</returns> protected int AddFootnoteReference(string description) { FootnotesPart fpart = mainPart.FootnotesPart; if (fpart == null) { fpart = mainPart.AddNewPart <FootnotesPart>(); } if (fpart.Footnotes == null) { // Insert a new Footnotes reference new Footnotes( new Footnote( new Paragraph( new ParagraphProperties { SpacingBetweenLines = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto } }, new Run( new SeparatorMark()) ) ) { Type = FootnoteEndnoteValues.Separator, Id = -1 }, new Footnote( new Paragraph( new ParagraphProperties { SpacingBetweenLines = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto } }, new Run( new ContinuationSeparatorMark()) ) ) { Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 0 }).Save(fpart); footnotesRef = 1; } else { // The footnotesRef Id is a required field and should be unique. You can assign yourself some hard-coded // value but that's absolutely not safe. We will loop through the existing Footnote // to retrieve the highest Id. foreach (var fn in fpart.Footnotes.Elements <Footnote>()) { if (fn.Id.HasValue && fn.Id > footnotesRef) { footnotesRef = (int)fn.Id.Value; } } footnotesRef++; } Run markerRun; Paragraph p; fpart.Footnotes.Append( new Footnote( p = new Paragraph( new ParagraphProperties { ParagraphStyleId = new ParagraphStyleId() { Val = htmlStyles.GetStyle("footnote text", StyleValues.Paragraph) } }, markerRun = new Run( new RunProperties { RunStyle = new RunStyle() { Val = htmlStyles.GetStyle("footnote reference", StyleValues.Character) } }, new FootnoteReferenceMark()), new Run( // Word insert automatically a space before the definition to separate the // reference number with its description new Text(" ") { Space = SpaceProcessingModeValues.Preserve }) ) ) { Id = footnotesRef }); // Description in footnote reference can be plain text or a web protocols/file share (like \\server01) Uri uriReference; Regex linkRegex = new Regex(@"^((https?|ftps?|mailto|file)://|[\\]{2})(?:[\w][\w.-]?)"); if (linkRegex.IsMatch(description) && Uri.TryCreate(description, UriKind.Absolute, out uriReference)) { HyperlinkRelationship extLink = fpart.AddHyperlinkRelationship(uriReference, true); var h = new Hyperlink( ) { History = true, Id = extLink.Id }; htmlStyles.EnsureKnownStyle(HtmlDocumentStyle.KnownStyles.Hyperlink); h.Append(new Run( new RunProperties { RunStyle = new RunStyle() { Val = htmlStyles.GetStyle("Hyperlink", StyleValues.Character) } }, new Text(description))); p.Append(h); } else { p.Append(new Run( new Text(description) { Space = SpaceProcessingModeValues.Preserve })); } if (!htmlStyles.DoesStyleExists("footnote reference")) { // Force the superscript style because if the footnote text style does not exists, // the rendering will be awful. markerRun.InsertInProperties(prop => prop.VerticalTextAlignment = new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript }); } fpart.Footnotes.Save(); return(footnotesRef); }
/// <summary> /// Add a note to the FootNotes part and ensure it exists. /// </summary> /// <param name="description">The description of an acronym, abbreviation, some book references, ...</param> /// <returns>Returns the id of the footnote reference.</returns> private int AddFootnoteReference(string description) { FootnotesPart fpart = mainPart.FootnotesPart; if (fpart == null) { fpart = mainPart.AddNewPart <FootnotesPart>(); } if (fpart.Footnotes == null) { // Insert a new Footnotes reference new Footnotes( new Footnote( new Paragraph( new ParagraphProperties { SpacingBetweenLines = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto } }, new Run( new SeparatorMark()) ) ) { Type = FootnoteEndnoteValues.Separator, Id = -1 }, new Footnote( new Paragraph( new ParagraphProperties { SpacingBetweenLines = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto } }, new Run( new ContinuationSeparatorMark()) ) ) { Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 0 }).Save(fpart); footnotesRef = 1; } else { // The footnotesRef Id is a required field and should be unique. You can assign yourself some hard-coded // value but that's absolutely not safe. We will loop through the existing Footnote // to retrieve the highest Id. foreach (var fn in fpart.Footnotes.Elements <Footnote>()) { if (fn.Id.HasValue && fn.Id > footnotesRef) { footnotesRef = (int)fn.Id.Value; } } footnotesRef++; } Run markerRun; Paragraph p; fpart.Footnotes.Append( new Footnote( p = new Paragraph( new ParagraphProperties { ParagraphStyleId = new ParagraphStyleId() { Val = htmlStyles.GetStyle("FootnoteText", StyleValues.Paragraph) } }, markerRun = new Run( new RunProperties { RunStyle = new RunStyle() { Val = htmlStyles.GetStyle("FootnoteReference", StyleValues.Character) } }, new FootnoteReferenceMark()), new Run( // Word insert automatically a space before the definition to separate the // reference number with its description new Text(" ") { Space = SpaceProcessingModeValues.Preserve }) ) ) { Id = footnotesRef }); // Description in footnote reference can be plain text or a web protocols/file share (like \\server01) Uri uriReference; Regex linkRegex = new Regex(@"^((https?|ftps?|mailto|file)://|[\\]{2})(?:[\w][\w.-]?)"); if (linkRegex.IsMatch(description) && Uri.TryCreate(description, UriKind.Absolute, out uriReference)) { // when URI references a network server (ex: \\server01), System.IO.Packaging is not resolving the correct URI and this leads // to a bad-formed XML not recognized by Word. To enforce the "original URI", a fresh new instance must be created uriReference = new Uri(uriReference.AbsoluteUri, UriKind.Absolute); HyperlinkRelationship extLink = fpart.AddHyperlinkRelationship(uriReference, true); var h = new Hyperlink( ) { History = true, Id = extLink.Id }; h.Append(new Run( new RunProperties { RunStyle = new RunStyle() { Val = htmlStyles.GetStyle("Hyperlink", StyleValues.Character) } }, new Text(description))); p.Append(h); } else { p.Append(new Run( new Text(description) { Space = SpaceProcessingModeValues.Preserve })); } fpart.Footnotes.Save(); return(footnotesRef); }