Skip to content

Commit

Permalink
Add BasicDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
rstm-sf committed Nov 27, 2022
1 parent e8af583 commit b268a57
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 26 deletions.
66 changes: 62 additions & 4 deletions OfficeIMO.VerifyTests/VerifyTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using DocumentFormat.OpenXml.CustomProperties;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using VerifyTests;
using VerifyXunit;

Expand All @@ -9,7 +15,7 @@ namespace OfficeIMO.VerifyTests;
[UsesVerify]
public abstract class VerifyTestBase {

private static XmlWriterSettings _xmlWriterSettings = new() {
private static readonly XmlWriterSettings XmlWriterSettings = new() {
Indent = true,
NewLineOnAttributes = false,
IndentChars = " ",
Expand All @@ -28,13 +34,65 @@ protected static VerifySettings GetSettings() {
return settings;
}

protected static string FormatXml(string value) {
protected static string ToVerifyResult(WordprocessingDocument document) {
var result = new StringBuilder();
foreach (var id in document.Parts) {
if (id.OpenXmlPart.RootElement is null)
continue;
var xml = FormatXml(id.OpenXmlPart.RootElement.OuterXml);
result.AppendLine(id.OpenXmlPart.Uri.ToString());
result.AppendLine("<!----------------------------------------------------------------------------------->");
result.AppendLine(xml);
result.AppendLine("<!----------------------------------------------------------------------------------->");
}

return result.ToString();
}

protected static void NormalizeWord(WordprocessingDocument document) {
NormalizeDocument(document.MainDocumentPart?.Document);
NormalizeCustomFilePropertiesPart(document.CustomFilePropertiesPart);
}

private static string FormatXml(string value) {
using var textReader = new StringReader(value);
using var xmlReader = XmlReader.Create(
textReader, new XmlReaderSettings { ConformanceLevel = _xmlWriterSettings.ConformanceLevel } );
textReader, new XmlReaderSettings { ConformanceLevel = XmlWriterSettings.ConformanceLevel } );
using var textWriter = new StringWriter();
using (var xmlWriter = XmlWriter.Create(textWriter, _xmlWriterSettings))
using (var xmlWriter = XmlWriter.Create(textWriter, XmlWriterSettings))
xmlWriter.WriteNode(xmlReader, true);
return textWriter.ToString();
}

private static void NormalizeDocument(Document? document) {
if (document is null)
return;

var i = 1;
foreach (var hyperlink in document.Descendants<Hyperlink>()) {
hyperlink.Id = "R" + i.ToString("X8");
i++;
}

i = 1;
foreach (var headerReference in document.Descendants<HeaderReference>()) {
headerReference.Id = "R" + i.ToString("X8");
i++;
}

i = 1;
foreach (var footerReference in document.Descendants<FooterReference>()) {
footerReference.Id = "R" + i.ToString("X8");
i++;
}
}

private static void NormalizeCustomFilePropertiesPart(CustomFilePropertiesPart? part) {
var fileTime = part?.Properties
.FirstOrDefault(x => ((CustomDocumentProperty?)x)?.VTFileTime != null);
if (fileTime != null) {
((CustomDocumentProperty?) fileTime)!.VTFileTime!.Text =
DateTimeOffset.MaxValue.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
}
}
}
21 changes: 5 additions & 16 deletions OfficeIMO.VerifyTests/Word/AdvancedDocumentTests.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
using System;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;
using VerifyXunit;
using Xunit;

using Color = SixLabors.ImageSharp.Color;

namespace OfficeIMO.VerifyTests.Word;

public class AdvancedDocumentTests : VerifyTestBase {

private static async Task DoTest(WordprocessingDocument wordprocessingDocument) {
NormalizeWord(wordprocessingDocument);

var result = new StringBuilder();
foreach (var id in wordprocessingDocument.Parts) {
if (id.OpenXmlPart.RootElement is null)
continue;
var xml = FormatXml(id.OpenXmlPart.RootElement.OuterXml);
result.AppendLine(id.OpenXmlPart.Uri.ToString());
result.AppendLine("------------");
result.AppendLine(xml);
result.AppendLine("------------");
}

await Verifier.Verify(result.ToString(), GetSettings());
private static async Task DoTest(WordprocessingDocument document) {
NormalizeWord(document);
var result = ToVerifyResult(document);
await Verifier.Verify(result, GetSettings());
}

[Fact]
Expand Down
50 changes: 50 additions & 0 deletions OfficeIMO.VerifyTests/Word/BasicDocument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;
using VerifyXunit;
using Xunit;

using Color = SixLabors.ImageSharp.Color;

namespace OfficeIMO.VerifyTests.Word;

public class BasicDocument : VerifyTestBase {

private static async Task DoTest(WordprocessingDocument document) {
NormalizeWord(document);
var result = ToVerifyResult(document);
await Verifier.Verify(result, GetSettings());
}

[Fact]
public async Task BasicEmptyWord() {
using var document = WordDocument.Create();
document.BuiltinDocumentProperties.Title = "This is my title";
document.BuiltinDocumentProperties.Creator = "Przemysław Kłys";
document.BuiltinDocumentProperties.Keywords = "word, docx, test";
document.Save();

await DoTest(document._wordprocessingDocument);
}

[Fact]
public async Task BasicWord() {
using var document = WordDocument.Create();
var paragraph = document.AddParagraph("Adding paragraph with some text");
paragraph.ParagraphAlignment = JustificationValues.Center;

paragraph.Color = Color.Red;

paragraph = document.AddParagraph("Adding another paragraph with some more text");
paragraph.Bold = true;
paragraph = paragraph.AddText(" , but now we also decided to add more text to this paragraph using different style");
paragraph.Underline = UnderlineValues.DashLong;
paragraph = paragraph.AddText(" , and we still continue adding more text to existing paragraph.");
paragraph.Color = Color.CornflowerBlue;

document.Save();

await DoTest(document._wordprocessingDocument);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/word/document.xml
------------
<!----------------------------------------------------------------------------------->
<?xml version="1.0" encoding="utf-16"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
Expand Down Expand Up @@ -816,16 +816,16 @@
</w:sectPr>
</w:body>
</w:document>
------------
<!----------------------------------------------------------------------------------->
/docProps/app.xml
------------
<!----------------------------------------------------------------------------------->
<?xml version="1.0" encoding="utf-16"?>
<ap:Properties xmlns:ap="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">
<ap:Company>Evotec Services</ap:Company>
</ap:Properties>
------------
<!----------------------------------------------------------------------------------->
/docProps/custom.xml
------------
<!----------------------------------------------------------------------------------->
<?xml version="1.0" encoding="utf-16"?>
<op:Properties xmlns:op="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties">
<op:property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="TestProperty">
Expand All @@ -838,4 +838,4 @@
<vt:bool xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">true</vt:bool>
</op:property>
</op:Properties>
------------
<!----------------------------------------------------------------------------------->
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/word/document.xml
<!----------------------------------------------------------------------------------->
<?xml version="1.0" encoding="utf-16"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:sectPr />
</w:body>
</w:document>
<!----------------------------------------------------------------------------------->
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/word/document.xml
<!----------------------------------------------------------------------------------->
<?xml version="1.0" encoding="utf-16"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:pPr>
<w:jc w:val="center" />
</w:pPr>
<w:r>
<w:rPr>
<w:color w:val="FF0000" />
</w:rPr>
<w:t xml:space="preserve">Adding paragraph with some text</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr />
<w:r>
<w:rPr>
<w:b />
<w:bCs />
</w:rPr>
<w:t xml:space="preserve">Adding another paragraph with some more text</w:t>
</w:r>
<w:r>
<w:rPr>
<w:u w:val="dashLong" />
</w:rPr>
<w:t xml:space="preserve"> , but now we also decided to add more text to this paragraph using different style</w:t>
</w:r>
<w:r>
<w:rPr>
<w:color w:val="6495ED" />
</w:rPr>
<w:t xml:space="preserve"> , and we still continue adding more text to existing paragraph.</w:t>
</w:r>
</w:p>
<w:sectPr />
</w:body>
</w:document>
<!----------------------------------------------------------------------------------->

0 comments on commit b268a57

Please sign in to comment.