Skip to content

Commit 8e943a6

Browse files
authored
Merge pull request OpenXmlDev#8 from Codeuctivity/SymbolFontToUnicode
Enabled image handler and text transform handler injection
2 parents d5879a7 + 37b79a2 commit 8e943a6

File tree

91 files changed

+901
-1440
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+901
-1440
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,6 @@ dotnet_naming_style.begins_with_i.required_prefix = I
199199
dotnet_naming_style.begins_with_i.required_suffix =
200200
dotnet_naming_style.begins_with_i.word_separator =
201201
dotnet_naming_style.begins_with_i.capitalization = pascal_case
202+
203+
# CS1591: Missing XML comment for publicly visible type or member
204+
dotnet_diagnostic.CS1591.severity = suggestion

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"preLaunchTask": "build",
1212
// If you have changed target frameworks, make sure to update the program path.
13-
"program": "${workspaceFolder}/OpenXmlPowerToolsExamples/DocumentAssembler/bin/Debug/netcoreapp3.1/DocumentAssembler.dll",
13+
"program": "${workspaceFolder}/OpenXmlPowerToolsExamples/DocumentAssembler/bin/Debug/net5.0/DocumentAssembler.dll",
1414
"args": [],
1515
"cwd": "${workspaceFolder}/OpenXmlPowerToolsExamples/DocumentAssembler",
1616
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console

OpenXmlPowerTools.Tests/ChartUpdaterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DocumentFormat.OpenXml.Packaging;
22
using OpenXmlPowerTools;
3+
using OpenXmlPowerTools.Tests;
34
using System;
45
using System.IO;
56
using Xunit;

OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DocumentFormat.OpenXml.Packaging;
22
using DocumentFormat.OpenXml.Validation;
33
using OpenXmlPowerTools;
4+
using OpenXmlPowerTools.Tests;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;

OpenXmlPowerTools.Tests/DocumentBuilderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DocumentFormat.OpenXml.Packaging;
22
using DocumentFormat.OpenXml.Validation;
33
using OpenXmlPowerTools;
4+
using OpenXmlPowerTools.Tests;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;

OpenXmlPowerTools.Tests/FormattingAssemblerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DocumentFormat.OpenXml.Packaging;
22
using DocumentFormat.OpenXml.Validation;
33
using OpenXmlPowerTools;
4+
using OpenXmlPowerTools.Tests;
45
using System;
56
using System.Collections.Generic;
67
using System.IO;

OpenXmlPowerTools.Tests/HtmlConverterTests.cs

Lines changed: 0 additions & 356 deletions
This file was deleted.

OpenXmlPowerTools.Tests/HtmlToWmlConverterTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DocumentFormat.OpenXml.Packaging;
22
using DocumentFormat.OpenXml.Validation;
33
using OpenXmlPowerTools;
4+
using OpenXmlPowerTools.Tests;
45
using System;
56
using System.IO;
67
using System.Linq;
@@ -360,10 +361,7 @@ public void HW001(string name)
360361

361362
var doc = HtmlToWmlConverter.ConvertHtmlToWml(defaultCss, usedAuthorCss, userCss, html, settings, null, s_ProduceAnnotatedHtml ? annotatedHtmlFi.FullName : null);
362363
Assert.NotNull(doc);
363-
if (doc != null)
364-
{
365-
SaveValidateAndFormatMainDocPart(destDocxFi, doc);
366-
}
364+
SaveValidateAndFormatMainDocPart(destDocxFi, doc);
367365
}
368366

