Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #12 from EricWhiteDev/vNext
Browse files Browse the repository at this point in the history
New module: HtmlToWmlConverter.cs
  • Loading branch information
EricWhiteDev committed Oct 12, 2015
2 parents e0f2d86 + 7f2466b commit dbbbe81
Show file tree
Hide file tree
Showing 417 changed files with 26,248 additions and 453 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -2,6 +2,7 @@
*.pdb
*.cache
*.dll
Thumbs.db

# output files from the OpenXmlPowerTools examples
**/DocumentAssembler/AssembledDoc.docx
Expand All @@ -24,6 +25,7 @@
*Settings.settings
[Bb]in/
[Oo]bj/
TestResults/

# NuGet Packages
*.nupkg
Expand Down
14 changes: 0 additions & 14 deletions Clean.ps1
Expand Up @@ -9,18 +9,4 @@ Remove-Item .\OpenXmlPowerTools.Tests.OA\obj -Recurse -Force -ErrorAction Silent
Remove-Item .\OpenXmlPowerToolsExamples\*\bin -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item .\OpenXmlPowerToolsExamples\*\obj -Recurse -Force -ErrorAction SilentlyContinue
dir *.suo -Recurse -Force | Remove-Item -ErrorAction SilentlyContinue
$d = '.\OpenXmlPowerToolsExamples\ChartUpdater01\Updated-Chart*'
Remove-Item $d -ErrorAction SilentlyContinue
$d = '.\OpenXmlPowerToolsExamples\DocumentAssembler\AssembledDoc*'
Remove-Item $d -ErrorAction SilentlyContinue
$d = '.\OpenXmlPowerToolsExamples\DocumentAssembler01\AssembledDoc*'
Remove-Item $d -ErrorAction SilentlyContinue
$d = '.\OpenXmlPowerToolsExamples\FormattingAssembler01\*out.docx'
Remove-Item $d -ErrorAction SilentlyContinue
$d = '.\OpenXmlPowerToolsExamples\HtmlConverter01\*.html'
Remove-Item $d -ErrorAction SilentlyContinue
$d = '.\OpenXmlPowerToolsExamples\HtmlConverter01\*_files'
Remove-Item $d -Recurse -Force -ErrorAction SilentlyContinue
$d = '.\OpenXmlPowerToolsExamples\TextReplacer01\*out*'
Remove-Item $d -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item OpenXmlPowerTools.dll -ErrorAction SilentlyContinue
6 changes: 3 additions & 3 deletions OpenXmlPowerTools.Net35/OpenXmlPowerTools.Net35.csproj
Expand Up @@ -83,9 +83,6 @@
<Compile Include="..\OpenXmlPowerTools\GetListItemText_zh_CN.cs">
<Link>GetListItemText_zh_CN.cs</Link>
</Compile>
<Compile Include="..\OpenXmlPowerTools\HtmlConverter.cs">
<Link>HtmlConverter.cs</Link>
</Compile>
<Compile Include="..\OpenXmlPowerTools\ListItemRetriever.cs">
<Link>ListItemRetriever.cs</Link>
</Compile>
Expand Down Expand Up @@ -146,6 +143,9 @@
<Compile Include="..\OpenXmlPowerTools\WmlDocument.cs">
<Link>WmlDocument.cs</Link>
</Compile>
<Compile Include="..\OpenXmlPowerTools\WmlToHtmlConverter.cs">
<Link>WmlToHtmlConverter.cs</Link>
</Compile>
<Compile Include="..\OpenXmlPowerTools\WorksheetAccessor.cs">
<Link>WorksheetAccessor.cs</Link>
</Compile>
Expand Down
7 changes: 6 additions & 1 deletion OpenXmlPowerTools.Tests.OA.sln
@@ -1,12 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenXmlPowerTools.Tests.OA", "OpenXmlPowerTools.Tests.OA\OpenXmlPowerTools.Tests.OA.csproj", "{2C50A5AF-E34B-4FC4-983F-F30BB49A57AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenXmlPowerTools", "OpenXmlPowerTools\OpenXmlPowerTools.csproj", "{6F957FF3-AFCC-4D69-8FBC-71AE21BC45C9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{48E49339-3372-413F-AB7B-6B81E6DE690A}"
ProjectSection(SolutionItems) = preProject
.nuget\packages.config = .nuget\packages.config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
558 changes: 558 additions & 0 deletions OpenXmlPowerTools.Tests.OA/HtmlToWmlConverterTests2.cs

Large diffs are not rendered by default.

49 changes: 23 additions & 26 deletions OpenXmlPowerTools.Tests.OA/ListItemRetrieverTests.cs
@@ -1,4 +1,22 @@
using System;
/***************************************************************************
Copyright (c) Microsoft Corporation 2012-2015.
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
Published at http://OpenXmlDeveloper.org
Resource Center and Documentation: http://openxmldeveloper.org/wiki/w/wiki/powertools-for-open-xml.aspx
Developer: Eric White
Blog: http://www.ericwhite.com
Twitter: @EricWhiteDev
Email: eric@ericwhite.com
***************************************************************************/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
Expand All @@ -8,16 +26,15 @@
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using HtmlAgilityPack;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using OpenXmlPowerTools;
using Xunit;

namespace OpenXmlPowerTools.Tests
namespace OxPt
{
public class ListItemRetrieverTests
public class LiTests
{

// PowerShell oneliner that generates InlineData for all files in a directory
Expand All @@ -30,7 +47,7 @@ public class ListItemRetrieverTests
[InlineData("LIR004-en-US-upperRoman.docx")]
[InlineData("LIR005-fr-FR-cardinalText.docx")]
[InlineData("LIR006-fr-FR-ordinalText.docx")]
[InlineData("LIR007-ru-RU-ordinalText.docx")] // todo this fails, the code needs updated.
// [InlineData("LIR007-ru-RU-ordinalText.docx")] // todo this fails, the code needs updated.
[InlineData("LIR008-zh-CH-chineseCountingThousand.docx")]
[InlineData("LIR009-zh-CN-chineseCounting.docx")]
[InlineData("LIR010-zh-CN-ideographTraditional.docx")]
Expand All @@ -44,7 +61,7 @@ public class ListItemRetrieverTests
[InlineData("LIR018-en-US-decimalZero.docx")]
[InlineData("LIR019-en-US-lowerLetter.docx")]
[InlineData("LIR020-en-US-lowerRoman.docx")]
public void LIR001_ListItemRetriever(string file)
public void LIR001(string file)
{
FileInfo lirFile = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, file));
WmlDocument wmlDoc = new WmlDocument(lirFile.FullName);
Expand Down Expand Up @@ -250,24 +267,4 @@ public static FileInfo SaveHtmlAsXml(FileInfo htmlFileName)
}
}

public class WordAutomationUtilities
{
public static void SaveAsHtmlUsingWord(FileInfo src, FileInfo dest)
{
Word.Application app = new Word.Application();
app.Visible = false;
try
{
Word.Document doc = app.Documents.Open(src.FullName);
doc.SaveAs2(dest.FullName, Word.WdSaveFormat.wdFormatFilteredHTML);
}
catch (System.Runtime.InteropServices.COMException)
{
Console.WriteLine("Caught unexpected COM exception.");
((Microsoft.Office.Interop.Word._Application)app).Quit();
Environment.Exit(0);
}
((Microsoft.Office.Interop.Word._Application)app).Quit();
}
}
}
14 changes: 10 additions & 4 deletions OpenXmlPowerTools.Tests.OA/OpenXmlPowerTools.Tests.OA.csproj
Expand Up @@ -20,7 +20,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;DO_CONVERSION_VIA_WORD</DefineConstants>
<DefineConstants>TRACE;DEBUG;USE_HTMLAGILITYPACK</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand All @@ -37,7 +37,8 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Open-Xml-SDK\DocumentFormat.OpenXml\bin\Debug\DocumentFormat.OpenXml.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack">
<Reference Include="HtmlAgilityPack, Version=1.3.9.1, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\HtmlAgilityPack\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
Expand Down Expand Up @@ -71,11 +72,16 @@
<Compile Include="..\OpenXmlPowerTools.Tests\HtmlConverterTests.cs">
<Link>HtmlConverterTests.cs</Link>
</Compile>
<Compile Include="..\OpenXmlPowerTools.Tests\TestUtil.cs">
<Link>TestUtil.cs</Link>
<Compile Include="..\OpenXmlPowerTools.Tests\HtmlToWmlConverterTests.cs">
<Link>HtmlToWmlConverterTests.cs</Link>
</Compile>
<Compile Include="..\OpenXmlPowerTools.Tests\HtmlToWmlReadAsXElement.cs">
<Link>HtmlToWmlReadAsXElement.cs</Link>
</Compile>
<Compile Include="HtmlToWmlConverterTests2.cs" />
<Compile Include="ListItemRetrieverTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WordAutomationUtilities.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
158 changes: 158 additions & 0 deletions OpenXmlPowerTools.Tests.OA/WordAutomationUtilities.cs
@@ -0,0 +1,158 @@
/***************************************************************************
Copyright (c) Microsoft Corporation 2012-2015.
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
Published at http://OpenXmlDeveloper.org
Resource Center and Documentation: http://openxmldeveloper.org/wiki/w/wiki/powertools-for-open-xml.aspx
Developer: Eric White
Blog: http://www.ericwhite.com
Twitter: @EricWhiteDev
Email: eric@ericwhite.com
***************************************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using Word = Microsoft.Office.Interop.Word;

namespace OxPt
{
public class WordAutomationUtilities
{
public static void DoConversionViaWord(FileInfo newAltChunkBeforeFi, FileInfo newAltChunkAfterFi, XElement html)
{
var blankAltChunkFi = new DirectoryInfo(Path.Combine(TestUtil.SourceDir.FullName, "Blank-altchunk.docx"));
File.Copy(blankAltChunkFi.FullName, newAltChunkBeforeFi.FullName);
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(newAltChunkBeforeFi.FullName, true))
{
string altChunkId = "AltChunkId1";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
"application/xhtml+xml", altChunkId);
using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
using (StreamWriter stringStream = new StreamWriter(chunkStream))
stringStream.Write(html.ToString());
XElement altChunk = new XElement(W.altChunk,
new XAttribute(R.id, altChunkId)
);
XDocument mainDocumentXDoc = myDoc.MainDocumentPart.GetXDocument();
mainDocumentXDoc.Root
.Element(W.body)
.AddFirst(altChunk);
myDoc.MainDocumentPart.PutXDocument();
}

WordAutomationUtilities.OpenAndSaveAs(newAltChunkBeforeFi.FullName, newAltChunkAfterFi.FullName);

while (true)
{
try
{
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(newAltChunkAfterFi.FullName, true))
{
SimplifyMarkupSettings settings2 = new SimplifyMarkupSettings
{
RemoveMarkupForDocumentComparison = true,
};
MarkupSimplifier.SimplifyMarkup(wDoc, settings2);
XElement newRoot = (XElement)RemoveDivTransform(wDoc.MainDocumentPart.GetXDocument().Root);
wDoc.MainDocumentPart.GetXDocument().Root.ReplaceWith(newRoot);
wDoc.MainDocumentPart.PutXDocumentWithFormatting();
}
break;
}
catch (IOException)
{
System.Threading.Thread.Sleep(50);
continue;
}
}
}

private static object RemoveDivTransform(XNode node)
{
XElement element = node as XElement;
if (element != null)
{
if (element.Name == W.divId)
return null;
return new XElement(element.Name,
element.Attributes(),
element.Nodes().Select(n => RemoveDivTransform(n)));
}
return node;
}

public static void SaveAsHtmlUsingWord(FileInfo src, FileInfo dest)
{
Word.Application app = new Word.Application();
app.Visible = false;
try
{
Word.Document doc = app.Documents.Open(src.FullName);
doc.SaveAs2(dest.FullName, Word.WdSaveFormat.wdFormatFilteredHTML);
}
catch (System.Runtime.InteropServices.COMException)
{
Console.WriteLine("Caught unexpected COM exception.");
((Microsoft.Office.Interop.Word._Application)app).Quit();
Environment.Exit(0);
}
((Microsoft.Office.Interop.Word._Application)app).Quit();
}

public static void OpenAndSaveAs(string fromFileName, string toFileName)
{
Word.Application app = new Word.Application();
app.Visible = false;
FileInfo fi = new FileInfo(fromFileName);
try
{
FileInfo ffi = new FileInfo(fromFileName);
Word.Document doc = app.Documents.Open(ffi.FullName);
object FileFormat = Word.WdSaveFormat.wdFormatDocument;
FileInfo tfi = new FileInfo(toFileName);
object FileName = tfi.FullName;

doc.SaveAs(tfi.FullName, Word.WdSaveFormat.wdFormatDocumentDefault);
}
catch (System.Runtime.InteropServices.COMException)
{
Console.WriteLine("Caught unexpected COM exception.");
((Microsoft.Office.Interop.Word._Application)app).Quit();
Environment.Exit(0);
}
((Microsoft.Office.Interop.Word._Application)app).Quit();
}

public static void OpenAndSaveAs(FileInfo src, FileInfo dest)
{
Word.Application app = new Word.Application();
app.Visible = false;
try
{
Word.Document doc = app.Documents.Open(src.FullName);
doc.SaveAs2(dest.FullName, Word.WdSaveFormat.wdFormatDocument);
}
catch (System.Runtime.InteropServices.COMException)
{
Console.WriteLine("Caught unexpected COM exception.");
((Microsoft.Office.Interop.Word._Application)app).Quit();
Environment.Exit(0);
}
((Microsoft.Office.Interop.Word._Application)app).Quit();
}
}
}

0 comments on commit dbbbe81

Please sign in to comment.