using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Validation; using W = DocumentFormat.OpenXml.Wordprocessing; namespace AIStudio.Tests.Tools.DocumentSegments.Word; /// /// Builds the Word documents of the tests in code, so that no binary document is checked in. /// internal static class TestWordDocument { /// /// The relationship of the link which every document of the tests has, for a W.Hyperlink to point to. /// public const string LINK_ID = "rIdManual"; private const string HEADER_ID = "rIdHeader"; private const string FOOTER_ID = "rIdFooter"; /// /// A document with nothing but the given paragraphs. /// /// Where to create the document. /// The paragraphs of its body. public static void Create(string path, params W.Paragraph[] paragraphs) { using var document = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document); var mainPart = document.AddMainDocumentPart(); mainPart.AddHyperlinkRelationship(new Uri("https://example.org/manual"), true, LINK_ID); mainPart.Document = new W.Document(new W.Body(paragraphs)); } /// /// A document with what Word documents commonly hold: a link, a tab, a reference to a footnote, a field /// with its result, a bookmark, words with mixed formatting, a header, a footer, a footnote, and an endnote. /// /// Where to create the document. public static void CreateSample(string path) { using var document = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document); var mainPart = document.AddMainDocumentPart(); mainPart.AddHyperlinkRelationship(new Uri("https://example.org/manual"), true, LINK_ID); var headerPart = mainPart.AddNewPart(HEADER_ID); headerPart.Header = new W.Header(Paragraph(Run("Confidential"))); var footerPart = mainPart.AddNewPart(FOOTER_ID); footerPart.Footer = new W.Footer(Paragraph(Run("Page footer"))); var endnotesPart = mainPart.AddNewPart(); endnotesPart.Endnotes = new W.Endnotes( new W.Endnote(Paragraph(new W.Run(new W.SeparatorMark()))) { Type = W.FootnoteEndnoteValues.Separator, Id = -1 }, new W.Endnote(Paragraph(new W.Run(new W.ContinuationSeparatorMark()))) { Type = W.FootnoteEndnoteValues.ContinuationSeparator, Id = 0 }, new W.Endnote(Paragraph(new W.Run(new W.EndnoteReferenceMark()), Run(" An endnote."))) { Id = 1 }); var footnotesPart = mainPart.AddNewPart(); footnotesPart.Footnotes = new W.Footnotes( new W.Footnote(Paragraph(new W.Run(new W.SeparatorMark()))) { Type = W.FootnoteEndnoteValues.Separator, Id = -1 }, new W.Footnote(Paragraph(new W.Run(new W.ContinuationSeparatorMark()))) { Type = W.FootnoteEndnoteValues.ContinuationSeparator, Id = 0 }, new W.Footnote(Paragraph(new W.Run(new W.FootnoteReferenceMark()), Run(" A note."))) { Id = 1 }); mainPart.Document = new W.Document(new W.Body( Paragraph(Run("Getting started")), Paragraph(Run("Read the "), new W.Hyperlink(Run("manual")) { Id = LINK_ID }, Run(" first."), new W.Run(new W.TabChar()), Run("Done")), Paragraph(Run("See the note"), new W.Run(new W.FootnoteReference { Id = 1 })), Paragraph( new W.Run(new W.FieldChar { FieldCharType = W.FieldCharValues.Begin }), new W.Run(new W.FieldCode(" REF _Ref1 \\h ") { Space = SpaceProcessingModeValues.Preserve }), new W.Run(new W.FieldChar { FieldCharType = W.FieldCharValues.Separate }), Run("Introduction"), new W.Run(new W.FieldChar { FieldCharType = W.FieldCharValues.End })), Paragraph(new W.BookmarkStart { Id = "1", Name = "_Ref1" }, Run("Chapter"), new W.BookmarkEnd { Id = "1" }), Paragraph(Run("Mixed "), Bold("bold"), Run(" words")), Paragraph(Run("2026")), new W.SectionProperties( new W.HeaderReference { Type = W.HeaderFooterValues.Default, Id = HEADER_ID }, new W.FooterReference { Type = W.HeaderFooterValues.Default, Id = FOOTER_ID }))); } public static W.Paragraph Paragraph(params OpenXmlElement[] content) => new(content); public static W.Run Run(string text, W.RunProperties? properties = null) => properties is null ? new W.Run(new W.Text(text) { Space = SpaceProcessingModeValues.Preserve }) : new W.Run(properties, new W.Text(text) { Space = SpaceProcessingModeValues.Preserve }); public static W.Run Bold(string text) => Run(text, new W.RunProperties(new W.Bold())); public static W.Run Italic(string text) => Run(text, new W.RunProperties(new W.Italic())); public static W.Hyperlink Link(string text) => new(Run(text)) { Id = LINK_ID }; /// /// What the validator finds, without where it finds it: new runs move the places of what the source already had. /// public static List ValidationErrorsOf(WordprocessingDocument document) => new OpenXmlValidator(FileFormatVersions.Office2019) .Validate(document) .Select(error => $"{error.Part?.Uri}: {error.Description}") .Order(StringComparer.Ordinal) .ToList(); }