369367
[Theory]
@@ -397,6 +395,11 @@ private static void SaveValidateAndFormatMainDocPart(FileInfo destDocxFi, WmlDoc
397395
{
398396
WmlDocument formattedDoc;
399397

398+
if (File.Exists(destDocxFi.FullName))
399+
{
400+
File.Delete(destDocxFi.FullName);
401+
}
402+
400403
doc.SaveAs(destDocxFi.FullName);
401404
using (var ms = new MemoryStream())
402405
{
@@ -407,12 +410,9 @@ private static void SaveValidateAndFormatMainDocPart(FileInfo destDocxFi, WmlDoc
407410
document.MainDocumentPart.PutXDocumentWithFormatting();
408411
var validator = new OpenXmlValidator();
409412
var errors = validator.Validate(document);
410-
var errorsString = errors
411-
.Select(e => e.Description + Environment.NewLine)
412-
.StringConcatenate();
413+
var errorsString = errors.Select(e => e.Description + Environment.NewLine).StringConcatenate();
413414

414-
// Assert that there were no errors in the generated document.
415-
Assert.Equal("", errorsString);
415+
Assert.True(errorsString.Length == 0, $"Error in {destDocxFi.FullName}\n{errorsString}");
416416
}
417417
formattedDoc = new WmlDocument(destDocxFi.FullName, ms.ToArray());
418418
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
using DocumentFormat.OpenXml.Packaging;
2+
using OpenXmlPowerTools;
3+
using OpenXmlPowerTools.OpenXMLWordprocessingMLToHtmlConverter;
4+
using OpenXmlPowerTools.Tests;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Xml.Linq;
9+
using Xunit;
10+
11+
namespace OxPt
12+
{
13+
public class WmlToHtmlConverterTests
14+
{
15+
// PowerShell oneliner that generates InlineData for all files in a directory
16+
// dir | % { '[InlineData("' + $_.Name + '")]' } | clip
17+
18+
[Theory]
19+
[InlineData("HC001-5DayTourPlanTemplate.docx")]
20+
[InlineData("HC002-Hebrew-01.docx")]
21+
[InlineData("HC003-Hebrew-02.docx")]
22+
[InlineData("HC004-ResumeTemplate.docx")]
23+
[InlineData("HC005-TaskPlanTemplate.docx")]
24+
[InlineData("HC006-Test-01.docx")]
25+
[InlineData("HC007-Test-02.docx")]
26+
[InlineData("HC008-Test-03.docx")]
27+
[InlineData("HC009-Test-04.docx")]
28+
[InlineData("HC010-Test-05.docx")]
29+
[InlineData("HC011-Test-06.docx")]
30+
[InlineData("HC012-Test-07.docx")]
31+
[InlineData("HC013-Test-08.docx")]
32+
[InlineData("HC014-RTL-Table-01.docx")]
33+
[InlineData("HC015-Vertical-Spacing-atLeast.docx")]
34+
[InlineData("HC016-Horizontal-Spacing-firstLine.docx")]
35+
[InlineData("HC017-Vertical-Alignment-Cell-01.docx")]
36+
[InlineData("HC018-Vertical-Alignment-Para-01.docx")]
37+
[InlineData("HC019-Hidden-Run.docx")]
38+
[InlineData("HC020-Small-Caps.docx")]
39+
[InlineData("HC021-Symbols.docx")]
40+
[InlineData("HC022-Table-Of-Contents.docx")]
41+
[InlineData("HC023-Hyperlink.docx")]
42+
[InlineData("HC024-Tabs-01.docx")]
43+
[InlineData("HC025-Tabs-02.docx")]
44+
[InlineData("HC026-Tabs-03.docx")]
45+
[InlineData("HC027-Tabs-04.docx")]
46+
[InlineData("HC028-No-Break-Hyphen.docx")]
47+
[InlineData("HC029-Table-Merged-Cells.docx")]
48+
[InlineData("HC030-Content-Controls.docx")]
49+
[InlineData("HC031-Complicated-Document.docx")]
50+
[InlineData("HC032-Named-Color.docx")]
51+
[InlineData("HC033-Run-With-Border.docx")]
52+
[InlineData("HC034-Run-With-Position.docx")]
53+
[InlineData("HC035-Strike-Through.docx")]
54+
[InlineData("HC036-Super-Script.docx")]
55+
[InlineData("HC037-Sub-Script.docx")]
56+
[InlineData("HC038-Conflicting-Border-Weight.docx")]
57+
[InlineData("HC039-Bold.docx")]
58+
[InlineData("HC040-Hyperlink-Fieldcode-01.docx")]
59+
[InlineData("HC041-Hyperlink-Fieldcode-02.docx")]
60+
[InlineData("HC042-Image-Png.docx")]
61+
[InlineData("HC043-Chart.docx")]
62+
[InlineData("HC044-Embedded-Workbook.docx")]
63+
[InlineData("HC045-Italic.docx")]
64+
[InlineData("HC046-BoldAndItalic.docx")]
65+
[InlineData("HC047-No-Section.docx")]
66+
[InlineData("HC048-Excerpt.docx")]
67+
[InlineData("HC049-Borders.docx")]
68+
[InlineData("HC050-Shaded-Text-01.docx")]
69+
[InlineData("HC051-Shaded-Text-02.docx")]
70+
[InlineData("HC060-Image-with-Hyperlink.docx")]
71+
[InlineData("HC061-Hyperlink-in-Field.docx")]
72+
public void HC001(string name)
73+
{
74+
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
75+
var sourceDocx = new FileInfo(Path.Combine(sourceDir.FullName, name));
76+
77+
var oxPtConvertedDestHtml = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceDocx.Name.Replace(".docx", "-3-OxPt.html")));
78+
ConvertToHtml(sourceDocx, oxPtConvertedDestHtml, false);
79+
}
80+
81+
[Theory]
82+
[InlineData("HC006-Test-01.docx")]
83+
public void HC002_NoCssClasses(string name)
84+
{
85+
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
86+
var sourceDocx = new FileInfo(Path.Combine(sourceDir.FullName, name));
87+
88+
var oxPtConvertedDestHtml = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceDocx.Name.Replace(".docx", "-5-OxPt-No-CSS-Classes.html")));
89+
ConvertToHtml(sourceDocx, oxPtConvertedDestHtml, true);
90+
}
91+
92+
private static void CopyFormattingAssembledDocx(FileInfo source, FileInfo dest)
93+
{
94+
var ba = File.ReadAllBytes(source.FullName);
95+
using var ms = new MemoryStream();
96+
ms.Write(ba, 0, ba.Length);
97+
using (var wordDoc = WordprocessingDocument.Open(ms, true))
98+
{
99+
RevisionAccepter.AcceptRevisions(wordDoc);
100+
var simplifyMarkupSettings = new SimplifyMarkupSettings
101+
{
102+
RemoveComments = true,
103+
RemoveContentControls = true,
104+
RemoveEndAndFootNotes = true,
105+
RemoveFieldCodes = false,
106+
RemoveLastRenderedPageBreak = true,
107+
108+
RemovePermissions = true,
109+
RemoveProof = true,
110+
RemoveRsidInfo = true,
111+
RemoveSmartTags = true,
112+
RemoveSoftHyphens = true,
113+
RemoveGoBackBookmark = true,
114+
ReplaceTabsWithSpaces = false,
115+
};
116+
MarkupSimplifier.SimplifyMarkup(wordDoc, simplifyMarkupSettings);
117+
118+
var formattingAssemblerSettings = new FormattingAssemblerSettings
119+
{
120+
RemoveStyleNamesFromParagraphAndRunProperties = false,
121+
ClearStyles = false,
122+
RestrictToSupportedLanguages = false,
123+
RestrictToSupportedNumberingFormats = false,
124+
CreateHtmlConverterAnnotationAttributes = true,
125+
OrderElementsPerStandard = false,
126+
ListItemRetrieverSettings =
127+
new ListItemRetrieverSettings()
128+
{
129+
ListItemTextImplementations = ListItemRetrieverSettings.DefaultListItemTextImplementations,
130+
},
131+
};
132+
133+
FormattingAssembler.AssembleFormatting(wordDoc, formattingAssemblerSettings);
134+
}
135+
var newBa = ms.ToArray();
136+
File.WriteAllBytes(dest.FullName, newBa);
137+
}
138+
139+
private static void ConvertToHtml(FileInfo sourceDocx, FileInfo destFileName, bool fabricateCssClasses)
140+
{
141+
var byteArray = File.ReadAllBytes(sourceDocx.FullName);
142+
using var memoryStream = new MemoryStream();
143+
memoryStream.Write(byteArray, 0, byteArray.Length);
144+
using var wDoc = WordprocessingDocument.Open(memoryStream, true);
145+
var outputDirectory = destFileName.Directory;
146+
destFileName = new FileInfo(Path.Combine(outputDirectory.FullName, destFileName.Name));
147+
var imageDirectoryName = destFileName.FullName.Substring(0, destFileName.FullName.Length - 5) + "_files";
148+
var pageTitle = (string)wDoc.CoreFilePropertiesPart.GetXDocument().Descendants(DC.title).FirstOrDefault();
149+
if (pageTitle == null)
150+
{
151+
pageTitle = sourceDocx.FullName;
152+
}
153+
154+
var settings = new WmlToHtmlConverterSettings()
155+
{
156+
PageTitle = pageTitle,
157+
FabricateCssClasses = fabricateCssClasses,
158+
CssClassPrefix = fabricateCssClasses ? "pt-" : null,
159+
RestrictToSupportedLanguages = false,
160+
RestrictToSupportedNumberingFormats = false
161+
};
162+
163+
var html = WmlToHtmlConverter.ConvertToHtml(wDoc, settings);
164+
165+
// Note: the xhtml returned by ConvertToHtmlTransform contains objects of type XEntity. PtOpenXmlUtil.cs define the XEntity class. See http://blogs.msdn.com/ericwhite/archive/2010/01/21/writing-entity-references-using-linq-to-xml.aspx for detailed explanation.
166+
// If you further transform the XML tree returned by ConvertToHtmlTransform, you must do it correctly, or entities will not be serialized properly.
167+
168+
var htmlString = html.ToString(SaveOptions.DisableFormatting);
169+
File.WriteAllText(destFileName.FullName, htmlString, Encoding.UTF8);
170+
}
171+
}
172+
}

OpenXmlPowerTools.Tests/OpenXmlPowerTools.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT' ">net472;net5.0</TargetFrameworks>
4+
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT' ">net472;net5.0;netcoreapp3.1</TargetFrameworks>
55
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT' ">net5.0</TargetFrameworks>
66
<LangVersion>8.0</LangVersion>
77
<EnableNETAnalyzers>true</EnableNETAnalyzers>

0 commit comments

Comments
 (0)